用python比较两个mp4是否实质相同
下面这个脚本会使用 ffmpeg
和 ffprobe
检查两个视频文件在以下方面是否“实质相同”:
✅ 检查内容:
- 分辨率(宽高)
- 帧率
- 视频总帧数
- 音频轨道数量和采样率
- 视频时长
- 视频帧哈希(可选) — 对比前 N 帧的图像感知哈希,判断是否完全一致(更精确)
🧰 脚本需求:
确保已安装:
sudo apt install ffmpeg
pip install imagehash Pillow
🐍 Python 脚本如下:
import subprocess
import json
import imagehash
from PIL import Image
import osdef ffprobe_info(path):cmd = ['ffprobe', '-v', 'error', '-show_entries','format=duration', '-show_streams','-of', 'json', path]output = subprocess.check_output(cmd)return json.loads(output)def extract_frame(path, frame_num, output_path):cmd = ['ffmpeg', '-y', '-i', path,'-vf', f'select=eq(n\,{frame_num})','-vframes', '1', output_path]subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)def compare_hashes(file1, file2, frames_to_check=5):for i in range(frames_to_check):extract_frame(file1, i * 10, 'frame1.jpg')extract_frame(file2, i * 10, 'frame2.jpg')try:h1 = imagehash.average_hash(Image.open('frame1.jpg'))h2 = imagehash.average_hash(Image.open('frame2.jpg'))if h1 != h2:print(f"[❌] Frame {i*10} differs: {h1} vs {h2}")return Falseexcept Exception as e:print(f"[⚠️] Error comparing frame {i*10}: {e}")return Falseprint("[✅] Frame hashes match.")return Truedef compare_videos(src, dst):info1 = ffprobe_info(src)info2 = ffprobe_info(dst)def get_val(info, stream_type, key):for stream in info['streams']:if stream['codec_type'] == stream_type:return stream.get(key)return Nonecomparisons = [('Resolution', f"{get_val(info1, 'video', 'width')}x{get_val(info1, 'video', 'height')}",f"{get_val(info2, 'video', 'width')}x{get_val(info2, 'video', 'height')}"),('Frame rate', get_val(info1, 'video', 'r_frame_rate'),get_val(info2, 'video', 'r_frame_rate')),('Audio rate', get_val(info1, 'audio', 'sample_rate'),get_val(info2, 'audio', 'sample_rate')),('Duration (s)', round(float(info1['format']['duration']), 2),round(float(info2['format']['duration']), 2)),]all_match = Truefor name, val1, val2 in comparisons:match = "✅" if val1 == val2 else "❌"print(f"[{match}] {name}: {val1} vs {val2}")if val1 != val2:all_match = Falseprint("\n🔍 Checking frame visual hashes (first few frames)...")if not compare_hashes(src, dst):all_match = Falseif all_match:print("\n🎉 视频文件在核心内容上完全一致")else:print("\n⚠️ 视频存在差异")# 清理临时文件os.remove('frame1.jpg') if os.path.exists('frame1.jpg') else Noneos.remove('frame2.jpg') if os.path.exists('frame2.jpg') else None# 使用示例
compare_videos("yanhuorenjia_12_src.mp4", "yanhuorenjia_12_dst.mp4")
💡 说明:
- 它会提取每个视频的前 5 帧(间隔取)做图像哈希对比,适合检查是否重新编码或压缩。
- 如果帧、音频、时长、分辨率都一样且图像一致,那可以认为视频实质相同。
- 支持大视频,速度也不错。