0

I'm trying to use ffmpeg to get the resolution height and the audio bitrate from a video file, but I'm getting the following error that doesn't tell me much:

File "/home/user/code/python/reduce_video_size/main.py", line 94, in get_metadata
    return video_streams[0]
KeyError: 0

----------------------------------------------------------------------
Ran 1 test in 0.339s

FAILED (errors=1)

so I don't know what can I do to fix it.

print(get_metadata("/home/user/code/python/reduce_video_size/test.mp4"))

def get_metadata(path):
    video_streams = ffmpeg.probe(path, select_streams = "v")
    if video_streams:
        return video_streams[0]

If there's need for more context here is the code.

This solved it but it would still be nice to have some error checking:

def get_metadata(path):
    video_stream = ffmpeg.probe(path, select_streams = "v")
    return video_stream['streams'][0]
  • firstr you could use `print()` to see what you really have in `video_streams` – furas Jun 04 '22 at 20:22
  • maybe you should check `if video_stream['streams']: return video_stream['streams'][0]` or even `if ('streams' in video_stream) and (video_stream['streams']): return video_stream['streams'][0]` – furas Jun 04 '22 at 20:23

1 Answers1

0

According to the source code, ffmpeg.probe returns a dictionary loaded from JSON. So, you don't need to take out the first item and the [0] can be omitted. It does obviously not have any integer indices.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
The_spider
  • 1,202
  • 1
  • 8
  • 18