Example 4: Audio and Video Streaming App

This example will show you how to stream audio and video using Node.js , you will find that it is so simple to do so with just a few line of codes.

Let’s try create a new app with some folders:

example under {your_node.js_installed_path}\nodejs\
demo4 under {your_node.js_installed_path}\nodejs\examples\
files under {your_node.js_installed_path}\nodejs\examples\demo4\files\
log under {your_node.js_installed_path}\nodejs\examples\demo4\log\

 

  • Folder “files” is using to store the video and audio files.
  • Folder “log” is using to store log files outputting from console.log().

 

Then, create a js file “app.js” under {your_node.js_installed_path}\nodejs\examples\demo4\ with below codes.

//Declare required library

var express                                         = require(‘express’);

var fs                                                                     = require(‘fs’);

var morgan                                                         = require(‘morgan’);

var FileStreamRotator    = require(‘file-stream-rotator’);

 

var app = express();

var logFolder = __dirname + ‘/log’;

 

fs.existsSync(logFolder) || fs.mkdirSync(logFolder);

 

//Logger details

var logFileStream = FileStreamRotator.getStream({

date_format:   ‘YYYY-MM-DD’,

filename:           logFolder + ‘/demo4_%DATE%.log’,

frequency:        ‘daily’,

verbose:                            false

});

 

//Setup app variables

app.set(‘port’, 3000);

 

app.use(morgan(‘combined’, {stream: logFileStream}));

 

//Setup the index page

app.get(‘/’, function(req, res) {

res.send( [{title: ‘Example 4: Audio and Video Streaming App’, message: ‘This example will show you how to stream audio and video using Node.js , you will find that it is so simple to do so with just a few line of codes.’}] );

});

 

//Setup audio request URL

app.get(‘/audio’, function(req, res) {

//Define the video file

var path = __dirname + ‘/files/’ + ‘sample1.mp3’;              //you may need to rename this file

var stat = fs.statSync(path);

var total = stat.size;

 

console.log(‘path=’+path);

console.log(‘stat=’+stat);

console.log(‘total=’+total);

 

res.writeHead(200, {

‘Content-Length’: total,

‘Content-Type’: ‘audio/mpeg’

});

fs.createReadStream(path).pipe(res);

});

 

//Setup video request URL

app.get(‘/video’, function(req, res) {

//Define the video file

var path = __dirname + ‘/files/’ + ‘sample2.mp4’;              //you may need to rename this file

var stat = fs.statSync(path);

var total = stat.size;

 

console.log(‘path=’+path);

console.log(‘stat=’+stat);

console.log(‘total=’+total);

 

res.writeHead(200, {

‘Content-Length’: total,

‘Content-Type’: ‘video/mp4’

});

fs.createReadStream(path).pipe(res);

});

 

//Setup Listener with Port app.get(‘port’)

app.listen(app.get(‘port’), function () {

console.log(‘Demo 4 listening on port ‘ + app.get(‘port’));

});

 

The above code (or, you can download here) is the whole program of demo4 application with the below 3 requested URLs:

Requested URLs Routing Methods
/ app.get(‘/’, function(req, res)
/audio app.get(‘/audio’, function(req, res)
/video app.get(‘/video’, function(req, res)

 

You may download some sample audio and video files for your testing from:

Audio http://www.sample-videos.com/download-sample-audio.php
Video http://www.sample-videos.com/

 

And then store the files under {your_node.js_installed_path}\nodejs\examples\demo4\files\ .

Ok, everything is ready now. Let’s try to run the application and see what’s happening. You can:

  1. run “cmd”
  2. cd {your_node.js_installed_path}\nodejs\
  3. node examples/demo4/app.js

 

Open a browser with below URLs, it will stream the audio or video.

Streaming Type Requested URLs
Audio http://localhost:3000/audio
Video http://localhost:3000/video

 

So simple, isn’t it?

Please rate this