Search code examples
videoffmpegmp4h.264

How to correlate MP4 excerpt to frame(s) of video?


I have an MP4 file, and a 16kb excerpt from the file. I would like to know what frame(s) the excerpt matches. I can find the offset of the excerpt, but I believe MP4 encoding is variable-length (so I can't just say "this excerpt is from frame #(offset/framesize)") That also means that my excerpt might not be on a valid frame boundary, so I can't just rename it to "excerpt.mp4" and play it.

What's a good way to figure out what frames correspond to the excerpt?


Solution

  • Run

    ffprobe -show_entries frame=pkt_pos,pkt_size -select_streams v -of csv=p=0:nk=1 -v 0 in.mp4
    

    Its output will be

    48|33720
    54534|76
    53159|974
    54920|381
    34070|19089
    68405|520
    67315|740
    69279|397
    55627|11356
    84445|534
    83571|536
    85314|979
    

    where the first column is the starting byte offset of a frame and the 2nd column is the size. Frame n appears on line # n. You can use whatever shell tools..etc to check, for which line, your discovered offset value is between position and position+size. The position series may not be monotonic, so simply checking the first column isn't reliable.