commit 1efb2e177562b71cfe2fa467b50de0effeb25f59 Author: yair-mantis Date: Sat Jun 1 17:09:02 2024 +0300 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eba74f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..bb0dfec --- /dev/null +++ b/main.py @@ -0,0 +1,78 @@ +from fastapi import FastAPI, HTTPException +from pydantic import BaseModel +import yt_dlp +import vlc +import threading +import time + +app = FastAPI() + +# Define a data model for the queue item +class VideoLink(BaseModel): + url: str + +# Queue to store video URLs +video_queue = [] +current_video = None + +# VLC player instance +vlc_instance = vlc.Instance() +player = vlc_instance.media_player_new() +player.fullscreen = True + +# Add video URL to the queue +@app.post("/add") +def add_video(video_link: VideoLink): + video_queue.append(video_link.url) + return {"message": "Video added to the queue"} + +# Get current queue +@app.get("/queue") +def get_queue(): + return {"queue": video_queue} + +# Control playback: play, pause, stop +@app.post("/control/{action}") +def control_playback(action: str): + if action == "play": + player.play() + elif action == "pause": + player.pause() + elif action == "stop": + player.stop() + elif action == "mute": + player.audio_toggle_mute() + else: + raise HTTPException(status_code=400, detail="Invalid action") + return {"message": f"Player action {action} executed"} + +# Seek video to a specific time in seconds +@app.post("/seek/{seconds}") +def seek(seconds: int): + player.set_time(seconds * 1000) + return {"message": f"Seeked to {seconds} seconds"} + +# Function to play videos from the queue +def play_videos(): + global current_video + while True: + if video_queue and player.get_state() not in [vlc.State.Playing, vlc.State.Paused]: + current_video = video_queue.pop(0) + ydl_opts = {'format': 'best', 'quiet': True} + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + info_dict = ydl.extract_info(current_video, download=False) + video_url = info_dict.get("url", None) + media = vlc_instance.media_new(video_url) + player.set_media(media) + player.play() + time.sleep(1) # Give some time for the player to start + time.sleep(1) + +# Start the video playing thread +video_thread = threading.Thread(target=play_videos, daemon=True) +video_thread.start() + +# Run FastAPI +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="0.0.0.0", port=8000) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e2e2b4f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +yt-dlp +python-vlc +fastapi +uvicorn