pymediainfoを使用してPythonから動画・音声ファイルのメタ情報を取得する事ができます。
インストール方法
1. pipenvをインストールしていない場合は、以下のコマンドを実行します。
sudo apt-get update
sudo apt-get -y install python3-pip python3-distutils python3-dev
python3 -m pip install --user pipenv
echo "export PIPENV_VENV_IN_PROJECT=true" >> ~/.profile
echo 'export PATH=$PATH:$HOME/.local/bin' >> ~/.profile
source ~/.profile
2. pymediainfoのインストール
sudo apt-get update
sudo apt-get -y install mediainfo libjpeg-dev
pipenv --python 3
pipenv install pymediainfo
サンプルプログラム
ファイル中のトラックから、コーデックの種類や解像度などの情報を一覧表示します。
※ファイル名は適宜変更してください
mi.py
from pymediainfo import MediaInfo
mi = MediaInfo.parse("/home/debian/hydrangea.mp4")
for track in mi.tracks:
print("--------------------------")
for mykey, myvalue in track.to_data().items():
print("key: {}, value:{}".format(mykey, myvalue))
〇実行例
pipenv run python mi.py
--------------------------
key: track_type, value:General
key: count, value:333
key: count_of_stream_of_this_kind, value:1
key: kind_of_stream, value:General
key: other_kind_of_stream, value:['General']
key: stream_identifier, value:0
key: count_of_video_streams, value:1
key: count_of_audio_streams, value:1
key: video_format_list, value:AVC
key: video_format_withhint_list, value:AVC
key: codecs_video, value:AVC
key: video_language_list, value:English
key: audio_format_list, value:AAC LC
key: audio_format_withhint_list, value:AAC LC
key: audio_codecs, value:AAC LC
key: audio_language_list, value:English
key: complete_name, value:/home/debian/hydrangea.mp4
key: folder_name, value:/home/debian
key: file_name, value:hydrangea.mp4
key: other_file_name, value:['hydrangea']
key: file_extension, value:mp4
key: format, value:MPEG-4
key: other_format, value:['MPEG-4']
key: format_extensions_usually_used, value:mov mp4 m4v m4a m4b m4p m4r 3ga 3gpa 3gpp 3gp 3gpp2 3g2 k3g jpm jpx mqv ismv isma ismt f4a f4b f4v
key: commercial_name, value:MPEG-4
key: format_profile, value:Base Media / Version 2
key: internet_media_type, value:video/mp4
key: codec_id, value:mp42
key: other_codec_id, value:['mp42 (isom/mp42)']
key: codec_id_url, value:http://www.apple.com/quicktime/download/standalone.html
key: codecid_compatible, value:isom/mp42
key: file_size, value:134478747
key: other_file_size, value:['128 MiB', '128 MiB', '128 MiB', '128 MiB', '128.2 MiB']
key: duration, value:61065
key: other_duration, value:['1 min 1 s', '1 min 1 s 65 ms', '1 min 1 s', '00:01:01.065', '00:01:01:02', '00:01:01.065 (00:01:01:02)']
key: overall_bit_rate, value:17617784
key: other_overall_bit_rate, value:['17.6 Mb/s']
key: frame_rate, value:30.000
key: other_frame_rate, value:['30.000 FPS']
key: frame_count, value:1832
key: stream_size, value:26507
key: other_stream_size, value:['25.9 KiB (0%)', '26 KiB', '26 KiB', '25.9 KiB', '25.89 KiB', '25.9 KiB (0%)']
key: proportion_of_this_stream, value:0.00020
key: headersize, value:3249
key: datasize, value:134452248
key: footersize, value:23250
key: isstreamable, value:No
key: encoded_date, value:UTC 2020-06-12 05:37:22
key: tagged_date, value:UTC 2020-06-12 05:37:22
key: file_last_modification_date, value:UTC 2020-06-12 05:37:22
key: file_last_modification_date__local, value:2020-06-12 14:37:22
key: xyz, value:+00.0000+000.0000/
key: comandroidversion, value:8.0.0
--------------------------
key: track_type, value:Video
key: count, value:376
key: count_of_stream_of_this_kind, value:1
key: kind_of_stream, value:Video
key: other_kind_of_stream, value:['Video']
key: stream_identifier, value:0
key: streamorder, value:1
key: track_id, value:2
key: other_track_id, value:['2']
key: format, value:AVC
key: other_format, value:['AVC']
key: format_info, value:Advanced Video Codec
key: format_url, value:http://developers.videolan.org/x264.html
key: commercial_name, value:AVC
key: format_profile, value:High@L4
key: format_settings, value:CABAC / 1 Ref Frames
key: format_settings__cabac, value:Yes
key: other_format_settings__cabac, value:['Yes']
key: format_settings__reframes, value:1
key: other_format_settings__reframes, value:['1 frame']
key: format_settings__gop, value:M=1, N=30
key: internet_media_type, value:video/H264
key: codec_id, value:avc1
key: codec_id_info, value:Advanced Video Coding
key: duration, value:61065
key: other_duration, value:['1 min 1 s', '1 min 1 s 65 ms', '1 min 1 s', '00:01:01.065', '00:01:01:02', '00:01:01.065 (00:01:01:02)']
key: bit_rate, value:17458076
key: other_bit_rate, value:['17.5 Mb/s']
key: width, value:1920
key: other_width, value:['1 920 pixels']
key: height, value:1080
key: other_height, value:['1 080 pixels']
key: stored_height, value:1088
key: sampled_width, value:1920
key: sampled_height, value:1080
key: pixel_aspect_ratio, value:1.000
key: display_aspect_ratio, value:1.778
key: other_display_aspect_ratio, value:['16:9']
key: rotation, value:0.000
key: frame_rate_mode, value:VFR
key: other_frame_rate_mode, value:['Variable']
key: frame_rate, value:30.000
key: other_frame_rate, value:['30.000 FPS']
key: minimum_frame_rate, value:29.841
key: other_minimum_frame_rate, value:['29.841 FPS']
key: maximum_frame_rate, value:30.141
key: other_maximum_frame_rate, value:['30.141 FPS']
key: frame_count, value:1832
key: standard, value:NTSC
key: color_space, value:YUV
key: chroma_subsampling, value:4:2:0
key: other_chroma_subsampling, value:['4:2:0']
key: bit_depth, value:8
key: other_bit_depth, value:['8 bits']
key: scan_type, value:Progressive
key: other_scan_type, value:['Progressive']
key: bits__pixel_frame, value:0.281
key: stream_size, value:133263312
key: other_stream_size, value:['127 MiB (99%)', '127 MiB', '127 MiB', '127 MiB', '127.1 MiB', '127 MiB (99%)']
key: proportion_of_this_stream, value:0.99096
key: title, value:VideoHandle
key: language, value:en
key: other_language, value:['English', 'English', 'en', 'eng', 'en']
key: encoded_date, value:UTC 2020-06-12 05:37:22
key: tagged_date, value:UTC 2020-06-12 05:37:22
key: colour_description_present, value:Yes
key: colour_description_present_source, value:Stream
key: color_range, value:Limited
key: colour_range_source, value:Stream
key: color_primaries, value:BT.601 PAL
key: colour_primaries_source, value:Stream
key: transfer_characteristics, value:BT.601
key: transfer_characteristics_source, value:Stream
key: matrix_coefficients, value:BT.470 System B/G
key: matrix_coefficients_source, value:Stream
key: mdhd_duration, value:61065
key: codec_configuration_box, value:avcC
--------------------------
key: track_type, value:Audio
key: count, value:278
key: count_of_stream_of_this_kind, value:1
key: kind_of_stream, value:Audio
key: other_kind_of_stream, value:['Audio']
key: stream_identifier, value:0
key: streamorder, value:0
key: track_id, value:1
key: other_track_id, value:['1']
key: format, value:AAC
key: other_format, value:['AAC LC']
key: format_info, value:Advanced Audio Codec Low Complexity
key: commercial_name, value:AAC
key: format_additionalfeatures, value:LC
key: codec_id, value:mp4a-40-2
key: duration, value:60970
key: other_duration, value:['1 min 0 s', '1 min 0 s 970 ms', '1 min 0 s', '00:01:00.970', '00:01:00:38', '00:01:00.970 (00:01:00:38)']
key: source_duration, value:61071
key: other_source_duration, value:['1 min 1 s', '1 min 1 s 71 ms', '1 min 1 s', '00:01:01.071']
key: source_duration_firstframe, value:100
key: other_source_duration_firstframe, value:['100 ms', '100 ms', '100 ms', '00:00:00.100']
key: bit_rate_mode, value:CBR
key: other_bit_rate_mode, value:['Constant']
key: bit_rate, value:156000
key: other_bit_rate, value:['156 kb/s']
key: channel_s, value:2
key: other_channel_s, value:['2 channels']
key: channel_positions, value:Front: L R
key: other_channel_positions, value:['2/0/0']
key: channel_layout, value:L R
key: samples_per_frame, value:1024
key: sampling_rate, value:48000
key: other_sampling_rate, value:['48.0 kHz']
key: samples_count, value:2926560
key: frame_rate, value:46.875
key: other_frame_rate, value:['46.875 FPS (1024 SPF)']
key: frame_count, value:2858
key: source_frame_count, value:2858
key: compression_mode, value:Lossy
key: other_compression_mode, value:['Lossy']
key: stream_size, value:1188928
key: other_stream_size, value:['1.13 MiB (1%)', '1 MiB', '1.1 MiB', '1.13 MiB', '1.134 MiB', '1.13 MiB (1%)']
key: proportion_of_this_stream, value:0.00884
key: source_stream_size, value:1188928
key: other_source_stream_size, value:['1.13 MiB (1%)', '1 MiB', '1.1 MiB', '1.13 MiB', '1.134 MiB', '1.13 MiB (1%)']
key: source_streamsize_proportion, value:0.00884
key: title, value:SoundHandle
key: language, value:en
key: other_language, value:['English', 'English', 'en', 'eng', 'en']
key: encoded_date, value:UTC 2020-06-12 05:37:22
key: tagged_date, value:UTC 2020-06-12 05:37:22
key: mdhd_duration, value:60970