2014-02-19 18 views
5

Ich versuche, HLS-Inhalt mit node.js zu streamen. Und irgendwie funktioniert es nicht. Es wird eine große Hilfe sein, wenn mir jemand hilft.HLS-Streaming mit Knoten JS

Problem: - Versuch HLS Inhalt von node.js zu dienen (nicht Strom leben, aber eine Reihe von .ts Dateien und .m3u8 Playlist, oder mit anderen Worten VODMaterial)

Ordnerstruktur

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

Mein sieht app.js wie diese

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 Found\n'); 
     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 error\n'); 
     res.end(); 
    } 

}).listen(8000); 

Die test.m3u8 Mag ich ffmpeg die Segmente verwendet

#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 
. 
. 
. 

schaut zu erstellen und 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 

-Test Scenraio: - fein funktioniert, wenn von Apache bedient, nicht, wenn vom Knoten bedient.

Test Tool: - VNC Spieler

+2

Warum lassen Sie 'express.static' das nicht für Sie tun? – Brad

+0

"Ich benutzte" connect.static "früher, aber es funktionierte nicht. Jedoch mit" express.static "scheint es zu funktionieren. Danke für die Idee." – Tirtha

Antwort

3

Mit der Idee von Brad, ich war in der Lage, dies mit express.static zu tun. Hier geht die Lösung.

Die app.js ist wie diese geändert

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'); 

und die .m3u8 Playlist geändert dieser

#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 

Und das ist es. Voila !!!

+0

funktioniert das in Chrome? – bitoiu

+0

Nein. M3u8 Streams funktionieren nicht in Chrom. Ich habe VLC-Player zum Testen in Windows verwendet. Arbeitete perfekt in Mac-Safari. Hat nicht mit Win-Safari funktioniert. – Tirtha