Python-Zugriff auf die Daten im JSON-Objekt

damit ich das in mein Skript:

import json
info = json.loads(get_info())
print info

Welche Ausgänge:

richard@richard-desktop:~/projects/hello-python$ python main.py 
{
    "streams": [
        {
            "index": 0,
            "codec_name": "mpeg2video",
            "codec_long_name": "MPEG-2 video",
            "codec_type": "video",
            "codec_time_base": "1001/48000",
            "codec_tag_string": "[0][0][0][0]",
            "codec_tag": "0x0000",
            "width": 1920,
            "height": 1080,
            "has_b_frames": 1,
            "sample_aspect_ratio": "1:1",
            "display_aspect_ratio": "16:9",
            "pix_fmt": "yuv422p",
            "level": 2,
            "timecode": "00:59:59:00",
            "id": "0x1e0",
            "r_frame_rate": "24000/1001",
            "avg_frame_rate": "10000/417",
            "time_base": "1/90000",
            "start_time": "0.945411"
        },
        {
            "index": 1,
            "codec_name": "pcm_dvd",
            "codec_long_name": "PCM signed 20|24-bit big-endian",
            "codec_type": "audio",
            "codec_time_base": "1/48000",
            "codec_tag_string": "[0][0][0][0]",
            "codec_tag": "0x0000",
            "sample_fmt": "s32",
            "sample_rate": "48000",
            "channels": 2,
            "bits_per_sample": 0,
            "id": "0xa0",
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/90000",
            "start_time": "0.945411",
            "duration": "600.595000"
        }
    ],
    "format": {
        "filename": "/home/richard/projects/hello-python/tests/test_1.mpg",
        "nb_streams": 2,
        "format_name": "mpeg",
        "format_long_name": "MPEG-PS format",
        "start_time": "0.945411",
        "duration": "600.595000",
        "size": "4033241092",
        "bit_rate": "53723272"
    }
}

Wie kann ich streams oder formatieren und dann Eigenschaften in Ihnen? Zum Beispiel, wie man codec_long_name aus der zweiten stream oder wie man die Dauer von format?

Habe ich versucht:

print info[0]

Welche Ausgänge:

richard@richard-desktop:~/projects/hello-python$ python main.py 
{

Wenn ich print repr(info), erhalte ich:

richard@richard-desktop:~/projects/hello-python$ python main.py 
'{\n    "streams": [\n        {\n            "index": 0,\n            "codec_name": "mpeg2video",\n            "codec_long_name": "MPEG-2 video",\n            "codec_type": "video",\n            "codec_time_base": "1001/48000",\n            "codec_tag_string": "[0][0][0][0]",\n            "codec_tag": "0x0000",\n            "width": 1920,\n            "height": 1080,\n            "has_b_frames": 1,\n            "sample_aspect_ratio": "1:1",\n            "display_aspect_ratio": "16:9",\n            "pix_fmt": "yuv422p",\n            "level": 2,\n            "timecode": "00:59:59:00",\n            "id": "0x1e0",\n            "r_frame_rate": "24000/1001",\n            "avg_frame_rate": "10000/417",\n            "time_base": "1/90000",\n            "start_time": "0.945411"\n        },\n        {\n            "index": 1,\n            "codec_name": "pcm_dvd",\n            "codec_long_name": "PCM signed 20|24-bit big-endian",\n            "codec_type": "audio",\n            "codec_time_base": "1/48000",\n            "codec_tag_string": "[0][0][0][0]",\n            "codec_tag": "0x0000",\n            "sample_fmt": "s32",\n            "sample_rate": "48000",\n            "channels": 2,\n            "bits_per_sample": 0,\n            "id": "0xa0",\n            "r_frame_rate": "0/0",\n            "avg_frame_rate": "0/0",\n            "time_base": "1/90000",\n            "start_time": "0.945411",\n            "duration": "600.595000"\n        }\n    ],\n    "format": {\n        "filename": "/home/richard/projects/hello-python/tests/test_1.mpg",\n        "nb_streams": 2,\n        "format_name": "mpeg",\n        "format_long_name": "MPEG-PS format",\n        "start_time": "0.945411",\n        "duration": "600.595000",\n        "size": "4033241092",\n        "bit_rate": "53723272"\n    }\n}\n'

Dem Weg ich bin immer der JSON-string ist bu dieser Befehl ausgeführt wird:

ffmpeg_command = "ffprobe -v quiet -print_format json -show_format -show_streams %s" % self.absolute_path
return subprocess.check_output(ffmpeg_command, shell=True)
InformationsquelleAutor Richard Knop | 2012-06-28
Schreibe einen Kommentar