videohash

repository·main·Indexed 18 days ago

https://github.com/akamhy/videohash

A Python package for near-duplicate video detection using 64-bit perceptual hashing. It generates hashes resilient to resizing, transcoding, color changes, watermarks, and aspect ratio changes. The library provides the VideoHash class to calculate Hamming distance and check for similarity between videos via URLs or local file paths.

Tokens
830
Snippets
2
Records
4
Agent score
14%

What's inside videohash

  1. What is Videohash and how does it work?

    main

    Videohash is a Python package for Perceptual Video Hashing, designed to detect near-duplicate videos. Unlike standard checksums, it generates a 64-bit hash that remains stable even if the video is resized, transcoded, watermarked, or has its color/frame rate/aspect ratio changed.

    How it works

    1. A frame is extracted every second.
    2. Frames are resized to a 144x144 square and arranged into a collage.
    3. A wavelet hash is calculated from this collage.
    4. The frames are stitched horizontally and divided into 64 images; the dominant color of each is compared to a pattern to create a second bit-list.
    5. The final 64-bit hash is generated by bitwise XORing these two bit-lists.
  2. Limitations of Videohash

    main

    While powerful for near-duplicate detection, Videohash has specific limitations:

    • No Video Fingerprinting: It cannot be used to verify if one video is a sub-segment (part) of another video.
    • Rotation/Reversal Sensitivity: If a video is reversed or rotated by more than 10 degrees, the hash will change significantly. To compare a reversed video, you must reverse it manually before hashing.
  3. Install videohash

    main

    To use videohash, you must have FFmpeg installed on your system. Once FFmpeg is available, you can install the package using one of the following methods:

    pip install videohash

    Note: If installation fails, try appending --prefer-binary to the command.

    conda install -c conda-forge videohash
    pip install git+https://github.com/akamhy/videohash.git
    pip install videohash
  4. Use the VideoHash class for near-duplicate detection

    main

    The VideoHash class is the primary interface for generating 64-bit perceptual hashes from videos. You can initialize it using either a url or a local file path.

    Once initialized, you can compare VideoHash instances using several methods and operators:

    • Similarity Check: Use .is_similar(other_hash) to return a boolean indicating if two videos are near-duplicates.
    • Difference Check: Use .is_diffrent(other_hash) to return a boolean indicating if they are different.
    • Hamming Distance (Subtraction): Use the - operator (e.g., hash1 - hash2) to calculate the Hamming distance between two hashes.
    • Equality: Use the == operator to check if hashes are identical.

    Near-duplicate detection is robust against resizing, transcoding, watermarks, color changes, frame rate changes, aspect ratio changes, cropping, and black bars.

    from videohash import VideoHash
    
    # Initialize from a URL
    url1 = "https://example.com/video1.mp4"
    videohash1 = VideoHash(url=url1)
    
    # Initialize from a local path
    path2 = "/path/to/video2.mp4"
    videohash2 = VideoHash(path=path2)
    
    # Compare hashes
    if videohash2.is_similar(videohash1):
        print("Videos are near-duplicates")
    
    distance = videohash2 - videohash1
    print(f"Hamming distance: {distance}")