Author’s note: This is a very simple automation technique for FFmpeg beginners. I’m sure there are much more efficient ways to script this project, but this represents my baby steps in FFmpeg automation.
I recently started a consulting project that involved encoding multiple files to multiple CRF values to create rate-distortion curves and BD-Rate computations. I’m testing three codecs with sixteen different test files, encoding each codec in a separate folder to avoid any kind of FFmpeg or other operating conflicts. So, this means three batch files for each file (one for each codec) or 48 different batch files. Not too onerous because all that changes is the file name, but I was wondering if there was a better way.
Here’s where I started; the first line creates the file, the second measures VMAF and PSNR with the Moscow State University Video Quality Measurement Tool.
ffmpeg -y -i GTAV.mp4 -c:v libx264 -crf 25 GTAV_x264_CRF25.mp4
"C:\Program Files\MSU_VQMT_12\msu_metric.exe" -in GTAV.mp4 -in GTAV_x264_CRF25.mp4 -csv -metr vmaf over Y -metr psnr over Y
Then I repeated the sequence for four different CRF values.
Adding the Wildcard
The client had sent a batch file that applied some wildcard automations. I couldn’t run it as is but adopted (stole) the operational schema which substituted wild cards for the static file name.
Here’s the new batch language that substitutes %1 for the file name in the source and output file. I saved the batch file as
ffmpeg -y -i %1.mp4 -c:v libx264 -crf 25 %1_x264_CRF25.mp4
"C:\Program Files\MSU_VQMT_12\msu_metric.exe" -in %1.mp4 -in %1_x264_CRF25.mp4 -csv -metr vmaf over Y -metr psnr over Y
To run the batch and encode the file, you type in the batch and file name in the command window. Or, to encode the GTAV test file you would run this.
Encoding Multiple Files
The obvious next question is how to run this command for all test files? The answer? Create another batch file in the same folder that sends out the batch file name:test filename commands in sequence. To make it work on Windows, you have to add
call before the commands, like so (for GTAV and TOS).
call ffmpeg_x264.bat GTAV
call ffmpeg_x264.bat TOS
Getting Fancy with Folders
Don’t want all the files saved in the same folder? You can add wildcard-driven directories to your commands as well. Add the top line to the batch file (ffmpeg_x264.bat in my case) to create the directory and the bolded sections to each of the command lines.
ffmpeg -y -i %1.mp4 -c:v libx264 -crf 25 %1\%1_x264_CRF25.mp4
"C:\Program Files\MSU_VQMT_12\msu_metric.exe" -in %1.mp4 -in %1\%1_x264_CRF25.mp4 -csv -metr vmaf over Y -metr psnr over Y -csv-dir %1
Again, you create the folder with the file name using the
md %1 command.
You store the file to that folder using the bolded command in the second line.
With the VQMT tool, you save the results file in the new directory via the bolded script in the third line.
These FFmpeg to the Rescue articles will appear in future additions of my book Learn to Produce FFmpeg in 30 Minutes or Less, now on the 2018 Edition. The book helps beginning and intermediate FFmpeg users produce high-quality, bandwidth-efficient files and encoding ladders as efficiently as possible. For those who prefer learning via video, check out this course.