ruslanmv commited on
Commit
f2ca935
·
verified ·
1 Parent(s): 99e2458

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -33,17 +33,26 @@ import yt_dlp
33
 
34
  def download_video(url):
35
  """
36
- Download a video from YouTube using yt-dlp.
37
  """
 
 
38
  ydl_opts = {
39
- 'format': 'bestvideo+bestaudio/best',
40
- 'outtmpl': '%(title)s.%(ext)s',
41
- 'merge_output_format': 'mp4',
42
- 'quiet': False,
43
  }
44
 
45
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
46
- ydl.download([url])
 
 
 
 
 
 
 
47
 
48
  # Example Usage
49
  url = "https://www.youtube.com/watch?v=uLVRZE8OAI4"
@@ -52,6 +61,7 @@ download_video(url)
52
 
53
 
54
 
 
55
  def validate_youtube(url):
56
  """
57
  Validates a YouTube URL, checks if the video exists, and returns whether its length exceeds 10 minutes.
 
33
 
34
  def download_video(url):
35
  """
36
+ Downloads a video from YouTube using yt-dlp.
37
  """
38
+ print("Downloading...")
39
+
40
  ydl_opts = {
41
+ 'format': 'bestvideo+bestaudio/best', # Ensures best quality
42
+ 'outtmpl': '%(title)s.%(ext)s', # Saves the file with the video title
43
+ 'merge_output_format': 'mp4', # Ensures the final file is MP4
44
+ 'quiet': False, # Shows download progress
45
  }
46
 
47
+ try:
48
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
49
+ info = ydl.extract_info(url, download=True)
50
+ local_file = ydl.prepare_filename(info) # Gets the output filename
51
+ print(f"Downloaded: {local_file}")
52
+ return local_file
53
+ except Exception as e:
54
+ print(f"Download failed: {str(e)}")
55
+ return None
56
 
57
  # Example Usage
58
  url = "https://www.youtube.com/watch?v=uLVRZE8OAI4"
 
61
 
62
 
63
 
64
+
65
  def validate_youtube(url):
66
  """
67
  Validates a YouTube URL, checks if the video exists, and returns whether its length exceeds 10 minutes.