Read in JSON code with Rust and retrieve it in a targeted manner

How can I store a JSON output from a program in an array or a tuple and then store any element from it into a variable?

This call outputs all important information about the video in JSON format:

ffprobe -show_streams -print_format json -i video.mp4

like these:

{
    "streams": [
        {
            "index": 0,
            "codec_name": "h264",
            "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
            "profile": "Main",
            "codec_type": "video",
            "codec_time_base": "1/50",
            "codec_tag_string": "avc1",
            "codec_tag": "0x31637661",
            "width": 1280,
            "height": 720,
            "coded_width": 1280,
            "coded_height": 720,
            "closed_captions": 0,
            "has_b_frames": 1,
            "sample_aspect_ratio": "1:1",
            "display_aspect_ratio": "16:9",
            "pix_fmt": "yuv420p",
            "level": 31,
            "color_range": "tv",
            "color_space": "bt709",
            "color_transfer": "bt709",
            "color_primaries": "bt709",
            "chroma_location": "left",
            "refs": 1,
            "is_avc": "true",
            "nal_length_size": "4",
            "r_frame_rate": "25/1",
            "avg_frame_rate": "25/1",
            "time_base": "1/12800",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 32819712,
            "duration": "2564.040000",
            "bit_rate": "1000918",
            "bits_per_raw_sample": "8",
            "nb_frames": "64101",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "creation_time": "2019-01-14T22:46:02.000000Z",
                "language": "und",
                "handler_name": "ISO Media file produced by Google Inc. Created on: 01/14/2019."
            }
        },
        {
            "index": 1,
            "codec_name": "aac",
            "codec_long_name": "AAC (Advanced Audio Coding)",
            "profile": "LC",
            "codec_type": "audio",
            "codec_time_base": "1/44100",
            "codec_tag_string": "mp4a",
            "codec_tag": "0x6134706d",
            "sample_fmt": "fltp",
            "sample_rate": "44100",
            "channels": 2,
            "channel_layout": "stereo",
            "bits_per_sample": 0,
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/44100",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 113074176,
            "duration": "2564.040272",
            "bit_rate": "127999",
            "nb_frames": "110424",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "creation_time": "2019-01-14T22:46:02.000000Z",
                "language": "und",
                "handler_name": "ISO Media file produced by Google Inc. Created on: 01/14/2019."
            }
        }
    ]
}

You will probably want to use serde together with serde_json for this.

1 Like

Something like this.

yes, maybe - I can't test that, it doesn't work in PlayGound and it doesn't work for me either

mmh, I'm new to Rust, I first have to read through how I can deal with it

You could also use the ffprobe crate which uses serde and defines the types you need. Looks like it implements calling ffprobe as well.

1 Like

That's interesting! I'll have a look at that.