0

So I have a link to a video online (e.g. somewebsite.com/myVideo.mkv) and I want to download that video on the server through a servlet. The video file has CDN enabled, so basically any public user can just put the link into the browser and it will start playing. This is the code I have so far.

downloadFile(URL myURL){
   InputStream input = myURL.openStream();
   File video = new File ("/path-to-file/" + myURL.getFile());
   FileOutputStream output = new FileOutputStream(output);

   byte[] buffer = new byte[1024];
   int read;

   // Write full range.
   while ((read = input.read(buffer)) > 0){
     output.write(buffer, 0, read);
   }

   output.close();
   input.close()
}

If I do that, it would download the entire video file from the URL and the video playback fine. However, if I want to specify a specific byte range on the video downloadFile(URL myURL, long startByte, long endByte), the video doesn't playback. I used the function input.skip() to skip forward to the startByte but I suspect it skips over some important header of the mkv format. That's why the player can't recognize it. Does anyone know how to do this in java?

Olaf
  • 6,249
  • 1
  • 19
  • 37
GiangP
  • 31
  • 6
  • 1
    The media files don't work that way. Just look at them as black boxes. What you are probably looking for is converting your video file to some streaming format and allowing your client to stream from whatever point they want. – Olaf Aug 06 '13 at 19:46
  • What you said makes sense but honestly I don't even know how to start with it. I thought when I did the url.openStream(), it's converting it into a streaming format. – GiangP Aug 06 '13 at 20:06

1 Answers1

0

There are 3 dominant HTTP streaming techologies: Apple HTTP Live Streaming, Microsoft Smooth Streaming, and Adobe HTTP Dynamic Streaming. Each of these technologies provides tools to convert video to corresponding format. If you start with one large video file, the Apple and Adobe tools would create a number of small files containing, say, 10 sec of video each, and a playlist file that would give the client a clue how to read them. I believe Microsoft tools actually can generate a single file, but it would contain small video fragments internally.

With the HTTP streaming, the "intelligence" lives in the client that knows how to read the master playlist file and how to get around either numerous media files or numerous media file fragments. The HTTP server only have to serve a file or a file fragment specified by the Range header.

Olaf
  • 6,249
  • 1
  • 19
  • 37