Videojs无法从节点http服务器中寻找超出缓冲行的flv

时间:2016-05-27 06:00:57

标签: node.js video-streaming video.js flv

我想创建一个支持flv的播放器,所以我必须通过节点http服务器传输视频文件。对于mp4,它完美无缺。但是,我不知道为什么,对于flv,当我点击缓冲线时,浏览器不会发送另一个请求。
  服务器基于Streaming a video file to an html5 video player with Node.js so that the video controls continue to work?
  您可以通过启动服务器并打开http://localhost:8080/?path=E:/1.flv

来尝试
"use strict";
var fs = require("fs");
var http = require("http");
var url = require("url");
var path = require("path");
var MIMETypes = new Map();
MIMETypes.set(".flv", "video/x-flv").set(".mp4", "video/mp4").set(".avi", "video/x-msvideo");
exports.server = http.createServer(function (req, res) {
    var query = url.parse(req.url, true).query;
    if (!query.path) {
        res.end("Empty query");
    }
    else if (query.path && !query.getStream) {
        // return a page.
        res.end(`
<head>
  <link href="http://vjs.zencdn.net/5.9.2/video-js.css" rel="stylesheet">
</head>
<body>
  <video id="my-video" class="video-js" controls preload="auto" width="640" height="264" data-setup="{}">
    <source src="http://localhost:8080/?getStream=true&path=${query.path}" type='${MIMETypes.get(path.extname(query.path))}'>
  </video>
  <script src="http://vjs.zencdn.net/5.9.2/video.js"></script>
</body>`);
    }
    else {
        var mimetype = MIMETypes.get(path.extname(query.path));
        fs.stat(query.path, function (err, stats) {
            if (err) {
                console.log("fail to read file stat", err);
                res.end(JSON.stringify(query));
            }
            else {
                var range = req.headers.range;
                if (!range) {
                    res.writeHead(206, {
                        "Accept-Ranges": "bytes",
                        "Content-Length": stats.size,
                        "Content-Type": mimetype
                    });
                    var stream = fs.createReadStream(query.path);
                    stream.pipe(res);
                }
                else {
                    var positions = range.replace(/bytes=/, "").split("-");
                    var start = parseInt(positions[0], 10);
                    var total = stats.size;
                    var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
                    var chunksize = (end - start) + 1;
                    res.writeHead(206, {
                        "Content-Range": "bytes " + start + "-" + end + "/" + total,
                        "Accept-Ranges": "bytes",
                        "Content-Length": chunksize,
                        "Content-Type": mimetype
                    });
                    var stream_1 = fs.createReadStream(query.path, { start: start, end: end })
                        .on("open", function () {
                            stream_1.pipe(res);
                        }).on("error", function (err) {
                            res.end(err);
                        });
                }
            }
        });
    }
});
exports.server.listen(8080, "localhost");

0 个答案:

没有答案