Skip to content

Instantly share code, notes, and snippets.

@m4p
Created March 16, 2024 09:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m4p/295b1bcd88946e8fc5aeef1d39b37a31 to your computer and use it in GitHub Desktop.
Save m4p/295b1bcd88946e8fc5aeef1d39b37a31 to your computer and use it in GitHub Desktop.
A redirect server to download audio from YouTube for e.g. podcast hosting
import os
from flask import Flask,redirect
from youtube_dl import YoutubeDL
ydl = YoutubeDL()
app = Flask(__name__)
@app.route('/<ytid>')
def hello(ytid):
ytid = os.path.splitext(ytid)[0]
url = 'https://youtu.be/'+ytid
r = ydl.extract_info(url, download=False)
# Use 139 for lower audio quality
audioFormat = next((format for format in r['formats'] if format["format_id"] == "140"), None)
audioURL = audioFormat['url']
return redirect(audioURL, code=302)
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5555))
app.run(host='0.0.0.0', port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment