I want to extract the 5.1 audio from mkv/avi/mp4 for reference, how to do that?
Here's a way to obtain this result using ffprobe and ffmpeg.
First, let's see what audio streams are available in a given file :
$ ffprobe my_input_file.m4v
What we are interested in this part of the output :
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m/smpte170m/bt709), 718x552 [SAR 64:45 DAR 5744:3105], 1023 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc (default) Metadata: creation_time : 2013-01-26 10:39:52 encoder : JVT/AVC Coding Stream #0:1(fra): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 384 kb/s (default) Metadata: creation_time : 2013-01-26 10:39:52 Side data: audio service type: main Stream #0:2(eng): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 384 kb/s Metadata: creation_time : 2013-01-26 10:39:52 Side data: audio service type: main
It tells us that this container file includes two audio streams, identified as
#0:2, both are ac3 bitstreams and are 5.1 soundtracks.
If we only want to extract the ac3 bitstream of the 'en' soundtrack (#0:2) to a file :
$ ffmpeg -i my_input_file.m4v -map 0:2 -c:a copy output_file.ac3
-map option is here to specify which stream of the input file we want to get in our output file.
Now, maybe what you really want is to get a PCM wav 6 channels file of this soundtrack. Let's try this :
$ ffmpeg -i my_input_file.m4v -map 0:2 output_file.wav
This will generate a 6 channels 16 bit PCM wav file. But depending on the length of the original movie, that might lead you to a wav file that exceeds the 2 GB size limit for wav file ... (Notice that some systems and players will support up to 4 GB).
In this case, an alternative might be to output the soundtrack as 6 mono PCM wav files. First extract the soundtrack as ac3 file, as shown previously, then :
ffmpeg -i my_input_file.ac3 -map_channel 0.0.0 l.wav -map_channel 0.0.1 r.wav -map_channel 0.0.2 c.wav -map_channel 0.0.3 ls.wav -map_channel 0.0.4 rs.wav -map_channel 0.0.5 lfe.wav
(Note that channel mapping, ie which is front, rear left and so on might depend on the original bitstream channel mapping).
I have unfortunately not found a way to do this directly from the ac3 stream of an audio+video container.
Take a look at the free and open source TAudioConverter. It supports ripping/extracting audio from many different files.
According to the changelog it should support multichannel export as long as you don't choose mp3 and AAC-HEv2 as target.
Features (from website):
TAudioConverter support various encoders. It can convert to MP3, AAC, AAC HE, AC3, MusePack, Ogg Vorbis, Opus, WMA, DTS, ALAC, FLAC, Monkey's Audio, TAK, TTA, WavPack, AIFF and PCM.
TAudioConverter is multithreaded. This means that it will run several encoders in parallel to shorten encoding time. The number of parallel processes depends on the CPU cores you have.
It can also extract audio streams from video files. This can be done with or without encoding the audio stream. Even if your video file has more that one audio stream, you can select the one you want.
CD ripper has freedb support.
Audio Merger. Merge several audio files into one.
It can copy tags and cover art from source.
TAC can split cue sheets.
TAudioConverter can apply certain effects to audio. It can change audio volume level and playback speed.
TAudioConverter is free open source software.