Created
March 16, 2024 09:41
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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