Home > OS >  KeyError getting metadata from video file
KeyError getting metadata from video file

Time:06-05

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]

CodePudding user response:

According to the source code, ffmpeg.probe returns a dictionnary 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.

  • Related