Skip to content

Using JSON

Ahoy jq

Let's parse some json with jq. First we're going to need some json to work with. Let's use ffprobe to dump some from a video file

Example

ffprobe -v quiet -print_format json -show_streams "${video}" >"${video}.json"

Now let's take a look at the json, we can either use the file we created or pipe it directly into jq

Example

ffprobe -v quiet -print_format json -show_streams "${video}" | jq
# OR
jq < "${video}"

Let's parse the data in a few ways, ending with only selecting English subtitles. These methods can be mixed and matched as needed to get the information you require from json

Example

# Get information on the first stream in the video file
jq -r '.streams[0]' <${video}
# Get all streams
jq -r '.streams[]' <${video}
# Return only the index number of the streams
jq -r '.streams[] | .index' <${video}
# Return the index, codec name, and codec type of all video streams.
jq -r '.streams[] | {index, codec_name, codec_type' <${video}
# Return the index and codec name of only the subtitle streams
jq -r '.streams[] | select(.codec_type=="subtitles") | {index, codec_name}' <${video}
# Return only the subtitle streams that have a language tag
jq -r '.streams[] | select(.codec_type=="subtitles") | select(.tags.language)' <${video}
# Return only the subtitle streams that contain "en" in their language tag
jq -r '.streams[] | select(.codec_type=="subtitles") | select(.tags.language | contains("en"))' <${video}