简体   繁体   English

通过servlet下载视频文件的一部分

[英]Download part of a video file through a servlet

So I have a link to a video online (eg somewebsite.com/myVideo.mkv) and I want to download that video on the server through a servlet. 因此,我有一个在线视频的链接(例如somewebsite.com/myVideo.mkv),我想通过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. 该视频文件已启用CDN,因此基本上任何公共用户都可以将链接放入浏览器,然后它将开始播放。 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. 如果这样做,它将从URL下载整个视频文件,并且视频播放正常。 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. 但是,如果我想在视频downloadFile(URL myURL,long startByte,long endByte)上指定特定的字节范围,则无法播放视频。 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. 我使用了input.skip()函数来向前跳转到startByte,但是我怀疑它跳过了mkv格式的一些重要标头。 That's why the player can't recognize it. 这就是玩家无法识别它的原因。 Does anyone know how to do this in java? 有人知道如何在Java中执行此操作吗?

There are 3 dominant HTTP streaming techologies: Apple HTTP Live Streaming, Microsoft Smooth Streaming, and Adobe HTTP Dynamic Streaming. 有3种主要的HTTP流技术:Apple HTTP Live流,Microsoft Smooth流和Adobe HTTP动态流。 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. 如果从一个大视频文件开始,Apple和Adobe工具将创建许多小文件,每个小文件包含例如10秒钟的视频,以及一个播放列表文件,该文件将为客户提供如何读取它们的线索。 I believe Microsoft tools actually can generate a single file, but it would contain small video fragments internally. 我相信Microsoft工具实际上可以生成一个文件,但是它将在内部包含小的视频片段。

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. 通过HTTP流,“智能”驻留在客户端中,客户端知道如何读取主播放列表文件以及如何避开众多媒体文件或众多媒体文件片段。 The HTTP server only have to serve a file or a file fragment specified by the Range header. HTTP服务器只需要提供Range标头指定的文件或文件片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM