ruslanmv commited on
Commit
e4daff3
·
verified ·
1 Parent(s): f505bb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -19
app.py CHANGED
@@ -21,38 +21,64 @@ from utils import *
21
  import json
22
  import re
23
  from pytube import YouTube
 
 
 
 
24
  def download_video(url):
 
 
 
 
 
 
 
 
 
25
  print("Downloading...")
26
- local_file = (
27
- YouTube(url)
28
- .streams.filter(progressive=True, file_extension="mp4")
29
- .first()
30
- .download()
31
- )
32
- print("Downloaded")
33
- return local_file
 
 
 
 
 
 
34
 
35
  def validate_youtube(url):
36
  """
37
  Validates a YouTube URL, checks if the video exists, and returns whether its length exceeds 10 minutes.
38
-
 
39
  :param url: str - YouTube video URL
40
  :return: bool - True if the URL is invalid or video is longer than 10 minutes, otherwise False
41
  """
42
  try:
43
- yt = YouTube(url)
44
- video_length = yt.length # Video length in seconds
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  except Exception as e:
46
  print(f"Error: The provided URL is invalid or not accessible. ({e})")
47
  return True # Return True since the URL is invalid
48
-
49
- if video_length > 600:
50
- print("Your video is longer than 10 minutes.")
51
- return True
52
- else:
53
- print("Your video is 10 minutes or shorter.")
54
- return False
55
-
56
  def validate_url(url):
57
  import validators
58
  if not validators.url(url):
 
21
  import json
22
  import re
23
  from pytube import YouTube
24
+ from yt_dlp import YoutubeDL
25
+ from yt_dlp import YoutubeDL
26
+ import os
27
+
28
  def download_video(url):
29
+ """
30
+ Downloads a video from a URL using yt-dlp.
31
+
32
+ Args:
33
+ url: The URL of the video to download.
34
+
35
+ Returns:
36
+ The path to the downloaded file, or None if an error occurred.
37
+ """
38
  print("Downloading...")
39
+ try:
40
+ ydl_opts = {
41
+ 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', # Prioritize mp4
42
+ 'merge_output_format': 'mp4', # Ensure final output is mp4
43
+ 'outtmpl': '%(title)s.%(ext)s', # Use title for filename
44
+ }
45
+ with YoutubeDL(ydl_opts) as ydl:
46
+ info_dict = ydl.extract_info(url, download=True)
47
+ filename = ydl.prepare_filename(info_dict) # Get the actual filename.
48
+ print("Downloaded")
49
+ return filename
50
+ except Exception as e:
51
+ print(f"Error during download: {e}")
52
+ return None
53
 
54
  def validate_youtube(url):
55
  """
56
  Validates a YouTube URL, checks if the video exists, and returns whether its length exceeds 10 minutes.
57
+ Uses yt-dlp for more robust URL handling.
58
+
59
  :param url: str - YouTube video URL
60
  :return: bool - True if the URL is invalid or video is longer than 10 minutes, otherwise False
61
  """
62
  try:
63
+ with YoutubeDL({'quiet': True, 'no_warnings': True}) as ydl:
64
+ info = ydl.extract_info(url, download=False)
65
+ video_length = info.get('duration') # Video length in seconds
66
+
67
+ if video_length is None: # Handle cases where duration isn't available.
68
+ print("Could not determine video length.")
69
+ return True # Treat as invalid for now. Consider returning None if you want to handle differently.
70
+
71
+ if video_length > 600:
72
+ print("Your video is longer than 10 minutes.")
73
+ return True
74
+ else:
75
+ print("Your video is 10 minutes or shorter.")
76
+ return False
77
+
78
  except Exception as e:
79
  print(f"Error: The provided URL is invalid or not accessible. ({e})")
80
  return True # Return True since the URL is invalid
81
+
 
 
 
 
 
 
 
82
  def validate_url(url):
83
  import validators
84
  if not validators.url(url):