Detect Looping Video Frames with Perceptual Hashing in Python
This article demonstrates how to use a perceptual average‑hash algorithm in Python to identify duplicate frames in a 24‑hour video, revealing hidden loops and exposing potential video manipulation through systematic frame comparison and analysis.
Plan
Write a program to detect loops in a video using Python, despite the author's limited experience with video processing.
First Attempt
Viewing a video is like rapidly flipping through images; each frame is an image array of RGB values. By counting identical frames with two dictionaries—one for seen frames and another for duplicate frames—the author attempts to spot repeats.
Code (shown as an image):
The script ran for about an hour on a MacBook Pro, producing results that highlighted matching frame pairs such as 5928 with 2048454 and 5936 with 2048462.
Complexity Increases
The program identifies identical frames to determine looping, but some visually identical frames were not flagged due to compression noise. Subtracting one frame from another revealed minor pixel differences caused by video compression.
Hashing each image converts it to an integer; identical images yield the same hash, but the goal is to have similar hashes for slightly different frames as well.
Simplifying the Compression Issue
Perceptual hashing (aHash) produces similar hashes for visually similar inputs, useful for reverse image search. For video, the hash function must be tolerant to compression noise yet sensitive enough to distinguish adjacent frames.
Be tolerant: frames differing only due to compression should hash to the same value.
Be sensitive: adjacent frames should produce different hashes.
Choosing aHash Parameters
The author experiments with resolutions 8×8 and 64×64. The 8×8 resolution loses too much detail, while 64×64 retains more information but may still miss subtle differences.
Testing various resolutions on similar videos yields match buckets such as [8,108], [9,109], and [10,11,110,111], indicating occasional false positives.
Metrics considered include the number of matching buckets, average frames per bucket, and maximum bucket size, aiming for many small buckets (ideally two frames per bucket for a single loop).
Resolution 24 provides a better balance, reducing excessive matches.
Results
Replacing the original hash with the new aHash produced many matching frames. Sample matching frame indices include 4262, 72096, 124855, 132392, 147466, 162540, 170077, 185151, 207762, 252984, etc.
Converted to timestamps (seconds), some matches occur at 142.07 s, 2403.2 s, 4161.83 s, 4413.07 s, 4915.53 s, 5418.0 s, 5669.23 s, 6171.7 s, 6925.4 s, and so on, often aligning with the midpoint of the slapping action in the video.
These findings suggest the video contains repeated segments, supporting the hypothesis that the 24‑hour slapping video is fabricated.
Author: Python developer Source: http://developer.51cto.com/art/201706/543686.htm
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
