JSON Python检查数据

时间:2017-03-03 11:36:43

标签: python json ffmpeg ffprobe

所以我正在尝试编写一个查看JSON文件的脚本,并检查索引为1的流是否存在。现在我的程序没有这样做。这是程序本身。我想检查(data["streams"][1])是否存在以及是否存在打印编解码器,采样率和所有可用音频流的比特率。

#!/usr/bin/env python
import subprocess
import json
import os.path

# Saving the file path and the name path into input_file

input_file = raw_input("Please enter the input file path: ")

# Loop until the user enters a valid input file

while os.path.isfile(input_file) == False:
        print "Please try again, the specified file / path don't exist!"
        input_file = raw_input("Please enter the input file path again: ")

# Execution of the ffprobe command to list the general statistics of the file. I have separated both scripts because the script for analyzing the frames is taking longer time.

returned_data = subprocess.check_output(['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', input_file])

# Loading of the json file

data = json.loads(returned_data.decode('utf-8'))
t = (data["streams"][0]["avg_frame_rate"])
fps = [float(x) for x in t.split('/')]

# Printing of the general information about the video file

print "========================== Video ============================="
print
print "Codec: %s" %(data["streams"][0]["codec_long_name"])
print "Profile: %s" %(data["streams"][0]["profile"])
print "Resolution: %d x %d" %((data["streams"][0]["width"]), (data["streams"][0]["height"]))
print "Pixel Format: %s" %(data["streams"][0]["pix_fmt"])
print "Bits per sample: %s" %(data["streams"][0]["bits_per_raw_sample"])
print
print "========================== Audio ============================="
print
print "Codec: %s" %(data["streams"][1]["codec_name"])
print "Sample Rate: %.3f KHz" %(int(data["streams"][1]["sample_rate"])/1000)
print "Bitrate: %d Kbps" %(int(data["streams"][1]["bit_rate"])/1000)

这是JSON文件的输出。

{
    "streams": [
        {
            "index": 0,
            "codec_name": "mpeg4",
            "codec_long_name": "MPEG-4 part 2",
            "profile": "Simple Profile",
            "codec_type": "video",
            "codec_time_base": "1/24",
            "codec_tag_string": "FMP4",
            "codec_tag": "0x34504d46",
            "width": 854,
            "height": 480,
            "coded_width": 854,
            "coded_height": 480,
            "has_b_frames": 0,
            "sample_aspect_ratio": "1:1",
            "display_aspect_ratio": "427:240",
            "pix_fmt": "yuv420p",
            "level": 1,
            "chroma_location": "left",
            "refs": 1,
            "quarter_sample": "false",
            "divx_packed": "false",
            "r_frame_rate": "24/1",
            "avg_frame_rate": "24/1",
            "time_base": "1/24",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 14315,
            "duration": "596.458333",
            "bit_rate": "2500431",
            "nb_frames": "14315",
            "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
            }
        },
        {
            "index": 1,
            "codec_name": "ac3",
            "codec_long_name": "ATSC A/52A (AC-3)",
            "codec_type": "audio",
            "codec_time_base": "1/48000",
            "codec_tag_string": "[0] [0][0]",
            "codec_tag": "0x2000",
            "sample_fmt": "fltp",
            "sample_rate": "48000",
            "channels": 6,
            "channel_layout": "5.1(side)",
            "bits_per_sample": 0,
            "dmix_mode": "-1",

5 个答案:

答案 0 :(得分:0)

你应该可以简单地使用:

if data["streams"][1]:
    print(..........)

答案 1 :(得分:0)

您可以使用len函数检查列表中是否存在特定索引。类似的东西:

if len(data["streams"]) > 1: print data["streams"][1]

答案 2 :(得分:0)

我已经解决了我的问题:)

for i in range(1, len(data["streams"])):
        if (data["streams"][i]["codec_type"]) == "audio":
                print
                print "Audio Channel Number: %d" %(i)
                print "Codec: %s" %(data["streams"][i]["codec_name"])
                print "Sample Rate: %.3f KHz" %(int(data["streams"][i]["sample_rate"])/1000)
                print "Bitrate: %d Kbps" %(int(data["streams"][i]["bit_rate"])/1000)
                print
        else:
                print
                print "No valid audio streams!"
                print

答案 3 :(得分:0)

你可以简单地使用

if data["streams"][1]:

答案 4 :(得分:0)

安全

<table> <tr> <th style="background-color: #007CE2"> Test </th> <th style="background-color: #E54040"> Test </th> </tr> <tr> <td style="background-color: #007CE2"> <p id="t1_list">test<br>another<br>testing</p> <input type="text" name="t1_input" id="t1_input"> <button> Add </button> </td> <td style="background-color: #E54040"> <p id="t2_list"></p> <div class="value_input2"> <input type="text" name="t2_input" id="t2_input"> <button> Add </button> </div> </td> </tr> <tr> <th style="background-color: #8BC34A"> Test </th> <th style="background-color: #FF9800"> Test </th> </tr> <tr> <td style="background-color: #8BC34A"> <p id="t3_list"></p> <input type="text" name="t3_input" id="t3_input"> <button> Add </button> </td> <td style="background-color: #FF9800"> <p id="t4_list"></p> <input type="text" name="t4_input" id="t4_input"> <button> Add </button> </td> </tr> </table> 如果超过1个索引为1的文件(似乎不太可能)松开break语句

for i in range(len(data["streams"])):
    if "index" in data["streams"][i].keys() and  data["streams"][i]["index"]==1:
        try:
            print "Codec: %s" %(data["streams"][i]["codec_name"])
            print "Sample Rate: %.3f KHz" %(int(data["streams"][i]["sample_rate"])/1000)
            print "Bitrate: %d Kbps" %(int(data["streams"][i]["bit_rate"])/1000)
        except:
            pass
        break

如果您确定如果索引1存在,那么比特率,采样率和编解码器将始终存在松散的try catch

for i in range(len(data["streams"])):
    if "index" in data["streams"][i].keys() and  data["streams"][i]["index"]==1:
        try:
            print "Codec: %s" %(data["streams"][i]["codec_name"])
            print "Sample Rate: %.3f KHz" %(int(data["streams"][i]["sample_rate"])/1000)
            print "Bitrate: %d Kbps" %(int(data["streams"][i]["bit_rate"])/1000)
        except:
            pass

如果你知道如果数据[“streams”]中的元素松散了第一个条件,那么“index”将永远存在

for i in range(len(data["streams"])):
    if "index" in data["streams"][i].keys() and  data["streams"][i]["index"]==1:
        print "Codec: %s" %(data["streams"][i]["codec_name"])
        print "Sample Rate: %.3f KHz" %(int(data["streams"][i]["sample_rate"])/1000)
        print "Bitrate: %d Kbps" %(int(data["streams"][i]["bit_rate"])/1000)

如果您现在只有索引0出现时才会出现索引1并且始终出现在数据[“streams”]数组中的位置1

for i in range(len(data["streams"])):
    if  data["streams"][i]["index"]==1:
        print "Codec: %s" %(data["streams"][i]["codec_name"])
        print "Sample Rate: %.3f KHz" %(int(data["streams"][i]["sample_rate"])/1000)
        print "Bitrate: %d Kbps" %(int(data["streams"][i]["bit_rate"])/1000)