Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using chunks of MP4 file for thumbnails

Tags:

video

ffmpeg

mp4

I want to extract single frames (as thumbs) from videos, without having to download the full video file. I would like to just read the mp4 header (the info should be in the moov atom) and then download the required byte ranges. Afaik thats what browsers (HTML5 video) do when you skip to an unbuffered part

I already looked at: How can HTML5 video's byte-range requests (pseudo-streaming) work? but i can't figure out how to use it with ffmpeg for example.

Thanks a lot!

like image 386
Moritz Mahringer Avatar asked Jan 22 '26 09:01

Moritz Mahringer


1 Answers

Here's how a general algorithm might operate if you were absolutely determined to make this approach work:

  • download the first kilobyte of the MP4 file
  • make sure the moov atom is up front; determine moov atom's length and make another request to fetch the rest of the atom
  • dig through the moov atom to find the video trak atom; dig into that atom to find the following atoms: stsd, stss, stco/co64, and stsz
  • the stsd will give you initialization information required to feed into the H.264 video decoder
  • the stss atom gives you a list of all the sync samples (keyframes); these can be decoded independently and would be ideal for your thumbnailing prospects
  • when you know which frames are keyframes, courtesy of the stss atom, you can cross reference with the stco or co64 atoms (a trak will have one or the other) in order to find the absolute file location, and the stsz atom, which will tell you exactly how many bytes are in the frame

With all of this information combined, you should be able to download and decode (and thus resize and re-compress for thumbnailing) just the keyframes of an MP4 video.

like image 184
Multimedia Mike Avatar answered Jan 26 '26 19:01

Multimedia Mike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!