FFMPEG How to extract a variable number of audio streams?

2

I'm trying to make a small batch file that extracts all audio streams when dragging a video onto it.

ffmpeg.exe -i "%1" -map 0:a? -c:a copy "%1~extracted.aac"

But this seems to be overwriting the same file for each audio stream it finds.

I don't want to specify the number of audio tracks, since it may vary.

Thanks in advance for your help!

tugalaw

Posted 2018-07-25T19:29:55.917

Reputation: 33

Answers

1

There's no direct way to do this.

There is a somewhat tedious workaround that relies on the tee muxer and a guess of the maximum number of audio streams that you may encounter. Let's say that's 4.

So, the command is

ffmpeg -i in.mp4 -c copy -map 0:a?
       -f tee
              "[select='a\:0':onfail=ignore]in-0.aac|[select='a\:1':onfail=ignore]in-1.aac|
               [select='a\:2':onfail=ignore]in-2.aac|[select='a\:3':onfail=ignore]in-3.aac"

All four files will be created but if a size is zero, then the stream didn't exist. So, once you delete all zero-sized outputs, you'll have all extracted streams. FFmpeg doesn't automatically select output format, so streams have to be AAC. Use a recent ffmpeg version, 4.0+

Gyan

Posted 2018-07-25T19:29:55.917

Reputation: 21 016

Deleted the previous comments because I wasn't using the right version of ffmpeg. Your solution works, thank you! – tugalaw – 2018-07-25T23:23:20.843