This module streams youtube using ytdl to get the youtube download stream.
To convert to audio the module fluent-ffmpeg is used.
You will need to have ffmpeg and the necessary encoding libraries installed, as well as in your PATH. If you're on OSX, this can be handled easily using Homebrew (brew install ffmpeg
).
- With npm, run
npm install youtube-audio-stream
var youtubeStream = require('youtube-audio-stream')
Here is an example that:
- queries for a video by video ID
- Retrieves the audio via this package
- pipes it to
res
var getAudio = function (req, res) {
var requestUrl = 'http://youtube.com/watch?v=' + req.params.videoId
try {
youtubeStream(requestUrl).pipe(res)
} catch (exception) {
res.status(500).send(exception)
}
}
const stream = require('youtube-audio-stream')
const url = 'http://youtube.com/watch?v=34aQNMvGEZQ'
const decoder = require('lame').Decoder
const speaker = require('speaker')
stream(url)
.pipe(decoder())
.pipe(speaker())
This package comes with a simple example for testing. This can be run with the command npm test
, which will then serve the example at localhost:3000
. The example consists of an <audio>
component whose source is retrieved via this package.
You can test this module without the need o have ffmeg locally installed doing it inside a container.
To build the Docker image:
docker build . -t youtube-audio-stream-test
To run the test:
docker run --rm -it -p 3000:3000 youtube-audio-stream-test