Get dimensions of a video file

前端 未结 8 2063
情歌与酒
情歌与酒 2020-12-01 09:39

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? Th

8条回答
  •  既然无缘
    2020-12-01 09:53

    There is a python module called pymediainfo - https://pypi.org/project/pymediainfo/ You can use this to get the required metadata of the media file.

    Create a directory

    mkdir supportfiles
    

    For some reasons I install the module in target directory called "supportfiles"

    pip install pymediainfo -t supportfiles/
    

    To get the dimension/resolution of the video file

    resolution.py

    from supportfiles.pymediainfo import MediaInfo
    media_info = MediaInfo.parse('/home/sathish/Videos/Aandipatti.mp4')
    
    for track in media_info.tracks:
        if track.track_type == 'Video':
            print ("Resolution {}x{}".format(track.width, track.height))
    

    Here is the output

    [sathish@localhost test]$ python3.6 resolution.py 
    Resolution 1920x1080
    

    Just in case if you want to know the list of metadata attributes, Here is the solution

    attributes.py

    from supportfiles.pymediainfo import MediaInfo
    media_info = MediaInfo.parse('/home/sathish/Videos/Aandipatti.mp4')
    print(media_info.tracks)
    for track in media_info.tracks:
        if track.track_type == 'Video':
            print(track.to_data().keys())
        elif track.track_type == 'Audio':
            print(track.to_data().keys())
        elif track.track_type == 'General':
            print(track.to_data().keys())
        else:
            print("No metadata present! or probably file corrupt!")
    

    Here is the list of the attributes

    [sathish@localhost test]$ python3.6 attributes.py 
    [, , ]
    
    dict_keys(['track_type', 'count', 'count_of_stream_of_this_kind', 'kind_of_stream', 'other_kind_of_stream', 'stream_identifier', 'count_of_video_streams', 'count_of_audio_streams', 'video_format_list', 'video_format_withhint_list', 'codecs_video', 'audio_format_list', 'audio_format_withhint_list', 'audio_codecs', 'complete_name', 'folder_name', 'file_name', 'file_extension', 'format', 'other_format', 'format_extensions_usually_used', 'commercial_name', 'format_profile', 'internet_media_type', 'codec_id', 'other_codec_id', 'codec_id_url', 'codecid_compatible', 'codec', 'other_codec', 'codec_extensions_usually_used', 'file_size', 'other_file_size', 'duration', 'other_duration', 'overall_bit_rate', 'other_overall_bit_rate', 'frame_rate', 'other_frame_rate', 'frame_count', 'stream_size', 'other_stream_size', 'proportion_of_this_stream', 'headersize', 'datasize', 'footersize', 'isstreamable', 'file_last_modification_date', 'file_last_modification_date__local', 'writing_application', 'other_writing_application'])
    
    dict_keys(['track_type', 'count', 'count_of_stream_of_this_kind', 'kind_of_stream', 'other_kind_of_stream', 'stream_identifier', 'streamorder', 'track_id', 'other_track_id', 'format', 'format_info', 'format_url', 'commercial_name', 'format_profile', 'format_settings', 'format_settings__cabac', 'other_format_settings__cabac', 'format_settings__reframes', 'other_format_settings__reframes', 'internet_media_type', 'codec_id', 'codec_id_info', 'codec', 'other_codec', 'codec_family', 'codec_info', 'codec_url', 'codec_cc', 'codec_profile', 'codec_settings', 'codec_settings__cabac', 'codec_settings_refframes', 'duration', 'other_duration', 'duration_firstframe', 'other_duration_firstframe', 'bit_rate', 'other_bit_rate', 'width', 'other_width', 'height', 'other_height', 'stored_height', 'sampled_width', 'sampled_height', 'pixel_aspect_ratio', 'display_aspect_ratio', 'other_display_aspect_ratio', 'rotation', 'frame_rate_mode', 'other_frame_rate_mode', 'frame_rate', 'other_frame_rate', 'minimum_frame_rate', 'other_minimum_frame_rate', 'maximum_frame_rate', 'other_maximum_frame_rate', 'frame_count', 'resolution', 'other_resolution', 'colorimetry', 'color_space', 'chroma_subsampling', 'other_chroma_subsampling', 'bit_depth', 'other_bit_depth', 'scan_type', 'other_scan_type', 'interlacement', 'other_interlacement', 'bits__pixel_frame', 'stream_size', 'other_stream_size', 'proportion_of_this_stream', 'color_range', 'colour_description_present', 'color_primaries', 'transfer_characteristics', 'matrix_coefficients'])
    
    dict_keys(['track_type', 'count', 'count_of_stream_of_this_kind', 'kind_of_stream', 'other_kind_of_stream', 'stream_identifier', 'streamorder', 'track_id', 'other_track_id', 'format', 'format_info', 'commercial_name', 'format_profile', 'codec_id', 'codec', 'other_codec', 'codec_family', 'codec_cc', 'duration', 'other_duration', 'bit_rate_mode', 'other_bit_rate_mode', 'bit_rate', 'other_bit_rate', 'channel_s', 'other_channel_s', 'channel_positions', 'other_channel_positions', 'channellayout', 'samples_per_frame', 'sampling_rate', 'other_sampling_rate', 'samples_count', 'frame_rate', 'other_frame_rate', 'frame_count', 'compression_mode', 'other_compression_mode', 'stream_size', 'other_stream_size', 'proportion_of_this_stream', 'default', 'other_default', 'alternate_group', 'other_alternate_group'])
    

    Hope this will be useful!

提交回复
热议问题