samedi 18 avril 2015

Spawn, play video from stdout to stdin

Using nodejs, I'm trying to play a youtube clip in MPlayer by streaming video from youtube-dl. MPlayer stops playing after a few seconds as a standard, and whenever I'm trying to switch video (demonstrated by the timeout) everything crashes.


I feel as if I need some help to sort this out. Not sure what's going wrong. I would also prefer it if I could have one process with MPlayer and not having to restart it each time.


One error upon calling player.play(...) again:



events.js:85
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at exports._errnoException (util.js:746:11)
at WriteWrap.afterWrite (net.js:775:14)


Code:



var spawn = require('child_process').spawn;
var player = new Player();
player.play('http://ift.tt/WvL51I');

var testingPlayNext = setTimeout(function() {
player.play('http://ift.tt/WvL51I');
}, 3000);


function Player() {
this.process = {
dl: '',
player: ''
};
};

Player.prototype.play = function(url) {
if (typeof this.process !== 'undefined' && this.process.dl) {
this.stop();
}

this.process.player = spawn('mplayer', ['-']);
this.process.dl = spawn('youtube-dl', [url, '-o', '-']);

var that = this;
this.process.dl.stdout.on('data', function(data) {
that.process.player.stdin.write(data);
});
};

Player.prototype.stop = function() {
for (var prop in this.process) {
try {
this.process[prop].kill();
} catch (e) {
console.log('Error killing processes.', e);
}
}
};

process.on('exit', function() {
player.stop();
});

Aucun commentaire:

Enregistrer un commentaire