首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Node.js将视频文件流式传输到html5视频播放器,以便视频控件继续工作?

使用Node.js将视频文件流式传输到html5视频播放器,以便视频控件继续工作?
EN

Stack Overflow用户
提问于 2014-07-27 06:32:08
回答 3查看 139.8K关注 0票数 109

Tl;Dr -问题:

使用Node.js 将视频文件流式传输到html5视频播放器以使视频控件继续工作的正确方法是什么?

我认为这与处理头部的方式有关。不管怎样,这是背景信息。代码有点长,但是,它很简单。

使用Node将小视频文件流式传输到HTML5视频非常容易

I learned how to stream small video files to an HTML5 video player very easily.使用此设置,控件可以在我没有任何工作的情况下工作,并且视频流完美无瑕。带有示例视频的完整工作代码的工作副本是here, for download on Google Docs

客户端:

<html>
  <title>Welcome</title>
    <body>
      <video controls>
        <source src="movie.mp4" type="video/mp4"/>
        <source src="movie.webm" type="video/webm"/>
        <source src="movie.ogg" type="video/ogg"/>
        <!-- fallback -->
        Your browser does not support the <code>video</code> element.
    </video>
  </body>
</html>

服务器:

// Declare Vars & Read Files

var fs = require('fs'),
    http = require('http'),
    url = require('url'),
    path = require('path');
var movie_webm, movie_mp4, movie_ogg;
// ... [snip] ... (Read index page)
fs.readFile(path.resolve(__dirname,"movie.mp4"), function (err, data) {
    if (err) {
        throw err;
    }
    movie_mp4 = data;
});
// ... [snip] ... (Read two other formats for the video)

// Serve & Stream Video

http.createServer(function (req, res) {
    // ... [snip] ... (Serve client files)
    var total;
    if (reqResource == "/movie.mp4") {
        total = movie_mp4.length;
    }
    // ... [snip] ... handle two other formats for the video
    var range = req.headers.range;
    var positions = range.replace(/bytes=/, "").split("-");
    var start = parseInt(positions[0], 10);
    var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
    var chunksize = (end - start) + 1;
    if (reqResource == "/movie.mp4") {
        res.writeHead(206, {
            "Content-Range": "bytes " + start + "-" + end + "/" + total,
                "Accept-Ranges": "bytes",
                "Content-Length": chunksize,
                "Content-Type": "video/mp4"
        });
        res.end(movie_mp4.slice(start, end + 1), "binary");
    }
    // ... [snip] ... handle two other formats for the video
}).listen(8888);

但此方法仅限于小于1 1GB的文件。

使用fs.createReadStream流式传输(任何大小)的视频文件

通过利用fs.createReadStream(),服务器可以读取流中的文件,而不是一次将其全部读取到内存中。这听起来像是做事情的正确方法,而且语法非常简单:

服务器代码片段:

movieStream = fs.createReadStream(pathToFile);
movieStream.on('open', function () {
    res.writeHead(206, {
        "Content-Range": "bytes " + start + "-" + end + "/" + total,
            "Accept-Ranges": "bytes",
            "Content-Length": chunksize,
            "Content-Type": "video/mp4"
    });
    // This just pipes the read stream to the response object (which goes 
    //to the client)
    movieStream.pipe(res);
});

movieStream.on('error', function (err) {
    res.end(err);
});

这样就可以很好地播放视频了!但视频控件不再起作用。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24976123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档