ffmpeg: How to determine output extension automatically (-c:a copy)

There's a difference between containers and the encoding. m4v is a container, as is WAV, WMA, WMV, AAC, etc. They all support multiple encodings. But, there are some general patterns. ffprobe can help.

Extracting audio from video files using ffmpeg is covered very thoroughly here: https://gist.github.com/protrolium/e0dbd4bb0f1a396fcb55

In that, there is an example of how you could do what you are seeking, in some cases, using ffprobe and sed:

for file in *mp4 *avi; do ffmpeg -i "$file" -vn -acodec copy "$file".`ffprobe "$file" 2>&1 |sed -rn 's/.Audio: (...), ./\1/p'`; done

In the linked page, the above appeared to be corrupted by html encoding. I've attempted to fix it. It could likely be simplified for a single file to:

ffmpeg -i "myfile.m4v" -vn -acodec copy "myfile".`ffprobe "myfile.m4v" 2>&1 |sed -rn 's/.Audio: (...), ./\1/p'`

But, if you aren't using sed and a bash shell, then this won't work. (ie. won't work on windows). It also won't work if the encoding in the video file doesn't map commonly to a file extension. In windows, you could probably come up with a powershell or vbscript that would do the same thing.

Tags:

Ffmpeg