airplay-js没有开始流式传输

时间:2014-08-01 01:44:13

标签: node.js ffmpeg

我正在通过airplay协议编写本地文件流和字幕支持,为此我使用ffmpeg并且该过程工作正常,但主要问题是当我尝试在刻录后开始流式传输副标题,airplay不起作用,但当我评论做燃烧的线条工作正常。我究竟做错了什么?这是代码。

var fileSource = './source.mp4';
var subtitleSource = './source.srt';

var http = require('http'),
fs = require('fs'),
util = require('util'),
OS = require('os'),
spawn = require('child_process').spawn,
airplay = require('airplay-js');

var browser = airplay.createBrowser();

var tmpDir = OS.tmpdir();
var tmpFile = tmpDir + '/localTV.mp4';

var sourceSize;
var tmpFileSize;

function init(){
console.log('Starting LocalTV');
console.log('Burning Subtitle into ' + fileSource);

var stat = fs.statSync(fileSource);
sourceSize = stat.size;

fs.exists(tmpFile, function(exists){
    if(exists) fs.unlink(tmpFile)
});

var ffmpeg = spawn('./ffmpeg', [
    '-i', fileSource,
    '-sub_charenc', 'CP1252',
    '-i', subtitleSource,
    '-map', '0:v',
    '-map', '0:a',
    '-c', 'copy',
    '-map', '1',
    '-c:s:0', 'mov_text',
    '-metadata:s:s:0', 'language=esp',
    tmpFile
]); 

ffmpeg.stderr.pipe(process.stdout);

ffmpeg.on('close', function(code){
    if(code == 0){
        console.log('Burning subtitle finished');
        initServer();
    }
})

ffmpeg.on('error', function(error){
    console.log(error, 'ffmpeg error');
});

//  initServer();

return false;   
}

function initServer(){

http.createServer(function (req, res) {

    var path = tmpFile;
    var stat = fs.statSync(path);
    var total = stat.size;

    if (req.headers['range']) {
        var range = req.headers.range;
        var parts = range.replace(/bytes=/, "").split("-");
        var partialstart = parts[0];
        var partialend = parts[1];

        var start = parseInt(partialstart, 10);
        var end = partialend ? parseInt(partialend, 10) : total-1;
        var chunksize = (end-start)+1;

        var file = fs.createReadStream(path, {start: start, end: end});
        res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': 'video/mp4' });
        file.pipe(res);

      }else{        

        res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/mp4' });
        fs.createReadStream(path).pipe(res);
    }

}).listen(1337, '192.168.0.100', function(){
    console.log('HTTP Server Started');
    startStream();
});

return false;       
}

function startStream(){ 
console.log('Streaming to AppleTV');

var stat = fs.statSync(tmpFile);
tmpFileSize = stat.size;    

//  if(tmpFileSize >= sourceSize){
    browser.on('deviceOn', function(device) {
        device.play('http://192.168.0.100:1337', 0, function(status){
            console.info(status, 'Playing video');
        });
    });

    browser.on('error', function(err){
        console.log(err, 'Error airplay');
    })

    browser.start();
//  }else{
//      console.log('Temp file minor size that source, something is wrong');
//  }

return false;       
}

init();

0 个答案:

没有答案