0

I am trying to use if statement to display some video streams parameter(bitrate) in case it exists or displays a message( cannot measure bitrate) if there is no bit rate. and prevent my GUI from crashing.

where is the wrong in my code, or how can I make it better?

import json

import shlex

import subprocess


cmd = "ffprobe -v quiet -print_format json -show_streams"

args = shlex.split(cmd)

myurl = "udp://@239.168.2.6:2113"# this is my video stream 

args.append(myurl)

ffprobeOutput = subprocess.check_output(args).decode('utf-8')

ffprobeOutput = json.loads(ffprobeOutput)

video_stream = next((stream for stream in ffprobeOutput['streams'] if 
stream['codec_type'] == 'video'))



if  int(video_stream['bit_rate']) == True:

    bit_rate1=int(video_stream['bit_rate'])

    print(bit_rate1)

else:

    print('can not measure bitrate')

my video stream has a bitrate parameter but my if statement always give me the else (print('can not measure bitrate'))

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Can you provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), possibly with an example of how ffprobeOutput might be? – musicamante Mar 03 '20 at 10:57
  • you can check it now –  Mar 03 '20 at 11:40
  • as far as I know, the only string that ffprobe provides about bitrate is not "bit_rate", but "bitrate". – musicamante Mar 03 '20 at 12:07
  • is my if statement correct? –  Mar 03 '20 at 12:07
  • Actually, no. Why are you checking if the int is True? If the bitrate is 4 it will never be equal to True. – musicamante Mar 03 '20 at 12:14
  • i tryed this one ........... if video_stream['bit_rate'] in ffprobeOutput['streams']: ........... and still not working –  Mar 03 '20 at 12:25
  • No. You just have to check if the bit_rate key exists in the video_stream: `if 'bit_rate' in video_stream: ...` or, better: `if int(video_stream.get('bit_rate', 0)): ...` – musicamante Mar 03 '20 at 12:30
  • Ok. I'd suggest you to better understand the whole json output: it's a nested structure, and you've to be careful about how to access each level of data (and how to compare values). – musicamante Mar 03 '20 at 12:33

0 Answers0