1.3.1 • Published 4 years ago

@isolution/youtube-audio-stream v1.3.1

Weekly downloads
11
License
MIT
Repository
github
Last release
4 years ago

youtube-audio-stream

Build Status code-style

Credit

This packaged was originally created by James Kyburz and the repo can be found here: https://github.com/JamesKyburz/youtube-audio-stream

Improvements

  • Module returns a promise instead of a stream. Promise when resolved returns the stream that includes:
    • the stream source(m3u8 url or a Readable stream)
    • the info from ytdl-core. See here for what it contains
    • the fluent-ffmpeg instance
    • a boolean for whether or not it is a livestream
    • an event emitter
  • Livestreams are now supported
  • An example that accepts an arbitrary videoId.
  • Updated Dockerfile that uses Node version 10 and ffmpeg version 4
  • Two new options:
    • bitrate - the bitrate ffmpeg must use to convert the audio stream to. Defaults to 128. See here for possible values.
    • startTime - the time the video should begin. Does not apply to live streams. See here for possible values.

New options example:

// Encode audio at a 192 bitrate and start the audio 30 seconds in
const streamPromise = youtubeAudioStream(uri, { bitrate: 192, startTime: 30 });

Description

To get the youtube video's download stream, the module uses ytdl-core.

To convert to audio, the module uses fluent-ffmpeg.

You will need to have ffmpeg and the necessary encoding libraries and ffmpeg needs to be in the OS's PATH. If you're on OSX, this can be handled easily using Homebrew (brew install ffmpeg). Otherwise visit https://github.com/adaptlearning/adapt_authoring/wiki/Installing-FFmpeg

Installation

Via npm:

npm install @isolution/youtube-audio-stream

Usage

Here is an example that creates an express server with one route and streams the audio to the response. To hear the audio for a specific video:

  1. Get a videoId
  2. Go to http://localhost:3000/:videoId
const express = require('express');
const youtubeAudioStream = require('@isolution/youtube-audio-stream');
const app = express();
const port = 3000;

app.get('/:videoId', (req, res) => {
  const requestUrl = `http://youtube.com/watch?v=${req.params.videoId}`;
  const streamPromise = youtubeAudioStream(requestUrl);
  streamPromise
    .then(stream => {
      stream.emitter.on('error', err => {
        console.log(err);
      });
      stream.pipe(res);
    })
    .catch(err => {
      console.log(err);
    });
});

app.listen(port, () => {
  console.log(`Server started on ${port}`);
});

Error Handling

There are two places where you need to handle errors. They are:

  1. Promise rejected using .catch
  2. Promise resolved using stream.emitter to listen for error events

Testing

Testing locally

This package comes with a simple example for testing. This can be run with the command npm run example, which will start a basic http server that serves two routes, first one sends an html file to http://localhost:3000/ and the second one streams audio to the response using this package.

Note: After selecting "OK", the play button has to be clicked for audio to start playing because most browsers disable autoplay.

Testing inside a docker container

You can test this module without the need to have ffmpeg locally installed by doing it inside a docker container.

To build the docker image:

docker build --rm -f "Dockerfile" -t youtube-audio-stream:latest .

To run the test:

docker run --rm -d -p 3000:3000/tcp youtube-audio-stream:latest
1.3.1

4 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago