0 Transforming mkvmerge's JSON output
Moritz Bunkus edited this page 2018-03-05 19:17:55 +00:00

With the removal of mkvmerge's verbose identification output, processing data with line-based tools such as grep or awk isn't as straight-forward as it was before. You can easily process its JSON output with any language, of course, as JSON parsers are available for pretty much all of them.

For those that want to keep working on the command line, the jq utility offers a suitable replacement for those utilities. Here are a couple of tips & examples of what you can do by processing mkvmerge's JSON output with it. You can find out more about how its syntax in its manual.

I'm splitting up the arguments to the jq command into multiple lines for improved legibility. You should join them into a single line before executing them.

Outputting one line per track including the its ID, language and codec

mkvmerge -J v.mkv | jq -r '
  .tracks |
  map((.id | tostring) + " " + .properties.language + " " + .codec) |
  join("\n")
'

What it does:

  • iterate over all tracks and create a new array:
  • for each create a line consisting of the ID, the language and the codec
  • join all lines in the array by new lines

Only list track IDs of MP3 audio tracks whose language is eng

mkvmerge -J file.mkv | jq -r '
  .tracks |
  map(
    select(.type == "audio") |
    select(.properties.language == "eng") |
    select(.codec == "MP3") |
    .id
  ) |
  map(tostring) |
  join(",")'

What it does:

  • iterate over all tracks and create a new array:
    • for each: only keep those of type audio,
    • only keep those whose language is eng,
    • only keep those whose codec is MP3,
    • keep its ID
  • convert all entries in the array (which are numbers) to a string,
  • join all lines in the array by new lines

Categories: metadata