Get dimensions of a video file

In my last company we had similar problem and I couldn't find any python library to do this. So I ended up using mediainfo from python, media info also has a command line option and it is very easy to parse the output, so practically your python module which uses media-info will be sufficient. It has further advantage because eventually you will find all media-info type software doesn't support all codecs/format so you can use multiple software/libs under the hood with single python wrapper.


How about using python-ffmpeg:

import ffmpeg
probe = ffmpeg.probe(movie_path)
video_streams = [stream for stream in probe["streams"] if stream["codec_type"] == "video"]

It gives you a very nice output like this:

>>> import pprint
>>> pprint.pprint(video_streams[0])
{'avg_frame_rate': '30/1',
 'bit_rate': '3291',
 'bits_per_raw_sample': '8',
 'chroma_location': 'left',
 'codec_long_name': 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10',
 'codec_name': 'h264',
 'codec_tag': '0x31637661',
 'codec_tag_string': 'avc1',
 'codec_time_base': '1/60',
 'codec_type': 'video',
 'coded_height': 240,
 'coded_width': 320,
 'color_primaries': 'bt709',
 'color_range': 'tv',
 'color_space': 'bt709',
 'color_transfer': 'bt709',
 'display_aspect_ratio': '4:3',
 'disposition': {'attached_pic': 0,
                 'clean_effects': 0,
                 'comment': 0,
                 'default': 1,
                 'dub': 0,
                 'forced': 0,
                 'hearing_impaired': 0,
                 'karaoke': 0,
                 'lyrics': 0,
                 'original': 0,
                 'timed_thumbnails': 0,
                 'visual_impaired': 0},
 'duration': '71.833333',
 'duration_ts': 6465000,
 'field_order': 'progressive',
 'has_b_frames': 1,
 'height': 240,
 'index': 0,
 'is_avc': 'true',
 'level': 13,
 'nal_length_size': '4',
 'pix_fmt': 'yuv420p',
 'profile': 'Main',
 'r_frame_rate': '30/1',
 'refs': 1,
 'sample_aspect_ratio': '1:1',
 'start_pts': 0,
 'start_time': '0.000000',
 'tags': {'creation_time': '2018-10-26T04:25:07.000000Z',
          'handler_name': 'VideoHandler',
          'language': 'und'},
 'time_base': '1/90000',
 'width': 320}

The only downside is that ffmpeg is required, but this should not be a problem in most of the cases.


If I understood you correctly, you mean the resolution of a video for example (768x432).

This could be done simply using opencv in python.

import cv2
file_path = "./video.avi"  # change to your own video path
vid = cv2.VideoCapture(file_path)
height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)

This library seems to have an example that does just that on its main page (print_info(vs)):

http://code.google.com/p/ffvideo/

It's a wrapper around ffmpeg (there seems to be a few Python libraries for using ffmpeg).

Tags:

Python