处理来自 mediainfo 输出的 JSON 数据

Process JSON data from mediainfo output

我是 运行 来自子进程的“MediaInfo”命令:

cmd = ["mediainfo", "--output=JSON", file_loc]

我得到的输出是:

{
"media": {
"@ref": "/home/mediaworker/divergent.jpg",
"track": [
{
"@type": "General",
"ImageCount": "1",
"FileExtension": "jpg",
"Format": "JPEG",
"FileSize": "84227",
"StreamSize": "0",
"File_Modified_Date": "UTC 2019-07-16 05:36:32",
"File_Modified_Date_Local": "2019-07-16 11:06:32"
},
{
"@type": "Image",
"Format": "JPEG",
"Width": "612",
"Height": "612",
"ColorSpace": "YUV",
"ChromaSubsampling": "4:4:4",
"BitDepth": "8",
"Compression_Mode": "Lossy",
"StreamSize": "84227"
}
]
}
}

我正在尝试以不同的方式重新格式化此数据,如下所示: 基本上维护一个字典列表。

[
{
"desc":"ImageCount",
"val" : "1"
},
{
"desc":"FileExtension",
"val" : "jpg"
},
{
"desc":"Format",
"val" : "JPEG"
}{
"desc":"FileSize",
"val" : "84227"
},
{
"desc":"StreamSize",
"val" : "0"
},
{
"desc":"File_Modified_Date",
"val" : "UTC 2019-07-16 05:36:32"
},
{
"desc":"File_Modified_Date_Local",
"val" : "2019-07-16 11:06:32"
},
{
"desc":"Width",
"val" : "612"
},
{
"desc":"Height",
"val" : "612"
},
{
"desc":"ColorSpace",
"val" : "YUV"
},
{
"desc":"ChromaSubsampling",
"val" : "4:4:4"
},
{
"desc":"BitDepth",
"val" : "8"
},
{
"desc":"Compression_Mode",
"val" : "Lossy"
},
{
"desc":"StreamSize",
"val" : "1"
}
]

不用for循环多次循环,有什么办法可以实现吗?我还需要删除那些 不需要的和重复的 键。

尝试:

dct = {
    "media": {
        "@ref": "/home/mediaworker/divergent.jpg",
        "track": [
            {
                "@type": "General",
                "ImageCount": "1",
                "FileExtension": "jpg",
                "Format": "JPEG",
                "FileSize": "84227",
                "StreamSize": "0",
                "File_Modified_Date": "UTC 2019-07-16 05:36:32",
                "File_Modified_Date_Local": "2019-07-16 11:06:32",
            },
            {
                "@type": "Image",
                "Format": "JPEG",
                "Width": "612",
                "Height": "612",
                "ColorSpace": "YUV",
                "ChromaSubsampling": "4:4:4",
                "BitDepth": "8",
                "Compression_Mode": "Lossy",
                "StreamSize": "84227",
            },
        ],
    }
}

out = []
for d in dct["media"]["track"]:
    for k, v in d.items():
        if not k.startswith("@"):
            out.append({"desc": k, "val": v})

print(out)

打印:

[
    {"desc": "ImageCount", "val": "1"},
    {"desc": "FileExtension", "val": "jpg"},
    {"desc": "Format", "val": "JPEG"},
    {"desc": "FileSize", "val": "84227"},
    {"desc": "StreamSize", "val": "0"},
    {"desc": "File_Modified_Date", "val": "UTC 2019-07-16 05:36:32"},
    {"desc": "File_Modified_Date_Local", "val": "2019-07-16 11:06:32"},
    {"desc": "Format", "val": "JPEG"},
    {"desc": "Width", "val": "612"},
    {"desc": "Height", "val": "612"},
    {"desc": "ColorSpace", "val": "YUV"},
    {"desc": "ChromaSubsampling", "val": "4:4:4"},
    {"desc": "BitDepth", "val": "8"},
    {"desc": "Compression_Mode", "val": "Lossy"},
    {"desc": "StreamSize", "val": "84227"},
]