from flask import Flask, jsonify, request, Response, send_file import youtube_dl import eyed3 """ Workflow script: https://workflow.is/workflows/7b80d0daa48847ff867882bed7df8032 Requires python installed with packages Flask, youtube_dl and eyed3 installed through pip Required FFmpeg for mp3 conversion """ class SimpleYDL(youtube_dl.YoutubeDL): def __init__(self, *args, **kargs): super(SimpleYDL, self).__init__(*args, **kargs) self.add_default_info_extractors() app = Flask(__name__) @app.route('/download//') def download(ytid): opt = { 'format': 'bestaudio/best', 'outtmpl': 'test.%(ext)s', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192', }], } with SimpleYDL(opt) as ydl: ydl.download([ytid]) af = eyed3.load("test.mp3") af.tag.artist = request.args.get('artist') af.tag.album = request.args.get('album') af.tag.title = request.args.get('title') af.tag.save() return send_file('test.mp3', mimetype="audio/mp3", as_attachment=True, attachment_filename='%s.mp3'%ytid) @app.route('/stream/') def stream(ytid): res = SimpleYDL().extract_info(ytid, download=False) data = {item['ext']: item['url'] for item in res['formats'] if item['format_id'] == "171" or item['format_id'] == "140"} data['title'] = res['title'] data['info'] = res['description'] return jsonify(data) if __name__ == '__main__': app.debug = True app.run(host='0.0.0.0')