Python
You can extract streams using following code
import json
infile = "<path to your file>"
command = "ffprobe -v quiet -print_format json -show_format -show_streams {}".format(infile)
res = json.loads(subprocess.check_output(command, shell=True))
streams = res.get('streams')
Each stream would have a field duration
.
Special case of Webm
If your input file is webm, Your method of extracting stream duration is going to be different, since the standard store it in a different location.
In each stream you would see a field 'tags'
"tags": {
"language": "eng",
"HANDLER_NAME": "ISO Media file produced by Google Inc. Created on: 12/09/2019.",
"VENDOR_ID": "[0][0][0][0]",
"ENCODER": "Lavc59.4.101 libvorbis",
"DURATION": "00:03:14.703000000"
}
You would have to convert this human readable time format into seconds. Which you can easily do with following utility function.
def convert_secs(text):
if isinstance(text, float):
num = str(text)
nums = num.split('.')
else:
nums = text.split(':')
if len(nums) == 2:
st_sn = int(nums[0]) * 60 + float(nums[1])
return st_sn
elif len(nums) == 3:
st_sn = int(nums[0]) * 3600 + int(nums[1]) * 60 + float(nums[2])
return st_sn
else:
raise ValueError("Not correct time")