npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

youtube-audio-server

v2.8.2

Published

Easily stream and download audio from YouTube.

Downloads

16

Readme

youtube-audio-server

Easily stream and download audio from YouTube.

Build Status

JavaScript Style Guide

Install

npm install -g youtube-audio-server

Or:

npm install --save youtube-audio-server

Docker image

https://hub.docker.com/r/codealchemist/youtube-audio-server

Search and metadata

IMPORTANT: To be able to search and get video metadata you need to start the app passing your Google App KEY.

Your Google App needs to have the YouTube API enabled.

Login at https://console.cloud.google.com to get this data.

To support this features, YAS should be started like this:

KEY=[YOUR-APP-KEY] yas

If you use YAS programmatically you need to ensure the KEY environment var is set, or since version 2.2.0 you can also set it using the setKey method:

const yas = require('youtube-audio-server')
yas.setKey('YOUR-KEY')

Running on Heroku

To be able to run YAS on Heroku you need to install the ffmpeg buildpack:

heroku buildpacks:add https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git

Command line usage

REST API

Start YAS with yas.

Audio stream

Just hit the server passing a YouTube video id, like:

http://yourServerAddress:port/[videoId]

For example:

http://localhost:4000/HQmmM_qwG4k

This will stream the requested video's audio.

You can play it on an HTML5 audio tag or however you like.

Other endpoints:

  • /cache/[videoId]: Returns the same stream for requested audio until processing finishes. Useful to avoid multiple requests from creating zombie instances of ffmpeg. This happens in Chrome, which makes a document request first and then a media request. The document request makes ffmpeg to start processing but never finishes. Firefox properly loads the audio with just one request and allows seeking.
  • /chunk/[videoId]: Saves mp3 file to disk and returns a stream to it. This allows data chunks to be sent to the client, which will be able to seek across the file. Enables Chrome and VLC, for example, to do seeking.

Get metadata

Use: http://yourServerAddress:port/get/[videoId]

Search

Use: http://yourServerAddress:port/search/[query]/[[pageToken]]

To navigate pages you need to use pageToken which is provided in the results on the root level property nextPageToken.

Change port:

Default is 80.

You can easily change it by starting YAS like:

PORT=8080 yas

Or, you can set the port using args:

yas -p 8080 or yas --port 8080

Download audio

YAS can also be used to easliy download audio.

In this mode, the server is not started.

Usage:

yas --id [youtube-video-id|youtube-video-url] [--file [./sample.mp3]]

With metadata:

yas --id 2zYDMN4h2hY -m

Use -m or --metadata to retrieve and persist metadata as ID3 tags, naming your file with the video title by default.

Saved ID3 tags:

  • title
  • description
  • artist
  • album
  • comment: video URL

Other examples:

yas --id 2zYDMN4h2hY --file ~/Downloads/Music/sample.mp3
yas --id 2zYDMN4h2hY
yas --id https://www.youtube.com/watch?v=2zYDMN4h2hY

NOTE:

FILE defaults to ./[videoId].mp3 when not set.

Alternative method:

If you have a server instance running and you want to use it to download audio, you can do this:

curl [your-server-url]/[youtube-video-id] > sample.mp3

Programatic usage

Yeah, you can also include YAS in your project and use it programatically!

REST API

const yas = require('youtube-audio-server')

// Start listener (REST API).
const port = 7331
yas.listen(port, () => {
  console.log(`Listening on port http://localhost:${port}.`)
})

Download audio

const yas = require('youtube-audio-server')

const id = 'HQmmM_qwG4k' // "Whole Lotta Love" by Led Zeppelin.
const file = 'whole-lotta-love.mp3'
console.log(`Downloading ${id} into ${file}...`)
yas.downloader
  .setFolder('some/folder') // Optionally set a folder for downloaded content.
  .onSuccess(({id, file}) => {
    console.log(`Yay! Audio (${id}) downloaded successfully into "${file}"!`)
  })
  .onError(({ id, file, error }) => {
    console.error(`Sorry, an error ocurred when trying to download ${id}`, error)
  })
  .download({ id, file, cache, metadata })

Params:

  • id: Video ID or URL (HQmmM_qwG4k or https://www.youtube.com/watch?v=HQmmM_qwG4k)
  • file: Output file; defaults to video id or title when metadata is true
  • cache: Use cache
  • metadata: Retrieve and set metadata as ID3 tags

Get video metadata

const yas = require('youtube-audio-server')

yas.get('HQmmM_qwG4k', (err, data) => {
  console.log('GOT METADATA for HQmmM_qwG4k:', data || err)
})

Search

const yas = require('youtube-audio-server')

yas.search({
  query: 'led zeppelin',
  page: null
},
(err, data) => {
  console.log('RESULTS:', data || err)
})

To navigate pages you need to use pageToken which is provided in the results on the root level property data.nextPageToken.

Dependencies

The key dependency for youtube-audio-server is youtube-audio-stream, which depends on ffmpeg, which must be installed at system level, it's not a node dependency!

Install ffmpeg on OSX

brew install ffmpeg

Install ffmpeg on Debian Linux

sudo apt-get install ffmpeg

Testing

Just open the URL of your server instance without specifying a video id.

This will load a test page with an HTML5 audio element that will stream a test video id.

Run npm test to lint everything using StandardJS.

To start the listener and download an audio file use npm run test-run.

You can open the shown URL to test the REST API works as expected.

You can also use npm run test-focus to concentrate on one linting issue at a time with the help of standard-focus.

Enjoy!