MrSunshine
MrSunshine

Reputation: 31

Java - Get snippet from video of URL

Lets say I have this video url : http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4 and I need to download just a small playable snippet from this video (e.g. 00:24 - 00:30).

How should I do this in java? And I don't want to download the entire video and then cut it.

I already looked at the network tab of chrome and read the request and response headers but couldn't get much sense of it, except:

But I can't just choose any range because it would make the video not playable right?

So how does the browser know which range it should get if I click on 00:24?

Upvotes: 0

Views: 234

Answers (1)

Offbeatmammal
Offbeatmammal

Reputation: 8238

assuming you're working with .mp4 containers, the mp4 file format contains a hierarchical structure of 'boxes' (aka "atoms'). The magic for fast seeking lies in the moov atom which is why when encoding a video for web it's always best to optimize the structure of the file and relocate that to the front so that the browser has access to the metadata as the very first thing it downloads.

moov Movie box which is the container for all metadata Each moov has have a mvhd (Movie header box) It can contains N trak box(es). Each trak box contains media specific meta data information Usually, it will have 2 tracks (video and audio) More importantly, it contains sample information such as stsd, stts, stsz stsc, stco, etc...

stts - Time-To-Sample (stts) box. This box contains the time of every frame (in a compact fashion). From here you can find the 'chunk' that contains the frame using the Sample-to-Chunk (stsc) atom. And finally the Chunk offset atom (stco) gives you the byte offset into the file.

Projects like MP4Parser or Xuggler can get you started on processing the MP4 container yourself (have samples on reading the underlying MP4 structure), but it's not a trivial undertaking - sadly there doesn't seem to be a comprehensive MP4 API toolkit for Java

Upvotes: 1

Related Questions