Code Monkey home page Code Monkey logo

yt-streaming-api's Introduction

TypeScript

YouTube streaming API

This is an open source backend that can be deployed to any infrastructure that can run Node.js. It's intended to provide a seamless MP3 audio streaming experience with few other functionalities such as search.

This API works on top of the Express web application framework. It's used as a standalone application and is easily expandable due to it's modular structure.

Requirements

Install Node.js

Install yarn

  • Yarn package manager

Install FFmpeg

  • FFmpeg a complete, cross-platform solution to record, convert and stream audio and video

Getting Started

Clone the repository
git clone https://github.com/ce2kettu/yt-streaming-api.git
Install the dependencies
yarn install
Make cache and its subdirectories readable and writable
chmod -R 777 cache
Modify environt variables

Open .env.development or .env.production depending on your mode and modify the contents to fit your configuration. Example:

YT_API_KEY=AIzaSy...
API_KEY=not_so_secure
APP_PORT=3000
APP_HOST=localhost
APP_PROTOCOL=http
FFMPEG_PATH=ffmpeg
Run in production mode
yarn run start:prod
Run in development mode
yarn start

Docker

Build Docker image
docker build -t <your-image-name> .

Run image in detached mode

docker run -d -p <port-on-host>:<port-inside-docker-container> <your-image-name>

Run image in foreground mode

docker run -i -t -p <port-on-host>:<port-inside-docker-container> <your-image-name>

Stop Docker container

Detached mode

docker stop <container-id>

You can get a list of all running Docker container and its ids by following command

docker images

Foreground mode

Go to console and press + C at any time.

List of API Routes

+--------+-------------------------+
  Method | URI
+--------+-------------------------+
  GET    | /api/status
  GET    | /api/music/search
  GET    | /api/music/stream
  GET    | /api/music/stream/chunked
  GET    | /api/music/predownload
  GET    | /api/music/verify
  GET    | /api/music/song
  GET    | /api/music/playlist
+--------+-------------------------+

Endpoints and interaction

Status

Example: GET /api/status returns server status:

{
  "success": true,
  "message": "",
  "data": null
}
Search

Server can perform a search for songs at /api/music/search endpoint. You can remove the apiKey middleware to make this available for clients as well. Example: GET /api/music/search?q=Lady Gaga&maxResults=10&key=not_so_secure:

{
  "success": true,
  "message": "Retrived search result",
  "data": [
    {
      "id": "5L6xyaeiV58",
      "duration": 218000,
      "title": "Lady Gaga - Stupid Love (Official Music Video)",
      "artist": "LadyGagaVEVO"
    }
    ...
  ]
}
Stream

Clients can stream any song they want using /api/music/stream endpoint. You should remove this endpoint if you do not wish for this kind of behavior, but instead want the server to "whitelist" songs that can be played. The audio is cached by default for future requests. Example: GET /api/music/stream/5L6xyaeiV58 returns a playable mp3 audio stream.

Predownload

Server can predownload a song to make it available for clients to play. The audio is cached by default for future requests. Example: GET /api/music/predownload/5L6xyaeiV58&key=not_so_secure:

{
  "success": true,
  "message": "The requested song is now being downloaded",
  "data": null
}
Stream with server validation

Example: GET /api/music/stream/chunked/5L6xyaeiV58 returns a playable mp3 audio stream only if the server has predownloaded it. This way the server can ensure that clients cannot flood the server with requests that are not allowed.

Verify that a song exists

Returns whether a song is a valid. Example: GET /api/music/verify/5L6xyaeiV58&key=not_so_secure:

{
  "success": true | false,
  ...
}
Get song data

Example: GET /api/music/song/5L6xyaeiV58&key=not_so_secure:

{
  "success": true,
  "message": "Retrieved song data",
  "data": {
    "id": "5L6xyaeiV58",
    "duration": 218000,
    "title": "Lady Gaga - Stupid Love (Official Music Video)",
    "artist": "LadyGagaVEVO"
  }
}
Playlist data

Example: GET /api/music/playlist/PLx0sYbCqOb8TBPRdmBHs5Iftvv9TPboYG&key=not_so_secure:

{
  "success": true,
  "message": "Retrieved playlist data",
  "data": [
    "8EJ3zbKTWQ8",
    "9HDEHj2yzew",
    "2n9gE20hqU4",
    "kayI9QB1-IA",
    "EgBJmlPo8Xw",
    "T_OYT396cYw",
    "NKzd_YiW9AQ",
    ...
  ]
}

yt-streaming-api's People

Contributors

ce2kettu avatar janloebel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.