diff --git a/webint-playlist-yt-music-for-parties.py b/webint-playlist-yt-music-for-parties.py new file mode 100644 index 0000000..0e0febd --- /dev/null +++ b/webint-playlist-yt-music-for-parties.py @@ -0,0 +1,147 @@ +import sys +import json +import threading +import subprocess +from flask import Flask, render_template_string, request, redirect + +app = Flask(__name__) +PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 5000 +queue = [] +current_process = None + +HTML_TEMPLATE = """ + + + + + + Musikwünsche Playlist + + + +

Song Hinzufügen

+
+ + +
+

Musikwünsche Playlist

+

Nach Entfernen bitte neu laden

+ + + +""" + +def run_command(command): + result = subprocess.run(command, shell=True, capture_output=True, text=True) + return result.stdout.strip() + +def get_video_info(query): + if "youtube.com" in query or "youtu.be" in query: + command = f'yt-dlp --print "%(id)s:%(title)s" --quiet --no-warnings "{query}"' + else: + command = f'yt-dlp "ytsearch:{query}" --print "%(id)s:%(title)s" --quiet --no-warnings' + output = run_command(command) + if not output: + return None + video_id, title = output.split(":", 1) + audio_url = run_command(f'yt-dlp -f "bestaudio/best" --get-url --quiet --no-warnings "{video_id}"') + return {'title': title, 'url': audio_url} + +def play_next(): + global current_process + while queue: + song = queue[0] + current_process = subprocess.Popen(["ffplay", "-nodisp", "-autoexit", song['url']], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + current_process.wait() + queue.pop(0) + current_process = None + +@app.route('/') +def index(): + return render_template_string(HTML_TEMPLATE, queue=queue) + +@app.route('/add', methods=['POST']) +def add_song(): + query = request.form['query'] + song = get_video_info(query) + if song: + queue.append(song) + if not current_process: + threading.Thread(target=play_next, daemon=True).start() + return redirect('/') + +@app.route('/skip') +def skip_song(): + global current_process + if current_process: + current_process.terminate() + return redirect('/') + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=PORT, debug=False) +