Is there a way in python to get the dimensions of a video file or some other library that would accomplish this? The equivalent of a Media Info
or something? Thank you.
You can calculate the aspect ratio of a video by dividing the video's width by its height. However, to calculate the video resolution, you'll multiply the video's width by its height. A video with three different resolutions like 720p, 1080p, and 2160p can all have the same aspect ratio of 16:9.
60 MB with 720p at 30 fps. 130 MB with 1080p at 30 fps. 175 MB with 1080p at 60fps. 350 MB with 4K at 30 fps.
Video resolution is measured as width by height using pixels as units. Pixels are tiny squares that make up a digital image. For example, a 1920 x 1080 video means the width has 1,920 pixels and the height 1,080 pixels.
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)
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With