Extract any audio from container with ffmpeg
Solution 1:
The reason this isn't a simple option in ffmpeg is because you need a way to decide what container to use for the output.
If you determined which file extension to use for each codec, and named the file with that extension, ffmpeg would write the output file in the right container for you. The (unfortunately) simplest way to do this that I can think of is a Python 2 script:
import re
import subprocess
import sys
EXTENSIONS_BY_CODEC = {'mp3': '.mp3', 'aac': '.m4a'}
# Add more codecs as desired
_, infile, outfile = sys.argv
_, detect_data = subprocess.Popen(('ffmpeg', '-i', infile),
stderr=subprocess.PIPE).communicate()
codec = re.search(r'Audio: ([^,]+),', detect_data).group(1)
if codec in EXTENSIONS_BY_CODEC:
sys.exit(subprocess.call(('ffmpeg', '-i', infile, '-acodec', 'copy', '-vn',
outfile + EXTENSIONS_BY_CODEC[codec])))
else:
print >>sys.stderr, 'Extension not found for codec:', codec
sys.exit(2)