This commit is contained in:
yair-mantis 2024-06-01 17:09:02 +03:00
commit 1efb2e1775
3 changed files with 83 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
venv/

78
main.py Normal file
View File

@ -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)

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
yt-dlp
python-vlc
fastapi
uvicorn