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.
Install Node.js
- Node.js v12.16.1 or newer
Install yarn
- Yarn package manager
Install FFmpeg
- FFmpeg a complete, cross-platform solution to record, convert and stream audio and video
git clone https://github.com/ce2kettu/yt-streaming-api.git
yarn install
chmod -R 777 cache
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
yarn run start:prod
yarn start
docker build -t <your-image-name> .
docker run -d -p <port-on-host>:<port-inside-docker-container> <your-image-name>
docker run -i -t -p <port-on-host>:<port-inside-docker-container> <your-image-name>
docker stop <container-id>
You can get a list of all running Docker container and its ids by following command
docker images
Go to console and press + C at any time.
+--------+-------------------------+
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
+--------+-------------------------+
Example: GET /api/status
returns server status:
{
"success": true,
"message": "",
"data": null
}
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"
}
...
]
}
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.
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
}
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.
Returns whether a song is a valid.
Example: GET /api/music/verify/5L6xyaeiV58&key=not_so_secure
:
{
"success": true | false,
...
}
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"
}
}
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",
...
]
}