Spaces:
Paused
Paused
Update app.py
Browse files1 unmodified line23456789101112131415162 unmodified lines19202122232425262728293031323334353637383940411 unmodified linefrom fastapi.responses import StreamingResponseimport yt_dlpimport osapp = FastAPI()# Temporary download directoryDOWNLOAD_DIR = "downloads/"# Ensure the directory existsif not os.path.exists(DOWNLOAD_DIR):os.makedirs(DOWNLOAD_DIR)@app.get("/")def greet_json():2 unmodified lines@app.get("/download_video")def download_video(url: str):try:# Set up yt-dlp options to avoid downloading .webm filesydl_opts = {'format': 'bestvideo+bestaudio/best', # Download the best video and audio'outtmpl': DOWNLOAD_DIR + '%(id)s.%(ext)s', # Output path template'noplaylist': True, # Avoid downloading playlists, if it's a playlist URL'quiet': True, # Suppress unnecessary output}# Download video using yt-dlpwith yt_dlp.YoutubeDL(ydl_opts) as ydl:info_dict = ydl.extract_info(url, download=True)video_filename = ydl.prepare_filename(info_dict)# Return the video file as a responsevideo_file = open(video_filename, "rb")return StreamingResponse(video_file, media_type="video/mp4")except Exception as e:raise HTTPException(status_code=400, detail=str(e))1 unmodified line2345678910111213141516172 unmodified lines20212223242526272829303132333435363738394041424344454647481 unmodified linefrom fastapi.responses import StreamingResponseimport yt_dlpimport osfrom pathlib import Pathapp = FastAPI()# Temporary directory for downloaded videosDOWNLOAD_DIR = Path("downloads/")# Ensure the directory existsif not DOWNLOAD_DIR.exists():DOWNLOAD_DIR.mkdir(parents=True)@app.get("/")def greet_json():2 unmodified lines@app.get("/download_video")def download_video(url: str):try:# Set up yt-dlp options to download the best video/audioydl_opts = {'format': 'bestvideo+bestaudio/best', # Download the best video and audio'outtmpl': str(DOWNLOAD_DIR / '%(id)s.%(ext)s'), # Output path template'noplaylist': True, # Avoid downloading playlists, if it's a playlist URL'quiet': True, # Suppress unnecessary output}# Create a temporary filename for the videovideo_filename = DOWNLOAD_DIR / "temp_video.mp4"# Use yt-dlp to download the videowith yt_dlp.YoutubeDL(ydl_opts) as ydl:info_dict = ydl.extract_info(url, download=True)video_filename = ydl.prepare_filename(info_dict)# Check if the video was successfully downloadedif not video_filename.exists():raise HTTPException(status_code=500, detail="Video download failed.")# Open the video file and stream itvideo_file = open(video_filename, "rb")return StreamingResponse(video_file, media_type="video/mp4")except Exception as e:raise HTTPException(status_code=400, detail=str(e))
app.py+16-9