Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get dimensions of a video file

Tags:

python

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.

like image 577
David542 Avatar asked Sep 08 '11 13:09

David542


People also ask

How do I find the aspect ratio of a video?

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.

How big is a 30 minute 1080p video?

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.

What do you call the size of a video?

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.


2 Answers

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)
like image 149
omargamal8 Avatar answered Oct 20 '22 18:10

omargamal8


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.

like image 15
Piotr Dabkowski Avatar answered Oct 20 '22 20:10

Piotr Dabkowski