Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ffmpeg IF condition #4

Open
geextahslex opened this issue Mar 12, 2024 · 2 comments
Open

ffmpeg IF condition #4

geextahslex opened this issue Mar 12, 2024 · 2 comments

Comments

@geextahslex
Copy link

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 �[92m%~n1 Done!�[0m

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

:error
�
echo �[93mThere was an error. Please check your input file or report an issue on github.com/L0Lock/FFmpeg-bat-collection/issues.�[0m
pause
exit 0

:end
�
cls
echo �[92mEncoding succesful. This window will close after 10 seconds.�[0m
timeout /t 1

@L0Lock
Copy link
Owner

L0Lock commented Mar 14, 2024

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 "inputFile.ext": input file to query about
  • -show_entries stream=channels to tell which "entries" we want to query about the input file, here we use channels
  • -select_streams a:0 to indicate a specific track to query in the input file, here the first audio track.
  • -of compact=p=0:nk=1 sets the output formatting to show only the query value. I.E. without it, you would get [STREAM]channels=2[/STREAM] instead of just 2 for a stereo channel.
  • -v 0 disables ffprobe's verbosity aside from fatal errors (so that you don't get the usual ton of informational text from ffprobe).

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
Copy link
Author

geextahslex commented Mar 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants