Git Product home page Git Product logo

Comments (1)

Mzyxptlk avatar Mzyxptlk commented on August 17, 2024

I ran into another issue with the above approach: for some WEBM and MKV videos, pHash returns 0 hashes.

Cause: the frame count calculated in the above snippet is only an estimate. However, in ph_getKeyFramesFromVideo(), this estimate is treated as solid fact. When that frame count is an overestimation, then the last few values in the dist array are never written to, because the corresponding frames don't exist. If one of those places happens to hold a very small or even negative number, the associated (non-existent!) frame is selected as one of the frames to hash. ReadFrames() throws no error when it is told to decode a frame past the end of the video, it simply returns 0, to indicate it decoded no frames. Thus, if a non-existent frame is selected for hashing, no frame is read, and if that's the only frame selected for hashing, then ph_getKeyFramesFromVideo() returns 0 frames, and ph_dct_videohash() calculates 0 hashes, all without any errors being reported.

If the frame count is an overestimation, the last portion of the video is simply ignored, which is only slightly less bad.

Solution: allocate space in the dist array for 20% more frames than the header estimates, and when the end of the file has been reached, reduce the actual frame count as appropriate.

--- a/src/pHash.cpp
+++ b/src/pHash.cpp
@@ -354,6 +354,7 @@ CImgList<uint8_t> *ph_getKeyFramesFromVideo(const char *filename) {
     if (N <= 0) {
         return NULL;
     }
+    N *= 1.2f;
 
     float frames_per_sec = 0.5 * fps(filename);
     if (frames_per_sec < 0) {
@@ -413,6 +414,8 @@ CImgList<uint8_t> *ph_getKeyFramesFromVideo(const char *filename) {
     } while ((nbread >= st_info.nb_retrieval) && (k < nbframes));
     vfinfo_close(&st_info);
 
+    nbframes = k;
+
     int S = 10;
     int L = 50;
     int alpha1 = 3;

An alternative approach to the first half of the patch would be to change the snippet in the OP to overestimate by 20%, instead of doing it here. This would avoid some overallocation when the frame count is not an estimate, such as for MP4 containers, but would tie GetNumberOfVideoFrames() and ph_getKeyFramesFromVideo() more closely together, which may not be what you want.

from phash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.