简体   繁体   English

Javascript 视频 blob 和渐进式下载

[英]Javascript video blob and progressive download

Trying to switch my video streaming web application from using classic <video src="http://myserver/video.mp4"> with URL to blobs to avoid a full download on a simple Ctrl+s on the page.尝试将我的视频流 Web 应用程序从使用带有 URL 的经典<video src="http://myserver/video.mp4">切换到blob,以避免在页面上使用简单的 Ctrl+s 进行完整下载。

Use of XMLHttpRequest seems mandatory to allow creating the blob from the distant video file.使用 XMLHttpRequest 似乎必须允许从远程视频文件创建 blob。

Problem is XMLHttpRequest download the whole file, which is not usable for progressive download use.问题是 XMLHttpRequest 下载整个文件,不能用于渐进式下载使用。

Following code is the simpliest example to load a blob from a distant file.以下代码是从远程文件加载 blob 的最简单示例。

var r = new XMLHttpRequest();
r.onload = function() { // Triggered only when all video is downloaded
    video.prop("src", URL.createObjectURL(r.response));
};

r.open("GET", "http://myserver/video.mp4");
r.responseType = "blob";
r.send();

As the application is for video stream, this method is not usable (unless we want the user to wait X minutes to download the whole file, which is not streaming at all).由于该应用程序用于视频流,因此此方法不可用(除非我们希望用户等待 X 分钟才能下载整个文件,这根本不是流式传输)。

Is there any way to combine blob with progressive download ?有没有办法将 blob 与渐进式下载结合起来?

When using just the src="foo.mp4" method, progressive download should be the default behavior in the browser.当只使用 src="foo.mp4" 方法时,渐进式下载应该是浏览器中的默认行为。 However, the playback experience is dependent upon the actual format of the file.但是,播放体验取决于文件的实际格式。

"Normal" MP4 files are not designed for streaming. “普通” MP4 文件不是为流式传输而设计的。 They are structured such that the data needed to start playback may be split between the start and end of the file.它们的结构使得开始播放所需的数据可以在文件的开头和结尾之间拆分。 A player may thus need to buffer the whole thing to get the data it needs to start playing.因此,播放器可能需要缓冲整个内容以获取开始播放所需的数据。

You could try converting the normal MP4 to a fragmented MP4 using a tool like MP4Box , specifying the "-frag" option.您可以尝试使用MP4Box 之类的工具将普通 MP4 转换为碎片 MP4,指定“-frag”选项。 This rearranges the data in the file so that all the initialization data is at the front and the rest of the file is broken into chunks.这会重新排列文件中的数据,以便所有初始化数据都在前面,而文件的其余部分则被分成块。

A more complex option is to use something like MPEG-DASH.一个更复杂的选择是使用像 MPEG-DASH 这样的东西。 By leveraging MP4Box and dash.js , you can setup a fully fledged adaptive streaming media player.通过利用 MP4Box 和dash.js ,您可以设置一个完全成熟的自适应流媒体播放器。

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

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