Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream Youtube video via Node.js server

I am trying to create a simple web server in Node.js which would call Youtube video API to get a video and stream it to the client (after some checks, but that is not I am concerned about at the moment) as it retrieves it from Youtube.

To start with I am trying to do it in two parts, download and save to disk and then stream it from disk. While I am able to stream a file from disk to client, I could not get download and saving part working. Here is code for that

var request = require('request');
var http = require('http');
var fs = require('fs');

http.createServer(function(req,res)
{
    request('http://www.youtube.com/embed/XGSy3_Czz8k').pipe(fs.createWriteStream('testvideo.mp4'));
}).listen(80, '127.0.0.1');

        console.log('Server running at http://127.0.0.1:80/');

I thought the request module could simply pipe output to file. Could someone please suggest what am I doing wrong here?

Any guidance on how to get the video from youtube and do a simultaneous streaming to the client while the video is being retrieved from youtube?

like image 776
vibhu Avatar asked Aug 05 '15 19:08

vibhu


People also ask

Is node JS suitable for video streaming?

Node. js is better option for developing video (or audio) streaming website. I have done a peer to peer video chat system which worked great but it was hybrid (on node. js + PHP) as I was more famier with PHP back then so I used node.

How do I stream data in node JS?

Chaining the Streams Chaining is a mechanism to connect the output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations. Now we'll use piping and chaining to first compress a file and then decompress the same. Verify the Output.


1 Answers

I tried to do this and it works.

var request = require('request');
var http = require('http');
var fs = require('fs');

http.createServer(function(req,res)
{
    var x = request('http://www.youtube.com/embed/XGSy3_Czz8k')
    req.pipe(x)
    x.pipe(res)
}).listen(1337, '127.0.0.1');

        console.log('Server running at http://127.0.0.1:1337/');
like image 153
vibhu Avatar answered Oct 17 '22 14:10

vibhu