CMD批次变量不会保存FFprobe输出 [英] CMD Batch Variable Won't Save FFprobe Output

查看:46
本文介绍了CMD批次变量不会保存FFprobe输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CMD批处理脚本,它将mp4视频的文件夹转换为webm.

I have an CMD Batch Script that will convert a folder of mp4 videos to webm.

您将需要:

  • FFmpeg /FFprobe已安装并在环境变量中设置为从CMD运行.
  • 一个带有mp4的文件夹,供FFprobe解析.
  • FFmpeg/FFprobe installed and set in Environment Variables to run from CMD.
  • A folder with an mp4 for FFprobe to parse.

为了简单起见,这只是脚本的第一部分,显示了Video Bitrate变量.

To make it easy, this is only the first part of the script, showing the Video Bitrate variable.

这里是完整的脚本,只需替换路径即可.
https://pastebin.com/raw/3ng77Exz

Here is a full script, just replace the paths.
https://pastebin.com/raw/3ng77Exz

脚本的工作方式:

  • 浏览文件夹中的所有视频
  • 具有FFprobe解析视频的比特率并将其保存到%V%vBitrate%.
  • 具有FFmpeg使用%V.如-b:v %V将成为解析值 -b:v 9401k.
  • 使用已解析的比特率将每个视频从mp4转换为webm
  • Loops through all videos in folder
  • Has FFprobe parse the Video's Bitrate and save it to %V and %vBitrate%.
  • Has FFmpeg use %V. Such as -b:v %V will become the parsed value -b:v 9401k.
  • Converts each video from mp4 to webm using the parsed Bitrate

我无法将FFprobe的输出保存到变量中.我想出了一种解决方法,首先将比特率值保存到temp file,然后将其导入到%vBitrate%变量中.

I can't get FFprobe's Output to save to the variable. I've come up with a workaround, having it first save the bitrate value to a temp file, then import that to the %vBitrate% variable.

示例:(%V > tmp_vBitrate) & SET /p vBitrate= < tmp_vBitrate.

工程

临时文件变量

cd "C:\Users\Matt\Videos\" && for %f in (*.mp4) do ffprobe -i "C:\Users\Matt\Desktop\Test\%~f" -select_streams v:0 -show_entries stream=bit_rate -v quiet -of csv="p=0" & for /f "tokens=*" %V in ("ffprobe -i "%~f" -select_streams v:0 -show_entries stream=bit_rate -v quiet -of csv=p=0") do (echo ) & (%V > tmp_vBitrate) & SET /p vBitrate= < tmp_vBitrate & del tmp_vBitrate & for /F %V in ('echo %vBitrate%') do (echo %V)


不起作用

内存变量

cd "C:\Users\Matt\Videos\" && for %f in (*.mp4) do ffprobe -i "C:\Users\Matt\Desktop\Test\%~f" -select_streams v:0 -show_entries stream=bit_rate -v quiet -of csv="p=0" & for /f "tokens=*" %V in ("ffprobe -i "%~f" -select_streams v:0 -show_entries stream=bit_rate -v quiet -of csv=p=0") do (echo ) & SET vBitrate=%V & for /F %V in ('echo %vBitrate%') do (echo %V)


测试

运行第一个命令.完成后,在CMD中键入echo %vBitrate%,然后按Enter.您将看到已解析的最后一个mp4文件的比特率.

Run the first command. When it is finished, type echo %vBitrate% in CMD and press Enter. You'll see the bitrate of the last mp4 file parsed.

对第二个命令执行相同的操作,您会发现它不起作用.

Do the same for the second command and you'll see it doesn't work.

我想摆脱Temp File Variable并使第二条命令起作用.

I would like to get rid of the Temp File Variable and get the second command to work.

(%V > tmp_vBitrate) & SET /p vBitrate= < tmp_vBitrateSET vBitrate=%V.

也许可以简化整个过程?我使用的变量有误吗?

Maybe this whole thing can be simplified? Am I using the variables wrong?

推荐答案

我相信我使用了不同的FFmpeg命令和2^>^&1解决了这个问题.

I believe I solved this using different FFmpeg commands with 2^>^&1.

现在,它不再使用临时文件来设置变量.

Now it no longer uses a temporary file to set the variable.

视频比特率

for /F "delims=" %V in ('@ffprobe -v error -select_streams v:0 -show_entries stream^=bit_rate -of default^=noprint_wrappers^=1:nokey^=1 "%~f" 2^>^&1') do (SET vBitrate=%V) & for /F %V in ('echo %vBitrate%') do (echo %V)

音频比特率

for /F "delims=" %A in ('@ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default^=noprint_wrappers^=1:nokey^=1 "%~f" 2^>^&1') do (SET aBitrate=%A) & for /F %A in ('echo %aBitrate%') do (echo %A)

完整脚本

这里是完整的batch script,它将把mp4转换为webm,并使用video birate = (((size * 8) / 1000) / duration) * 1000自动计算和匹配比特率.

Here is a full batch script that will convert mp4 to webm and calculate and match the bitrates automatically using video birate = (((size * 8) / 1000) / duration) * 1000.

cd "C:\Users\Matt\Videos\" && for %f in (*.mp4) do (for /F "delims=" %S in ('@ffprobe -v error -select_streams v:0 -show_entries format^=size -of default^=noprint_wrappers^=1:nokey^=1 "%~f" 2^>^&1') do (SET size=%S) & for /F %S in ('echo %size%') do (echo %S) & for /F "delims=" %D in ('@ffprobe -v error -select_streams v:0 -show_entries format^=duration -of default^=noprint_wrappers^=1:nokey^=1 "%~f" 2^>^&1') do (SET duration=%D) & for /F "tokens=1 delims=." %R in ('echo %duration%') do (SET duration=%R) & for /F %D in ('echo %duration%') do (echo %D) & for /F "delims=" %V in ('@ffprobe -v error -select_streams v:0 -show_entries stream^=bit_rate -of default^=noprint_wrappers^=1:nokey^=1 "%~f" 2^>^&1') do (SET vBitrate=%V) & for /F %V in ('echo %vBitrate%') do (echo %V) & (if %V EQU N/A (SET /a vBitrate=%S*8/1000/%D*1000) ELSE (echo Video Bitrate Detected)) & for /F %V in ('echo %vBitrate%') do (echo %V) & for /F "delims=" %A in ('@ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default^=noprint_wrappers^=1:nokey^=1 "%~f" 2^>^&1') do (echo) & SET aBitrate=%A & for /F %A in ('echo %aBitrate%') do (echo %A) & (IF %A EQU N/A (SET aBitrate=320000)) & for /F %A in ('echo %aBitrate%') do (echo %A) & (IF %A gtr 500000 (SET aBitrate=500000) ELSE (echo Bitrate within Vorbis Limit of 500k)) & for /F %A in ('echo %aBitrate%') do (echo %A) && ffmpeg -y -i "C:\Users\Matt\Videos\%~f" -vcodec libvpx -quality good -cpu-used 0 -b:v %V -pass 1 -acodec libvorbis -b:a %A -map 0:v:0? -map 0:a:0? -sn -map_metadata 0 -threads 8 "C:\Users\Matt\Videos\%~nf.webm" && ffmpeg -y -i "C:\Users\Matt\Videos\%~f" -vcodec libvpx -quality good -cpu-used 0 -b:v %V -pass 2 -acodec libvorbis -b:a %A -map 0:v:0? -map 0:a:0? -sn -map_metadata 0 -threads 8 "C:\Users\Matt\Videos\%~nf.webm")

这篇关于CMD批次变量不会保存FFprobe输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆