Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
streaming mp3 using nodejs
var http = require('http'),
fileSystem = require('fs'),
path = require('path')
util = require('util');
http.createServer(function(request, response) {
var filePath = 'path_to_file.mp3';
var stat = fileSystem.statSync(filePath);
response.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': stat.size
});
var readStream = fileSystem.createReadStream(filePath);
// We replaced all the event handlers with a simple call to util.pump()
util.pump(readStream, response);
})
.listen(2000);
@njoyard

This comment has been minimized.

Copy link

@njoyard njoyard commented Jul 26, 2013

Note that util.pump() is now deprecated. Use readStream.pipe(writeStream) instead (see http://nodejs.org/api/util.html#util_util_pump_readablestream_writablestream_callback)

@allanchriswajega

This comment has been minimized.

Copy link

@allanchriswajega allanchriswajega commented Aug 20, 2013

nice stuff

@bigfish

This comment has been minimized.

Copy link

@bigfish bigfish commented Dec 13, 2013

missing comma after require('path')

maybe there's something to be said for the comma first style after all!

@daslicht

This comment has been minimized.

Copy link

@daslicht daslicht commented Mar 14, 2014

How would you start the playback at a specific point of time ? please

@daslicht

This comment has been minimized.

Copy link

@daslicht daslicht commented Mar 14, 2014

@njoyyard: dou you please like to post an example how this would look like , please?

var http = require('http'),
    fileSystem = require('fs'),
    path = require('path'),
    util = require('util');

http.createServer(function(request, response) {
    var filePath = 'AnsolasChillloop.mp3';
    var stat = fileSystem.statSync(filePath);

    response.writeHead(200, {
        'Content-Type': 'audio/mpeg',
        'Content-Length': stat.size
    });

    var readStream = fileSystem.createReadStream(filePath);

    var reader = getReadableStreamSomehow();
    var writeStream = ???; //???
    readStream.pipe(writeStream);
})
.listen(2000);
@daslicht

This comment has been minimized.

Copy link

@daslicht daslicht commented Mar 14, 2014

Okay here we go that seems to work:

var http = require('http'),
    fileSystem = require('fs'),
    path = require('path'),
    util = require('util');

http.createServer(function(request, response) {
    var filePath = 'AnsolasChillloop.mp3';
    var stat = fileSystem.statSync(filePath);

    response.writeHead(200, {
        'Content-Type': 'audio/mpeg',
        'Content-Length': stat.size
    });

    var readStream = fileSystem.createReadStream(filePath);
    readStream.pipe(response);
})
.listen(2000);

Next quest:
How would you start the playback at a specific point of time ?

@riston

This comment has been minimized.

Copy link

@riston riston commented Mar 25, 2014

var http = require('http'),
    fs   = require('fs'),
    filePath = '/home/risto/Downloads/oleg.mp3',
    stat = fs.statSync(filePath);

http.createServer(function(request, response) {

    response.writeHead(200, {
        'Content-Type': 'audio/mpeg',
        'Content-Length': stat.size
    });

    // We replaced all the event handlers with a simple call to util.pump()
    fs.createReadStream(filePath).pipe(response);
})
.listen(2000);
@JanHalozan

This comment has been minimized.

Copy link

@JanHalozan JanHalozan commented Jul 20, 2015

Has anyone managed to start playback at a specific point yet?

@daslicht

This comment has been minimized.

Copy link

@daslicht daslicht commented Mar 7, 2016

Not me

@daslicht

This comment has been minimized.

Copy link

@daslicht daslicht commented Mar 14, 2016

@daslicht

This comment has been minimized.

Copy link

@daslicht daslicht commented Mar 18, 2016

Ok now I can start a stream at anytime I like , but just by byte offset :/

@vgrafe

This comment has been minimized.

Copy link

@vgrafe vgrafe commented May 18, 2016

@daslicht can you share the solution you found to start playback at a specific point?

@edinsoncs

This comment has been minimized.

Copy link

@edinsoncs edinsoncs commented Aug 18, 2016

good (Y)

@qodesmith

This comment has been minimized.

Copy link

@qodesmith qodesmith commented Sep 3, 2016

@daslicht I'd like to see how you started playback at a specific point as well

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Dec 30, 2016

I'm looking to do the same, is there a way to start the playback at a specific point in time? Any help would be greatly appreciated.

@JhonatanHern

This comment has been minimized.

Copy link

@JhonatanHern JhonatanHern commented Feb 20, 2017

It was very useful, I'm working in a personal project and this code was a bless.

@zoecarver

This comment has been minimized.

Copy link

@zoecarver zoecarver commented Feb 20, 2017

thanks! super helpful code

@jconway45

This comment has been minimized.

Copy link

@jconway45 jconway45 commented Nov 11, 2018

var http = require('http'),
    url = require('url'),
    fs   = require('fs'),
    filePath = '/home/risto/Downloads/oleg.mp3',
    stat = fs.statSync(filePath);

http.createServer(function(request, response) {
    var queryData = url.parse(request.url, true).query;
    const skip = typeof(queryData.skip) == 'undefined' ? 0 : queryData.skip;
    const startByte = stat.size * skip;

    response.writeHead(200, {
        'Content-Type': 'audio/mpeg',
        'Content-Length': stat.size - startByte
    });

    // We replaced all the event handlers with a simple call to util.pump()
    fs.createReadStream(filePath, {start:startByte}).pipe(response);
})
.listen(2000);
@jconway45

This comment has been minimized.

Copy link

@jconway45 jconway45 commented Nov 11, 2018

This gives you skipping ^^

@image72

This comment has been minimized.

Copy link

@image72 image72 commented Nov 19, 2018

Video stream with range.
via: https://medium.com/@daspinola/video-stream-with-node-js-and-html5-320b3191a6b6

var http = require('http'),
    url = require('url'),
    fs   = require('fs'),
    filePath = '/home/risto/Downloads/oleg.mp4',
    stat = fs.statSync(filePath);

http.createServer(function(request, response) {        
    const fileSize = stat.size;
    const range = request.headers.range;
    if (range) {
      const parts = range.replace(/bytes=/, "").split("-");
      const start = parseInt(parts[0], 10);
      const end = parts[1] 
        ? parseInt(parts[1], 10)
        : fileSize - 1;
      const chunksize = (end - start) + 1;
      const readStream = fs.createReadStream(filePath, { start, end });
      const head = {
        'Content-Range': `bytes ${start}-${end}/${fileSize}`,
        'Accept-Ranges': 'bytes',
        'Content-Length': chunksize,
        'Content-Type': 'video/mp4',
      };
      response.writeHead(206, head);
      readStream.pipe(response);
    } else {
      const head = {
        'Content-Length': fileSize,
        'Content-Type': 'video/mp4',
      };
      response.writeHead(200, head);
      fs.createReadStream(filePath).pipe(response);
    }
})
.listen(2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment