HLS Streaming using node JS



我正在尝试使用node.js流式传输HLS内容。不知何故,它不起作用。如果有人帮助我,那将有很大帮助。

问题:-尝试从节点提供 HLS 内容.js(不是实时流,而是一组 .ts 文件和 .m3u8 播放列表或者换句话说,VOD 内容

文件夹结构

stream_test
|--- app.js
|--- node_modules
|--- streamcontent
        |--- test.m3u8
        |--- segment0.ts
        |--- segment1.ts
        .
        .
        .
        |--- segment127.ts

我的app.js看起来像这样

var http = require('http'),
    url = require('url'),
    path = require('path'),
    fs = require('fs');
var mimeTypes = {
    "html": "text/html",
    "jpeg": "image/jpeg",
    "jpg": "image/jpeg",
    "png": "image/png",
    "js": "text/javascript",
    "css": "text/css",
    "ts": "video/MP2T",
    "m3u8": "application/vnd.apple.mpegurl"};
http.createServer(function(req, res) {
    var uri = url.parse(req.url).pathname;
    var filename = path.join(process.cwd(), unescape(uri));
    var stats;
    console.log('filename '+filename);
    try {
        stats = fs.lstatSync(filename); // throws if path doesn't exist
    } catch (e) {
        res.writeHead(404, {'Content-Type': 'text/plain'});
        res.write('404 Not Foundn');
        res.end();
        return;
    }

    if (stats.isFile()) {
        // path exists, is a file
        var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
        res.writeHead(200, {'Content-Type': mimeType} );
        var fileStream = fs.createReadStream(filename);
        fileStream.pipe(res);
    } else if (stats.isDirectory()) {
        // path exists, is a directory
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.write('Index of '+uri+'n');
        res.write('TODO, show index?n');
        res.end();
    } else {
        // Symbolic link, other?
        // TODO: follow symlinks?  security?
        res.writeHead(500, {'Content-Type': 'text/plain'});
        res.write('500 Internal server errorn');
        res.end();
    }
}).listen(8000);

test.m3u8 看起来像这样

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:19
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:12.595922,
segment0.ts
.
.
.

我使用 ffmpeg 来创建片段和 palylist

ffmpeg -i video-a.mp4  -c:a libmp3lame -ar 48000 -ab 64k  -c:v libx264   -b:v 128k -flags -global_header -map 0 -f segment  -segment_list test.m3u8 -segment_time 30 -segment_format mpegts segment_%05d.ts

测试场景:-如果从 Apache 提供,则工作正常,如果从节点提供则不能。

测试工具:-VNC 播放器

根据Brad的想法,我能够使用express.static来做到这一点。解决方案是这样的。

应用程序.js是这样更改

var express = require('express');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname,'streamcontent')));
app.listen(8000);
console.log('Listening on Port 8000');

并且.m3u8播放列表更改为此

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:19
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:12.595922,
http://localhost:8000/segment0.ts
#EXTINF:10.135133,
http://localhost:8000/segment1.ts
#EXTINF:11.511511,
http://localhost:8000/segment2.ts

仅此而已。瞧!!!

相关内容

  • 没有找到相关文章

最新更新