Search code examples
node.jsaudio-streamingid3

Node - Attach ID3 music metadata to audio stream


I am using node+express an audio stream to the browser using the stream.pipe(res) function. I want to add metadata for the music that is being streamed such as an artist name or album art. Does one use response headers to send this information? How can I do it?

var url = 'https://www.youtube.com/watch?v='+videoId;

var audio = ytdl(url, {
    filter: 'audioonly'
});

audio.on('response', function(data) {

    ...

    res.writeHead(206, {
        'Content-Type': 'audio/mpeg',
        'Content-Range': ...,
        'Content-Length': ...,
        'Content-Disposition': 'inline; filename="' + req.query.track + ' - ' + req.query.artist + '.mp3"',
        'Accept-Ranges': 'bytes',

        // Add data like this?
        'Content-???': 'Artist=' + artist + ',Album=' + album
    });
});

audio.pipe(res);

I am using the node-ytdl-core module for the audio source.


Solution

  • Something I found is using

    res.writeHead(200, {
      'Content-Type': 'audio/mpeg3',
      'Transfer-Encoding': 'chuncked',
      'icy-br': '##',
      'ice-audio-info': 'bitrate=128;samplerate=22050',
      'icy-genre': 'Alternative',
      'icy-name': "Dj-Radio",
      'icy-description': "A NodeJS mp3 audio streamer",
      'icy-url': "http://localhost:8080",
      'Cache-Control': "no-cache",
      'Connection': 'Keep-Alive'
    });
    

    However since your are sending a header, and you can only do that once, if your streaming multiple songs in one stream and you want to have timed metadata with the start of each some, you will run into a problem. Because headers can only be sent one, and have to be before the body for else it isn't a head.

    Here is a repo where I made an internet radio (only code sorry if it isn't neat) > https://github.com/Hobgoblin101/Dj-Radio