#hash #frame #compute #extract #perceptual #difference

bin+lib videohash

The videohash crate provides functionality for computing perceptual hashes (pHash) and difference hashes (dHash) from video files. This crate extracts frames from videos and computes these hashes for each frame.

2 releases

0.1.1 Sep 12, 2024
0.1.0 Sep 12, 2024

#121 in Video

Download history 133/week @ 2024-09-06 102/week @ 2024-09-13 32/week @ 2024-09-20 37/week @ 2024-09-27 7/week @ 2024-10-04

80 downloads per month

MIT license

20KB
229 lines

VideoHash Crate

The videohash crate provides functionality for computing perceptual hashes (pHash) and difference hashes (dHash) from video files. This crate extracts frames from videos and computes these hashes for each frame.

Functions

compute_phash

Description: Computes a perceptual hash (pHash) for a video by extracting frames and computing the hash for each extracted frame.

Parameters:

  • video_path (type: &str): Path to the video file.

Returns: Result<Vec<String>, Box<dyn Error>> - A list of perceptual hashes for five frames extracted from the video.

Usage Example:

use videohash::compute_phash;

fn main() -> Result<(), Box<dyn Error>> {
    let video_path = "path/to/video.mp4";
    let phashes = compute_phash(video_path)?;
    for phash in phashes {
        println!("pHash: {}", phash);
    }
    Ok(())
}

Flow:

  1. Extract Frames: The function extracts five frames from the video at evenly spaced intervals.
  2. Compute pHash: Computes the perceptual hash for each of the five frames.
  3. Return: Returns a list of the computed pHashes.

compute_dhash

Description: Computes a difference hash (dHash) for a video by extracting frames and computing the hash for each extracted frame.

Parameters:

  • video_path (type: &str): Path to the video file.

Returns: Result<Vec<String>, Box<dyn Error>> - A list of difference hashes for five frames extracted from the video.

Usage Example:


use videohash::compute_dhash;

fn main() -> Result<(), Box<dyn Error>> {
    let video_path = "path/to/video.mp4";
    let dhashes = compute_dhash(video_path)?;
    for dhash in dhashes {
        println!("dHash: {}", dhash);
    }
    Ok(())
}

Flow:

  1. Extract Frames: The function extracts five frames from the video at evenly spaced intervals.
  2. Compute dHash: Computes the difference hash for each of the five frames.
  3. Return: Returns a list of the computed dHashes.

API Endpoints

/phash

Description: Computes perceptual hashes for five frames extracted from a video.

Parameters:

  • video_url (type: String): URL or path to the video file.

Returns: JSON array of perceptual hashes for the extracted frames.

Example Request:


GET /phash?video_url=path/to/video.mp4

Example Response:

["hash1", "hash2", "hash3", "hash4", "hash5"]

/dhash

Description: Computes difference hashes for five frames extracted from a video.

Parameters:

  • video_url (type: String): URL or path to the video file.

Returns: JSON array of difference hashes for the extracted frames.

Example Request:


GET /dhash?video_url=path/to/video.mp4

Example Response:

["hash1", "hash2", "hash3", "hash4", "hash5"]

Testing

To run tests for the API endpoints, ensure you have a video URL set in your environment variables:


export VIDEO_URL="path/to/video.mp4"
cargo test

License

This crate is licensed under the MIT License. See the LICENSE file for more details.

Contributions

Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.


This README provides a clear explanation of the `compute_phash` and `compute_dhash` functions, their usage, and their role in the crate. It also includes information about the API endpoints and how to test the functionality.

Dependencies

~35–49MB
~831K SLoC