2024-06-01 17:09:02 +03:00
|
|
|
from fastapi import FastAPI, HTTPException
|
|
|
|
from pydantic import BaseModel
|
|
|
|
import yt_dlp
|
|
|
|
import vlc
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
2024-06-01 21:24:06 +03:00
|
|
|
|
2024-06-01 17:09:02 +03:00
|
|
|
# Define a data model for the queue item
|
|
|
|
class VideoLink(BaseModel):
|
|
|
|
url: str
|
|
|
|
|
2024-06-01 21:24:06 +03:00
|
|
|
|
2024-06-01 17:09:02 +03:00
|
|
|
# Queue to store video URLs
|
|
|
|
video_queue = []
|
|
|
|
current_video = None
|
|
|
|
|
|
|
|
# VLC player instance
|
|
|
|
vlc_instance = vlc.Instance()
|
|
|
|
player = vlc_instance.media_player_new()
|
2024-06-01 21:24:06 +03:00
|
|
|
player.set_fullscreen(True)
|
|
|
|
|
2024-06-01 17:09:02 +03:00
|
|
|
|
|
|
|
# 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"}
|
|
|
|
|
2024-06-01 21:24:06 +03:00
|
|
|
|
2024-06-01 17:09:02 +03:00
|
|
|
# Get current queue
|
|
|
|
@app.get("/queue")
|
|
|
|
def get_queue():
|
|
|
|
return {"queue": video_queue}
|
|
|
|
|
2024-06-01 21:24:06 +03:00
|
|
|
|
2024-06-01 17:09:02 +03:00
|
|
|
# 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()
|
2024-06-01 21:24:06 +03:00
|
|
|
elif action == "fullscreen":
|
|
|
|
if player.get_fullscreen():
|
|
|
|
player.set_fullscreen(False)
|
|
|
|
else:
|
|
|
|
player.set_fullscreen(True)
|
2024-06-01 17:09:02 +03:00
|
|
|
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"}
|
|
|
|
|
2024-06-01 21:24:06 +03:00
|
|
|
|
2024-06-01 17:09:02 +03:00
|
|
|
# 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)
|
|
|
|
|
2024-06-01 21:24:06 +03:00
|
|
|
|
2024-06-01 17:09:02 +03:00
|
|
|
# Start the video playing thread
|
|
|
|
video_thread = threading.Thread(target=play_videos, daemon=True)
|
|
|
|
video_thread.start()
|
|
|
|
|
|
|
|
# Run FastAPI
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import uvicorn
|
2024-06-01 21:24:06 +03:00
|
|
|
uvicorn.run(app, host="127.0.0.1", port=1337)
|