L0Lock / FFmpeg-bat-collection

A collection of .bat files for some usefull ffmpeg conversions.
https://l0lock.github.io/FFmpeg-bat-collection/
GNU General Public License v3.0
37 stars 5 forks source link

ffmpeg IF condition #4

Open geextahslex opened 8 months ago

geextahslex commented 8 months ago

Hi I have a .bat file to convert and change channel volume (with ffmpeg) from 5.1 Audio to Stereo, and a different .bat for Stereo/Mono Audio that doesn't change loudness. Now I have to manually look if a movie is 5.1 or Mono/Stereo and decide which .bat to run. I would like to make one .bat with a condition so it looks what the channel layout is and then decide which profile to run. I hope this makes sense. It has to check also the language tag, because sometimes (in my case) german is 2.0 but English is 5.1 or vice versa.

Thank you for any advice

5.1

ffmpeg ^
    -i "%~1" ^
    -af "pan=stereo|c0=c2+0.6*c0+0.6*c4+c3|c1=c2+0.6*c1+0.6*c5+c3" -map 0:v:0 -c:v copy -map 0:a:m:language:ger -codec:a ac3 -b:a 160k -ar 44100 -sn -dn ^
    "G:\%~n1.mkv"

2.0/Mono

ffmpeg ^
    -i "%~1" ^
    -map 0:v:0 -c:v copy -map 0:a:m:language:ger -codec:a ac3 -b:a 160k -ar 44100 -sn -dn ^
    "G:\%~n1.mkv"

This is the whole .bat

@echo off
:again

ffmpeg ^
    -i "%~1" ^
    -map 0:v:0 -c:v copy -map 0:a:m:language:ger -codec:a ac3 -b:a 160k -ar 44100 -sn -dn ^
    "G:\%~n1.mkv"
if NOT ["%errorlevel%"]==["0"] goto:error
echo %~n1 Done!

shift
if "%~1" == "" goto:end
goto:again

:error

echo There was an error. Please check your input file or report an issue on github.com/L0Lock/FFmpeg-bat-collection/issues.
pause
exit 0

:end

cls
echo Encoding succesful. This window will close after 10 seconds.
timeout /t 1
L0Lock commented 8 months ago

Hi,

I am no expert, and I don't have access to a Windows machine for the time being, I can not test a full bat script easily... But I will do my best. I don't think you can make ffmpeg tell whether you have a stereo or a 5.1 audio track, but you can ask ffprobe (which should be shipped alongside the ffmpeg executable) for the amount of audio channels in a track (which i think is 6 for a 5.1 versus 2 for stereo).

For example:

ffprobe -i "inputFile.ext" -show_entries stream=channels -select_streams a:0 -of compact=p=0:nk=1 -v 0

I guess you could format your script like this:

for /f %%a in ('ffprobe -i %~1 -show_entries stream=channels -select_streams a:0 -of compact=p=0:nk=1 -v 0') do set channels=%%a

if %channels% LEQ 2 (
    REM Run ffmpeg job for <= 2 channels
) else (
    REM Run ffmpeg job for > 2 channels
)

Basically, store ffprobe's query in a variable. And run the appropriate ffmpeg command depending on whether ffprobe found a value <=2.

You might need to use trickeries to correctly parse the ffprobe command in the for loop. I can't test it now, but you can check the "alternative to escaping" part in this post answer.

Let me know how it goes.

geextahslex commented 8 months ago

Okay thank you. I think I would need something like ffprobe -i %~1 -v error -select_streams a -of csv=p=0 -show_entries stream=channels,index:stream_tags=language to check all audio tracks and language tags, to distinct between them ffprobe -i spider.mkv -show_entries stream=channels,index:stream_tags=language -select_streams a -of compact=p=0:nk=1 -v 0 output of the first one:

1,6,ger
2,6,eng

output of the 2nd:

1|6|ger
2|6|eng

I don't know if this makes any difference