私は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",