0

So I am trying to write a script which is looking into a JSON file and check if stream with index 1 exists. Right now my program doesn't do that. Here is the program itself. I want to check if (data["streams"][1]) exists and in case it exists to print the codec, sample rate and the bitrate of all available audio streams.

#!/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)

And here is the output of the JSON file.

{
    "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",
Georgi Stoyanov
  • 594
  • 1
  • 9
  • 26

5 Answers5

0

You should be able to simply use:

if data["streams"][1]:
    print(..........)
Tim B
  • 3,033
  • 1
  • 23
  • 28
0

You can use the len function to check if a particular index exists in the list. Something like:

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

yeniv
  • 1,589
  • 11
  • 25
0

I have solved my problem :)

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
Georgi Stoyanov
  • 594
  • 1
  • 9
  • 26
0

you can simply use

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

safe

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 if theres more than 1 file with index 1 (seems unlikely) loose the break statement

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

if you are sure that bitrate ,sample rate and codec will always exist if index 1 exists loose the try catch

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)

if you know that "index" will always exist if data["streams"] has elements loose the first condition in if

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)

if you now that index 1 will occur only if index 0 occurs and will always occur at position 1 in data["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