如何用Python获取MP4文件评论?批量处理500+文件方案咨询
批量提取MP4文件评论的可行方案
方案一:使用ffprobe(命令行批量处理)
ffprobe是ffmpeg生态下的活跃维护工具,可可靠读取MP4元数据(包括评论),无需依赖过时第三方库。
单文件提取命令
ffprobe -v quiet -print_format json -show_format "目标文件.mp4" | jq '.format.tags.comment'
参数说明:
-v quiet:关闭冗余日志输出-print_format json:以JSON格式输出元数据,便于解析jq:用于精准提取comment字段(需提前安装jq工具)
批量处理脚本
Linux/macOS(Bash脚本)
#!/bin/bash output_file="mp4_comments.txt" > "$output_file" # 清空输出文件 for file in *.mp4; do # 无评论时返回"无评论"占位符 comment=$(ffprobe -v quiet -print_format json -show_format "$file" | jq -r '.format.tags.comment // "无评论"') echo "$file: $comment" >> "$output_file" done
Windows(PowerShell脚本)
$outputFile = "mp4_comments.txt" Clear-Content $outputFile -ErrorAction SilentlyContinue Get-ChildItem -Filter *.mp4 | ForEach-Object { $comment = ffprobe -v quiet -print_format json -show_format $_.FullName | jq -r '.format.tags.comment // "无评论"' Add-Content $outputFile "$($_.Name): $comment" }
方案二:使用Python的pymediainfo包(适配后续脚本调用)
pymediainfo是基于MediaInfo库的Python绑定,目前维护状态良好,支持读取多种媒体文件的元数据,适合嵌入自定义脚本。
操作步骤
- 安装依赖:
pip install pymediainfo
同时需自行安装MediaInfo官方发行版(各系统对应包管理器或官方安装包均可)。
- 批量提取脚本
from pymediainfo import MediaInfo import os def get_mp4_comment(file_path): media_info = MediaInfo.parse(file_path) for track in media_info.tracks: if track.track_type == 'General': return track.comment or '无评论' return '无评论' # 配置路径 input_dir = './' # 替换为你的MP4文件目录 output_file = 'mp4_comments.txt' # 方式1:保存结果到文件 with open(output_file, 'w', encoding='utf-8') as f: for filename in os.listdir(input_dir): if filename.lower().endswith('.mp4'): file_path = os.path.join(input_dir, filename) comment = get_mp4_comment(file_path) f.write(f"{filename}: {comment}\n") # 方式2:保存为字典变量供后续脚本使用 comments_dict = {} for filename in os.listdir(input_dir): if filename.lower().endswith('.mp4'): file_path = os.path.join(input_dir, filename) comments_dict[filename] = get_mp4_comment(file_path) # 后续脚本可直接调用comments_dict
内容的提问来源于stack exchange,提问作者cavia cavia
相关产品推荐
相关产品推荐

