使用Python借助pymediainfo读取视频文件的详细信息

本文介绍了一种使用Python和pymediainfo库解析MP4文件元数据的方法,包括标题、评论、流派等信息,并提供了详细的代码示例。通过解析,我们可以获取到诸如视频格式、分辨率、帧率等关键属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 按

对于mp4文件,目前已测试在Windows10上设置后支持显示的的详细信息有:标题(title)、备注(comment)、流派(genre)、作曲者(composer
在这里插入图片描述
如需查看更多的详细信息,可以参考本文自己进行编写Python代码来查看。
如需编程批量设置视频文件的详细信息,可以参考:使用FFmpeg设置视频文件的详细信息

2. 代码

安装pymediainfo可以参考:Python3安装pymediainfo

from pymediainfo import MediaInfo
from moviepy.editor import VideoFileClip

media_info = MediaInfo.parse('t2.mp4')

data = media_info.to_json()
print(data)

3. 输出结果

注意:详细信息只有不为空的时候才能找到,如标题(title)为空,则无法找到title这个键。

{
	"tracks": [{
		"track_type": "General",
		"count": "334",
		"count_of_stream_of_this_kind": "1",
		"kind_of_stream": "General",
		"other_kind_of_stream": ["General"],
		"stream_identifier": "0",
		"count_of_video_streams": "1",
		"video_format_list": "AVC",
		"video_format_withhint_list": "AVC",
		"codecs_video": "AVC",
		"video_language_list": "English",
		"complete_name": "t2.mp4",
		"file_name_extension": "t2.mp4",
		"file_name": "t2",
		"file_extension": "mp4",
		"format": "MPEG-4",
		"other_format": ["MPEG-4"],
		"format_extensions_usually_used": "braw mov mp4 m4v m4a m4b m4p m4r 3ga 3gpa 3gpp 3gp 3gpp2 3g2 k3g jpm jpx mqv ismv isma ismt f4a f4b f4v",
		"commercial_name": "MPEG-4",
		"format_profile": "Base Media",
		"internet_media_type": "video/mp4",
		"codec_id": "isom",
		"other_codec_id": ["isom (isom/iso2/avc1/mp41)"],
		"codec_id_url": "http://www.apple.com/quicktime/download/standalone.html",
		"codecid_compatible": "isom/iso2/avc1/mp41",
		"file_size": 6997537,
		"other_file_size": ["6.67 MiB", "7 MiB", "6.7 MiB", "6.67 MiB", "6.673 MiB"],
		"duration": 7842,
		"other_duration": ["7 s 842 ms", "7 s 842 ms", "7 s 842 ms", "00:00:07.842", "00:00:07;25", "00:00:07.842 (00:00:07;25)"],
		"overall_bit_rate": 7138523,
		"other_overall_bit_rate": ["7 139 kb/s"],
		"frame_rate": "29.970",
		"other_frame_rate": ["29.970 FPS"],
		"frame_count": "235",
		"stream_size": 4297,
		"other_stream_size": ["4.20 KiB (0%)", "4 KiB", "4.2 KiB", "4.20 KiB", "4.196 KiB", "4.20 KiB (0%)"],
		"proportion_of_this_stream": "0.00061",
		"headersize": "40",
		"datasize": "6993248",
		"footersize": "4249",
		"isstreamable": "No",
		"title": "coco56_title",
		"collection": "coco56_show",
		"album": "coco56_album",
		"album_performer": "coco56_album_artist",
		"track_name": "coco56_title",
		"grouping": "coco56_grouping",
		"composer": "coco56_composer",
		"genre": "coco56_genre",
		"description": "coco56_description",
		"file_creation_date": "UTC 2020-08-25 16:19:09.863",
		"file_creation_date__local": "2020-08-26 00:19:09.863",
		"file_last_modification_date": "UTC 2020-08-26 10:51:05.790",
		"file_last_modification_date__local": "2020-08-26 18:51:05.790",
		"writing_application": "Lavf58.50.100",
		"other_writing_application": ["Lavf58.50.100"],
		"copyright": "coco56_copyright",
		"lyrics": "coco56_lyrics",
		"comment": "coco56_comment",
		"longdescription": "coco56_synopsis",
		"part_id": "coco56_episode_id",
		"tvnetworkname": "coco56_network"
	}, {
		"track_type": "Video",
		"count": "378",
		"count_of_stream_of_this_kind": "1",
		"kind_of_stream": "Video",
		"other_kind_of_stream": ["Video"],
		"stream_identifier": "0",
		"streamorder": "0",
		"track_id": 1,
		"other_track_id": ["1"],
		"format": "AVC",
		"other_format": ["AVC"],
		"format_info": "Advanced Video Codec",
		"format_url": "http://developers.videolan.org/x264.html",
		"commercial_name": "AVC",
		"format_profile": "High@L5.1",
		"format_settings": "CABAC / 4 Ref Frames",
		"format_settings__cabac": "Yes",
		"other_format_settings__cabac": ["Yes"],
		"format_settings__reference_frames": 4,
		"other_format_settings__reference_frames": ["4 frames"],
		"internet_media_type": "video/H264",
		"codec_id": "avc1",
		"codec_id_info": "Advanced Video Coding",
		"duration": 7842,
		"other_duration": ["7 s 842 ms", "7 s 842 ms", "7 s 842 ms", "00:00:07.842", "00:00:07;25", "00:00:07.842 (00:00:07;25)"],
		"bit_rate": 7134890,
		"other_bit_rate": ["7 135 kb/s"],
		"width": 4096,
		"other_width": ["4 096 pixels"],
		"height": 2048,
		"other_height": ["2 048 pixels"],
		"sampled_width": "4096",
		"sampled_height": "2048",
		"pixel_aspect_ratio": "1.000",
		"display_aspect_ratio": "2.000",
		"other_display_aspect_ratio": ["2.000"],
		"rotation": "0.000",
		"frame_rate_mode": "CFR",
		"other_frame_rate_mode": ["Constant"],
		"framerate_mode_original": "VFR",
		"frame_rate": "29.970",
		"other_frame_rate": ["29.970 (30000/1001) FPS"],
		"framerate_num": "30000",
		"framerate_den": "1001",
		"frame_count": "235",
		"color_space": "YUV",
		"chroma_subsampling": "4:2:0",
		"other_chroma_subsampling": ["4:2:0"],
		"bit_depth": 8,
		"other_bit_depth": ["8 bits"],
		"scan_type": "Progressive",
		"other_scan_type": ["Progressive"],
		"bits__pixel_frame": "0.028",
		"stream_size": 6993240,
		"other_stream_size": ["6.67 MiB (100%)", "7 MiB", "6.7 MiB", "6.67 MiB", "6.669 MiB", "6.67 MiB (100%)"],
		"proportion_of_this_stream": "0.99939",
		"writing_library": "x264 - core 160",
		"other_writing_library": ["x264 core 160"],
		"encoded_library_name": "x264",
		"encoded_library_version": "core 160",
		"encoding_settings": "cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=12 / lookahead_threads=2 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=crf / mbtree=1 / crf=23.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00",
		"language": "en",
		"other_language": ["English", "English", "en", "eng", "en"],
		"codec_configuration_box": "avcC"
	}]
}
### 如何用Python实现视频文件的去重处理 #### 使用OpenCV和哈希算法进行视频去重 通过使用OpenCV库,可以读取视频文件并将其转换为一系列图像帧。随后,可以通过计算这些帧的哈希值来判断两个视频是否具有相同的视觉内容。这种方法适用于检测完全一致的视频副本。 以下是具体实现方式的一个例子: ```python import cv2 import hashlib def get_video_hash(video_path): video = cv2.VideoCapture(video_path) hash_md5 = hashlib.md5() while True: ret, frame = video.read() if not ret: break # 将每一帧转为灰度图并压缩大小以提高效率 gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) resized_frame = cv2.resize(gray_frame, (8, 8)) # 计算该帧的哈希值 hash_md5.update(resized_frame.tobytes()) video.release() return hash_md5.hexdigest() video_hashes = {} for file_name in ["video1.mp4", "video2.mp4"]: vid_hash = get_video_hash(file_name) if vid_hash in video_hashes: print(f"{file_name} is a duplicate of {video_hashes[vid_hash]}") else: video_hashes[vid_hash] = file_name ``` 此代码片段展示了如何加载视频文件、逐帧读取它们的内容,并生成整个视频的唯一标识符[^1]。 #### 利用`pymediainfo`获取媒体详情辅助去重 另一种方法涉及收集更详细的元数据信息作为额外依据来进行匹配。这可能包括比特率、分辨率以及其他编码设置等方面的数据。借助于 `pymediainfo` 库可以从文件中提取此类属性用于进一步分析。 下面是如何运用这一策略的例子: ```python from pymediainfo import MediaInfo def extract_media_info(file_path): media_info = MediaInfo.parse(file_path) data_dict = {} for track in media_info.tracks: if track.track_type == 'Video': data_dict['duration'] = track.duration data_dict['frame_rate'] = track.frame_rate data_dict['bit_rate'] = track.bit_rate return str(data_dict) media_signatures = {} for fname in ['sample_vid_01.avi', 'duplicate_sample_vid_01.mpg']: sig = extract_media_info(fname) if sig in media_signatures: print("{} duplicates {}".format(fname, media_signatures[sig])) else: media_signatures[sig] = fname ``` 上述脚本说明了怎样利用多媒体信息库检索特定类型的轨道特性以便更好地识别潜在冗余项[^2]。 #### 基础参数与哈希组合方案 除了单独依赖视觉相似性或者技术规格之外,还可以综合考虑两者共同构建更加稳健可靠的判定机制。即先定义一组基础指标(比如持续时间、宽高比例),再配合整体摘要函数得出最终结论。 示例如下所示: ```python import os import hashlib def calculate_file_md5(filepath): with open(filepath,'rb') as f: md5obj = hashlib.md5() md5obj.update(f.read()) _hash = md5obj.hexdigest() return _hash unique_videos = set() duplicates_found = [] for root, dirs, files in os.walk("/path/to/videos"): for name in files: full_path = os.path.join(root,name) current_hash = calculate_file_md5(full_path) if current_hash in unique_videos: duplicates_found.append(name) else: unique_videos.add(current_hash) if len(duplicates_found)>0: print("Duplicate videos found:", ", ".join(duplicates_found)) else: print("No duplicates detected.") ``` 这段程序演示了一种简单粗暴却有效的方式—通过对每一个独立实体执行MD5运算从而快速定位重复条目[^3][^5]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

COCO56(徐可可)

建议微信红包:xucoco56

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值