YouTube-DL Audio API
An pythonic RESTful API for getting URL of YouTube videos with only audio or video+audio, perfect for playing in background reducing the network bandwith. Uses yt-dlp for the queries, and Flask for the web microframework.
Requirements
The project requires Python 3.10 or higher, and install some dependencies.
pipenv install
For local development, pipenv
will create a virtual environment for you. There's a Dockerfile available for creating an image.
You can use the Docker image to use it in development. (see Docker section) This command should do the trick:
docker container run --rm -it -p 5000:5000 melchor9000/youtubedl-audio-api
Using the web
By default, running the app with flask --app ytdl_audio_api.app run --debug
will run on http://localhost:5000 . If you set the environment variable PORT
to some port, it will listen for everyone at the specified port.
The app can be scalated using gunicorn -c gunicorn_config.py ytdl_audio_api.wsgi
. Is configured to use gevent
for asynchronous serving.
You can deploy the app to Heroku or into a Docker environment.
Note: by default it will use
2 * Cores + 1
workers. You can change this using theWORKERS
environment variable. In Heroku this is mandatory, so add a "Config Var" calledWORKERS
and put a value of 1 or 2 (if using the free dyno).
Example
We did a demo page for that.
Also, I have an instance running with a little example.
RESTful API
The API is described in the OpenApi Schema or in the /oas.yaml
//swagger.yaml
of a deployed service. Use https://editor.swagger.io, https://petstore.swagger.io (filling the URL at the top) or any other Swagger UI instance to see the API and its documentation.
With the spec, you can also generate clients to talk to your own instance on to my public instance.
Docker
By default, the Docker image created will publish the web in the port 5000. Uses the python:3
image.
An example of building the image, running and testing:
docker image build -t yt-audio-api .
docker container run -d --rm -p 5000:5000 yt-audio-api # or melchor9000/youtubedl-audio-api from Docker Hub
curl http://localhost:5000/api/0RLvtm0EghQ
It is available a docker-compose.yaml
file to test it out. Uses the image from Docker Hub and a redis server for caching.
Cache
Some request can be cached using Redis. The only thing you must do to use Redis is setting the environment
variable REDIS
(or REDIS_URL
useful for Heroku) with the url of the server.
Logging
The server has logging for some points of functions. To modify the logging level, set LOGGING_LEVEL
environment variable to the value you want. Valid values are CRITICAL
, FATAL
, ERROR
, WARNING
, WARN
, INFO
, DEBUG
or NOTSET
, see logging from Python.
Proxy
You can set a proxy with the environment variable PROXY
. Supported proxy protocols are http, https and socks5.
If you want to use a proxy as a fallback, use FALLBACK_PROXY
. This will be used if any call to the YT API fails.
CORS
By default, CORS is set to allow all origins. This can be overriden by defining the environment variable CORS_ORIGINS
with a comma-separatid list of origins.
Pretty JSONs
If, for some reason, you want to get the JSONs in a pretty format, set JSONIFY_PRETTYPRINT_REGULAR
environment variable to true
.