Search code examples
videoffmpegmkv

What's the most elegant way to get the language codes from an MKV file with multiple audio tracks?


I need a way to get the language codes from MKV files which have multiple audio tracks.

ffmpeg produces output which I could then filter using regular expressions but it doesn't seem very elegant:

$ ffmpeg -i file.mkv 2>&1 | grep Audio
Stream #0:1(eng): Audio: mp2, 48000 Hz, stereo, s16, 192 kb/s (default)
Stream #0:2(nar): Audio: mp2, 48000 Hz, mono, s16, 64 kb/s (default)

mediainfo has the ability to extract language information but in files with multiple tracks, it concatenates the codes into a single string:

$ mediainfo file.mkv --inform="Audio;%Language%"  
ennar

Is there a tool or command which will return language codes for multiple tracks in a nicer way, or a tool which would let me specify a track number and return the language code for that track only?


Solution

  • Running this ffprobe command

    ffprobe in.mp4 -show_entries stream=index:stream_tags=language -select_streams a -v 0 -of compact=p=0:nk=1
    

    will produce this output

    1|eng
    2|deu
    3|eng
    4|eng
    5|fre
    

    Where the first field is the absolute stream index i.e. counting all types V/A/S and is 0-based.

    ffprobe in.mp4 -show_entries stream=index:stream_tags=language -select_streams a:2 -v 0 -of compact=p=0:nk=1
    

    will show only the third audio stream.

    ffprobe in.mp4 -show_entries stream=index:stream_tags=language -select_streams 2 -v 0 -of compact=p=0:nk=1
    

    will show only the third stream, whichever type it is.