I have an CMD Batch Script that will convert a folder of mp4 videos to webm.
You will need:
- FFmpeg/FFprobe installed and set in Environment Variables to run from CMD.
- A folder with an mp4 for FFprobe to parse.
To make it easy, this is only the first part of the script, showing the Video Bitrate
variable.
Here is a full script, just replace the paths.
https://pastebin.com/raw/3ng77Exz
How the Script works:
- 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
Problem
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.
Example: (%V > tmp_vBitrate) & SET /p vBitrate= < tmp_vBitrate
.
Works
Temp File Variable
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)
Does Not Work
Memory Variable
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)
Testing It
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.
Solution
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_vBitrate
to just SET vBitrate=%V
.
Maybe this whole thing can be simplified? Am I using the variables wrong?
I believe I solved this using different FFmpeg commands with
2^>^&1
.Now it no longer uses a temporary file to set the variable.
Video Bitrate
Audio Bitrate
Full Script
Here is a full
batch script
that will convertmp4
towebm
and calculate and match the bitrates automatically usingvideo birate = (((size * 8) / 1000) / duration) * 1000
.