slimshadow commited on
Commit
6cc6875
·
verified ·
1 Parent(s): 0b5dc96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -2,15 +2,16 @@ from fastapi import FastAPI, HTTPException
2
  from fastapi.responses import StreamingResponse
3
  import yt_dlp
4
  import os
 
5
 
6
  app = FastAPI()
7
 
8
- # Temporary download directory
9
- DOWNLOAD_DIR = "downloads/"
10
 
11
  # Ensure the directory exists
12
- if not os.path.exists(DOWNLOAD_DIR):
13
- os.makedirs(DOWNLOAD_DIR)
14
 
15
  @app.get("/")
16
  def greet_json():
@@ -19,23 +20,29 @@ def greet_json():
19
  @app.get("/download_video")
20
  def download_video(url: str):
21
  try:
22
- # Set up yt-dlp options to avoid downloading .webm files
23
  ydl_opts = {
24
  'format': 'bestvideo+bestaudio/best', # Download the best video and audio
25
- 'outtmpl': DOWNLOAD_DIR + '%(id)s.%(ext)s', # Output path template
26
  'noplaylist': True, # Avoid downloading playlists, if it's a playlist URL
27
  'quiet': True, # Suppress unnecessary output
28
  }
29
 
30
- # Download video using yt-dlp
 
 
 
31
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
32
  info_dict = ydl.extract_info(url, download=True)
33
  video_filename = ydl.prepare_filename(info_dict)
34
 
35
- # Return the video file as a response
 
 
 
 
36
  video_file = open(video_filename, "rb")
37
  return StreamingResponse(video_file, media_type="video/mp4")
38
 
39
  except Exception as e:
40
  raise HTTPException(status_code=400, detail=str(e))
41
-
 
2
  from fastapi.responses import StreamingResponse
3
  import yt_dlp
4
  import os
5
+ from pathlib import Path
6
 
7
  app = FastAPI()
8
 
9
+ # Temporary directory for downloaded videos
10
+ DOWNLOAD_DIR = Path("downloads/")
11
 
12
  # Ensure the directory exists
13
+ if not DOWNLOAD_DIR.exists():
14
+ DOWNLOAD_DIR.mkdir(parents=True)
15
 
16
  @app.get("/")
17
  def greet_json():
 
20
  @app.get("/download_video")
21
  def download_video(url: str):
22
  try:
23
+ # Set up yt-dlp options to download the best video/audio
24
  ydl_opts = {
25
  'format': 'bestvideo+bestaudio/best', # Download the best video and audio
26
+ 'outtmpl': str(DOWNLOAD_DIR / '%(id)s.%(ext)s'), # Output path template
27
  'noplaylist': True, # Avoid downloading playlists, if it's a playlist URL
28
  'quiet': True, # Suppress unnecessary output
29
  }
30
 
31
+ # Create a temporary filename for the video
32
+ video_filename = DOWNLOAD_DIR / "temp_video.mp4"
33
+
34
+ # Use yt-dlp to download the video
35
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
36
  info_dict = ydl.extract_info(url, download=True)
37
  video_filename = ydl.prepare_filename(info_dict)
38
 
39
+ # Check if the video was successfully downloaded
40
+ if not video_filename.exists():
41
+ raise HTTPException(status_code=500, detail="Video download failed.")
42
+
43
+ # Open the video file and stream it
44
  video_file = open(video_filename, "rb")
45
  return StreamingResponse(video_file, media_type="video/mp4")
46
 
47
  except Exception as e:
48
  raise HTTPException(status_code=400, detail=str(e))