|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +import tempfile |
| 6 | +import base64 |
| 7 | +import webbrowser |
| 8 | +import argparse |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +def extract_frames(video_path, output_dir): |
| 13 | + output_pattern = str(output_dir / "frame_%04d.png") |
| 14 | + cmd = ['ffmpeg', '-i', video_path, '-vsync', '0', output_pattern, '-y'] |
| 15 | + subprocess.run(cmd, capture_output=True, check=True) |
| 16 | + frames = sorted(output_dir.glob("frame_*.png")) |
| 17 | + return frames |
| 18 | + |
| 19 | + |
| 20 | +def compare_frames(frame1_path, frame2_path): |
| 21 | + result = subprocess.run(['cmp', '-s', frame1_path, frame2_path]) |
| 22 | + return result.returncode == 0 |
| 23 | + |
| 24 | + |
| 25 | +def frame_to_data_url(frame_path): |
| 26 | + with open(frame_path, 'rb') as f: |
| 27 | + data = f.read() |
| 28 | + return f"data:image/png;base64,{base64.b64encode(data).decode()}" |
| 29 | + |
| 30 | + |
| 31 | +def create_diff_video(video1, video2, output_path): |
| 32 | + """Create a diff video using ffmpeg blend filter with difference mode.""" |
| 33 | + print("Creating diff video...") |
| 34 | + cmd = ['ffmpeg', '-i', video1, '-i', video2, '-filter_complex', '[0:v]blend=all_mode=difference', '-vsync', '0', '-y', output_path] |
| 35 | + subprocess.run(cmd, capture_output=True, check=True) |
| 36 | + |
| 37 | + |
| 38 | +def find_differences(video1, video2): |
| 39 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 40 | + tmpdir = Path(tmpdir) |
| 41 | + |
| 42 | + print(f"Extracting frames from {video1}...") |
| 43 | + frames1_dir = tmpdir / "frames1" |
| 44 | + frames1_dir.mkdir() |
| 45 | + frames1 = extract_frames(video1, frames1_dir) |
| 46 | + |
| 47 | + print(f"Extracting frames from {video2}...") |
| 48 | + frames2_dir = tmpdir / "frames2" |
| 49 | + frames2_dir.mkdir() |
| 50 | + frames2 = extract_frames(video2, frames2_dir) |
| 51 | + |
| 52 | + if len(frames1) != len(frames2): |
| 53 | + print(f"WARNING: Frame count mismatch: {len(frames1)} vs {len(frames2)}") |
| 54 | + min_frames = min(len(frames1), len(frames2)) |
| 55 | + frames1 = frames1[:min_frames] |
| 56 | + frames2 = frames2[:min_frames] |
| 57 | + |
| 58 | + print(f"Comparing {len(frames1)} frames...") |
| 59 | + different_frames = [] |
| 60 | + frame_data = [] |
| 61 | + |
| 62 | + for i, (f1, f2) in enumerate(zip(frames1, frames2, strict=False)): |
| 63 | + is_different = not compare_frames(f1, f2) |
| 64 | + if is_different: |
| 65 | + different_frames.append(i) |
| 66 | + |
| 67 | + if i < 10 or i >= len(frames1) - 10 or is_different: |
| 68 | + frame_data.append({'index': i, 'different': is_different, 'frame1_url': frame_to_data_url(f1), 'frame2_url': frame_to_data_url(f2)}) |
| 69 | + |
| 70 | + return different_frames, frame_data, len(frames1) |
| 71 | + |
| 72 | + |
| 73 | +def generate_html_report(video1, video2, different_frames, frame_data, total_frames): |
| 74 | + chunks = [] |
| 75 | + if different_frames: |
| 76 | + current_chunk = [different_frames[0]] |
| 77 | + for i in range(1, len(different_frames)): |
| 78 | + if different_frames[i] == different_frames[i - 1] + 1: |
| 79 | + current_chunk.append(different_frames[i]) |
| 80 | + else: |
| 81 | + chunks.append(current_chunk) |
| 82 | + current_chunk = [different_frames[i]] |
| 83 | + chunks.append(current_chunk) |
| 84 | + |
| 85 | + result_text = ( |
| 86 | + f"✅ Videos are identical! ({total_frames} frames)" |
| 87 | + if len(different_frames) == 0 |
| 88 | + else f"❌ Found {len(different_frames)} different frames out of {total_frames} total ({(len(different_frames) / total_frames * 100):.1f}%)" |
| 89 | + ) |
| 90 | + |
| 91 | + html = f"""<h2>UI Diff</h2> |
| 92 | +<table> |
| 93 | +<tr> |
| 94 | +<td width='33%'> |
| 95 | + <p><strong>Video 1</strong></p> |
| 96 | + <video id='video1' width='100%' autoplay muted loop onplay='syncVideos()'> |
| 97 | + <source src='{os.path.basename(video1)}' type='video/mp4'> |
| 98 | + Your browser does not support the video tag. |
| 99 | + </video> |
| 100 | +</td> |
| 101 | +<td width='33%'> |
| 102 | + <p><strong>Video 2</strong></p> |
| 103 | + <video id='video2' width='100%' autoplay muted loop onplay='syncVideos()'> |
| 104 | + <source src='{os.path.basename(video2)}' type='video/mp4'> |
| 105 | + Your browser does not support the video tag. |
| 106 | + </video> |
| 107 | +</td> |
| 108 | +<td width='33%'> |
| 109 | + <p><strong>Pixel Diff</strong></p> |
| 110 | + <video id='diffVideo' width='100%' autoplay muted loop> |
| 111 | + <source src='diff.mp4' type='video/mp4'> |
| 112 | + Your browser does not support the video tag. |
| 113 | + </video> |
| 114 | +</td> |
| 115 | +</tr> |
| 116 | +</table> |
| 117 | +<script> |
| 118 | +function syncVideos() {{ |
| 119 | + const video1 = document.getElementById('video1'); |
| 120 | + const video2 = document.getElementById('video2'); |
| 121 | + const diffVideo = document.getElementById('diffVideo'); |
| 122 | + video1.currentTime = video2.currentTime = diffVideo.currentTime; |
| 123 | +}} |
| 124 | +video1.addEventListener('timeupdate', () => {{ |
| 125 | + if (Math.abs(video1.currentTime - video2.currentTime) > 0.1) {{ |
| 126 | + video2.currentTime = video1.currentTime; |
| 127 | + }} |
| 128 | + if (Math.abs(video1.currentTime - diffVideo.currentTime) > 0.1) {{ |
| 129 | + diffVideo.currentTime = video1.currentTime; |
| 130 | + }} |
| 131 | +}}); |
| 132 | +video2.addEventListener('timeupdate', () => {{ |
| 133 | + if (Math.abs(video2.currentTime - video1.currentTime) > 0.1) {{ |
| 134 | + video1.currentTime = video2.currentTime; |
| 135 | + }} |
| 136 | + if (Math.abs(video2.currentTime - diffVideo.currentTime) > 0.1) {{ |
| 137 | + diffVideo.currentTime = video2.currentTime; |
| 138 | + }} |
| 139 | +}}); |
| 140 | +diffVideo.addEventListener('timeupdate', () => {{ |
| 141 | + if (Math.abs(diffVideo.currentTime - video1.currentTime) > 0.1) {{ |
| 142 | + video1.currentTime = diffVideo.currentTime; |
| 143 | + video2.currentTime = diffVideo.currentTime; |
| 144 | + }} |
| 145 | +}}); |
| 146 | +</script> |
| 147 | +<hr> |
| 148 | +<p><strong>Results:</strong> {result_text}</p> |
| 149 | +""" |
| 150 | + return html |
| 151 | + |
| 152 | + |
| 153 | +def main(): |
| 154 | + parser = argparse.ArgumentParser(description='Compare two videos and generate HTML diff report') |
| 155 | + parser.add_argument('video1', help='First video file') |
| 156 | + parser.add_argument('video2', help='Second video file') |
| 157 | + parser.add_argument('output', nargs='?', default='diff.html', help='Output HTML file (default: diff.html)') |
| 158 | + parser.add_argument('--no-open', action='store_true', help='Do not open HTML report in browser') |
| 159 | + |
| 160 | + args = parser.parse_args() |
| 161 | + |
| 162 | + print("=" * 60) |
| 163 | + print("VIDEO DIFF - HTML REPORT") |
| 164 | + print("=" * 60) |
| 165 | + print(f"Video 1: {args.video1}") |
| 166 | + print(f"Video 2: {args.video2}") |
| 167 | + print(f"Output: {args.output}") |
| 168 | + print() |
| 169 | + |
| 170 | + # Create diff video |
| 171 | + diff_video_path = os.path.join(os.path.dirname(args.output), "diff.mp4") |
| 172 | + create_diff_video(args.video1, args.video2, diff_video_path) |
| 173 | + |
| 174 | + different_frames, frame_data, total_frames = find_differences(args.video1, args.video2) |
| 175 | + |
| 176 | + if different_frames is None: |
| 177 | + sys.exit(1) |
| 178 | + |
| 179 | + print() |
| 180 | + print("Generating HTML report...") |
| 181 | + html = generate_html_report(args.video1, args.video2, different_frames, frame_data, total_frames) |
| 182 | + |
| 183 | + with open(args.output, 'w') as f: |
| 184 | + f.write(html) |
| 185 | + |
| 186 | + # Open in browser by default |
| 187 | + if not args.no_open: |
| 188 | + print(f"Opening {args.output} in browser...") |
| 189 | + webbrowser.open(f'file://{os.path.abspath(args.output)}') |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == "__main__": |
| 193 | + main() |
0 commit comments