2017-03-03 4 views
0

私はJSONファイルを探して、インデックス1のストリームが存在するかどうかを確認するスクリプトを作成しようとしています。今私のプログラムはそれをしません。ここにはプログラム自体があります。 (data["streams"][1])が存在するかどうかをチェックし、コーデック、サンプルレート、使用可能なすべてのオーディオストリームのビットレートを印刷する場合に備えてチェックします。JSON Pythonのデータチェック

#!/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", 

答えて

0

あなたは、単に使用することができるはずです。

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

あなたは、特定のインデックスがリスト内に存在するかどうかをチェックするためにlen機能を使用することができます。以下のような何か:

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

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 
0

あなたは、単にtheresの1以下のファイルならば

if data["streams"][1]: 
0

安全

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(sあなたはその「インデックス」を知っていれば、インデックス1は、トライキャッチ

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) 

を緩んで存在する場合、ビットレート、サンプルレートとコーデックが常に存在することが確実な場合EEMSはそう)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 

を失います常にデータ[「ストリーム」]は

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) 

場合は、今では、インデックス1はインデックス0が発生した場合にのみ発生する場合は、常にDの位置1で発生するの要素は、最初の条件が失われている場合に存在しますata ["streams"] array

try: 
    if data["streams"][1]["index"]==1: 
     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) 
except: 
    pass