Python程序,它从FFPROBE生成的JSON文件中读取和提取特定信息

时间:2017-02-03 11:29:47

标签: python json bash ffmpeg ffprobe

我想创建一个简单的Python脚本,它运行自定义的ffprobe脚本,然后从生成的JSON文件中提取一些特定信息。所以ffprobe命令是:

ffprobe -v quiet -print_format json -show_format -show_streams example.mp4 > output.json

此命令将视频特定信息提取到output.json文件中。然后我想读取文件并从中提取一些特定信息。例如,JSON文件具有以下格式:

{
    "streams": [
        {
            "index": 0,
            "codec_name": "h264",
            "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
            "profile": "High 4:2:2 Intra",
            "codec_type": "video",
            "codec_time_base": "1/100",
            "codec_tag_string": "[0][0][0][0]",
            "codec_tag": "0x0000",
            "width": 3840,
            "height": 2160,
            "coded_width": 3840,
            "coded_height": 2160,
            "has_b_frames": 0,
            "sample_aspect_ratio": "1:1",
            "display_aspect_ratio": "16:9",
            "pix_fmt": "yuv422p10le",
            "level": 52,
            "color_range": "tv",
            "color_space": "bt709",
            "color_transfer": "bt709",
            "color_primaries": "bt709",
            "chroma_location": "left",
            "field_order": "progressive",
            "refs": 1,
            "is_avc": "false",
            "nal_length_size": "0",
            "r_frame_rate": "50/1",
            "avg_frame_rate": "50/1",
            "time_base": "1/50",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 15000,
            "duration": "300.000000",
            "bits_per_raw_sample": "10",
            "disposition": {
                "default": 0,
                "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": {
                "file_package_umid": "0x060A2B340101010501010D0013EC94F152947134B6EC94F10052947134B6EC01",
                "file_package_name": "Source Package"
            }
        }
    ],
    "format": {
        "filename": "300sec.mxf",
        "nb_streams": 1,
        "nb_programs": 0,
        "format_name": "mxf",
        "format_long_name": "MXF (Material eXchange Format)",
        "start_time": "0.000000",
        "duration": "300.000000",
        "size": "16772788991",
        "bit_rate": "447274373",
        "probe_score": 100,
        "tags": {
            "uid": "adab4424-2f25-4dc7-92ff-29bd000c0000",
            "generation_uid": "adab4424-2f25-4dc7-92ff-29bd000c0001",
            "company_name": "FFmpeg",
            "product_name": "OP1a Muxer",
            "product_version": "57.66.101",
            "product_uid": "adab4424-2f25-4dc7-92ff-29bd000c0002",
            "modification_date": "0-01-02T00:00:00.000000Z",
            "material_package_umid": "0x060A2B340101010501010D0013EC94F152947134B6EC94F10052947134B6EC00",
            "timecode": "00:00:00:00"
        }
    }
}

如果Python脚本运行然后要求您输入输入文件的路径,那将是完美的。

不幸的是,我对Python的经验很少,但它预先安装在我的系统上,我想使用它。另一个选择是写一个bash脚本做同样的事情。任何建议将不胜感激。

到目前为止,我已经弄清楚如何打开JSON文件并使用以下方法从中提取数据:

import json

with open('output.json') as json_data:
    data = json.load(json_data)
    json_data.close()
    print(data["streams"][0]["codec_name"])
    print(data["streams"][0]["profile"])

1 个答案:

答案 0 :(得分:1)

试试这个:

import json
import sys

将文件名作为命令行参数传递

然后立即阅读整个json,只要文件ain太大就应该没问题

最后将json解析为dict

file_content = open(sys.argv[1], 'r').read()
data = json.loads(file_content) # is a dictionary
print(data["streams"][0]["codec_name"])
print(data["streams"][0]["profile"])

以:

运行
python script.py /path/to/output.json