'FFmpeg IF/ELSE statement
I am doing reencoding of lots of video files, and sometimes some of them have subtitles included, some dont. Is it possible to "ignore"
-vf "subtitles=%%f:force_style='MarginV=25,Fontsize=20'" -sn
statement in FFmpeg command IF input file does not have subtitles included?
My command is:
for %%f in (*.mkv) do ffmpeg -hwaccel nvdec -i "%%f" -strict -2 -max_muxing_queue_size 1024 -map_metadata -1 -map_chapters -1 -vf "subtitles=%%f:force_style='MarginV=25,Fontsize=20'" -sn -c:v hevc_nvenc -rc vbr -cq 24 -qmin 24 -qmax 24 -tier high -level 4 -preset medium -profile:v main10 -pix_fmt p010le -b:v 0K -ac 6 -ar 48000 -b:a:0 640k -c:a:0 ac3 -metadata:s:a:0 title="Dolby Digital" E:\Encoded\%%f
Note that there are more than 100 files in a batch and checking them by hand is time consuming.
Maybe right aproach is to first inspect file in a batch script and if that file has subtitles, then execute first command, if it does not have subtitles, execute second command. But, I dont know how to do a script like that.
Solution 1:[1]
After being "forced" to research and learn how to do this, I have found a solution, so here it is if someone in the future might need something like this:
SET subs=ffmpeg -hwaccel nvdec -i "%%f" -strict -2 -max_muxing_queue_size 1024 -map_metadata -1 -map_chapters -1 -vf "subtitles=%%f:force_style='MarginV=25,Fontsize=20'" -sn -c:v hevc_nvenc -rc vbr -cq 24 -qmin 24 -qmax 24 -tier high -level 4 -preset medium -profile:v main10 -pix_fmt p010le -b:v 0K -ac 6 -ar 48000 -b:a:0 640k -c:a:0 ac3 -metadata:s:a:0 title="Dolby Digital" E:\Encoded\%%f
SET nosubs=ffmpeg -hwaccel nvdec -i "%%f" -strict -2 -max_muxing_queue_size 1024 -map_metadata -1 -map_chapters -1 -sn -c:v hevc_nvenc -rc vbr -cq 24 -qmin 24 -qmax 24 -tier high -level 4 -preset medium -profile:v main10 -pix_fmt p010le -b:v 0K -ac 6 -ar 48000 -b:a:0 640k -c:a:0 ac3 -metadata:s:a:0 title="Dolby Digital" E:\Encoded\%%f
for %%f in (*.mkv) do ffmpeg -i "%%f" -c copy -map 0:s -f null - -v 0 -hide_banner && %subs% || %nosubs%
Script basicaly checks file if there are subtitles, and depending on output, it executes proper command. I think it can be used for other use scenarios also.
Note: It is for Windows batch, it would not work like this under Linux...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Hrvoje |