Omar-youssef commited on
Commit
4cd8c66
·
1 Parent(s): 116ad44
Files changed (1) hide show
  1. link_processor.py +35 -54
link_processor.py CHANGED
@@ -1,9 +1,11 @@
1
  from pathlib import Path
2
  import yt_dlp
 
3
  import streamlit as st
 
 
4
  import re
5
  import os
6
- from random import choice
7
 
8
 
9
  class LinkDownloader:
@@ -12,78 +14,57 @@ class LinkDownloader:
12
  self.output_dir.mkdir(parents=True, exist_ok=True)
13
 
14
  def sanitize_filename(self, title):
 
 
15
  clean_name = re.sub(r'[^a-zA-Z0-9.]', '_', title)
 
16
  if len(clean_name) > 50:
17
  clean_name = clean_name[:50]
18
  return clean_name
19
 
20
  def download_from_url(self, url):
 
21
  try:
 
22
  temp_filename = 'downloaded_video.%(ext)s'
23
  temp_filepath = str(self.output_dir / temp_filename)
24
 
25
- # Rotate between different user agents
26
- user_agents = [
27
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
28
- 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
29
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
30
- ]
31
-
32
- # Configure yt-dlp options with robust settings
33
  ydl_opts = {
34
- 'format': 'bestaudio/best', # Prefer audio formats
35
  'outtmpl': temp_filepath,
36
- 'quiet': True,
37
- 'no_warnings': True,
38
- 'extract_audio': True,
39
- 'postprocessors': [{
40
- 'key': 'FFmpegExtractAudio',
41
- 'preferredcodec': 'mp3',
42
- }],
43
- # Add robust options
44
- 'nocheckcertificate': True,
45
- 'user_agent': choice(user_agents),
46
- 'referer': 'https://www.youtube.com/',
47
- 'sleep_interval': 1, # Add small delay between requests
48
- 'max_sleep_interval': 5,
49
- 'socket_timeout': 30,
50
- 'retries': 10, # Increase retry attempts
51
- 'ignoreerrors': True,
52
- 'geo_bypass': True,
53
- 'geo_bypass_country': 'US'
54
  }
55
 
56
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
57
- try:
58
- st.info("Extracting audio...")
59
- info = ydl.extract_info(url, download=False)
60
- if info is None:
61
- raise Exception("Could not extract audio information")
62
 
63
- ydl.download([url])
 
 
 
 
 
64
 
65
- # Look for the downloaded file
66
- ext = 'mp3'
67
- actual_filepath = str(self.output_dir / f"downloaded_video.{ext}")
68
 
69
- if not os.path.exists(actual_filepath):
70
- possible_files = list(self.output_dir.glob("downloaded_video.*"))
71
- if possible_files:
72
- actual_filepath = str(possible_files[0])
73
- else:
74
- raise FileNotFoundError("No downloaded files found")
75
 
76
- st.success("Audio extraction completed!")
77
- return actual_filepath
 
 
78
 
79
- except yt_dlp.utils.DownloadError as e:
80
- if any(x in str(e).lower() for x in ['copyright', 'not available', 'private']):
81
- st.error("This video is not available for download.")
82
- else:
83
- st.error("Download failed. Please try a different video.")
84
- return None
85
 
86
  except Exception as e:
87
- st.error("An error occurred during processing.")
88
- print(f"Error details: {str(e)}")
89
  return None
 
1
  from pathlib import Path
2
  import yt_dlp
3
+ import tempfile
4
  import streamlit as st
5
+ from video_processor import VideoProcessor
6
+ from audio_processor import AudioProcessor
7
  import re
8
  import os
 
9
 
10
 
11
  class LinkDownloader:
 
14
  self.output_dir.mkdir(parents=True, exist_ok=True)
15
 
16
  def sanitize_filename(self, title):
17
+ """Sanitize filename by removing special characters and limiting length"""
18
+ # Replace special characters with underscore
19
  clean_name = re.sub(r'[^a-zA-Z0-9.]', '_', title)
20
+ # Limit length
21
  if len(clean_name) > 50:
22
  clean_name = clean_name[:50]
23
  return clean_name
24
 
25
  def download_from_url(self, url):
26
+ """Download video from URL using yt-dlp"""
27
  try:
28
+ # Create a temporary filename template
29
  temp_filename = 'downloaded_video.%(ext)s'
30
  temp_filepath = str(self.output_dir / temp_filename)
31
 
32
+ # Configure yt-dlp options
 
 
 
 
 
 
 
33
  ydl_opts = {
34
+ 'format': 'best',
35
  'outtmpl': temp_filepath,
36
+ 'quiet': False, # Enable output for debugging
37
+ 'no_warnings': False,
38
+ 'extract_audio': False,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  }
40
 
41
+ st.info("Starting video download...")
42
+ print(f"Download directory: {self.output_dir}")
43
+ print(f"Download template: {temp_filepath}")
 
 
 
44
 
45
+ # Download the video
46
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
47
+ # Extract info first
48
+ info = ydl.extract_info(url, download=False)
49
+ # Get the actual extension
50
+ ext = info.get('ext', 'mp4')
51
 
52
+ # Download the video
53
+ ydl.download([url])
 
54
 
55
+ # Construct the actual file path
56
+ actual_filepath = str(self.output_dir / f"downloaded_video.{ext}")
57
+ print(f"Expected downloaded file: {actual_filepath}")
 
 
 
58
 
59
+ # Verify file exists
60
+ if not os.path.exists(actual_filepath):
61
+ print(f"Files in directory: {os.listdir(self.output_dir)}")
62
+ raise FileNotFoundError(f"Downloaded file not found at {actual_filepath}")
63
 
64
+ print(f"File size: {os.path.getsize(actual_filepath)} bytes")
65
+ return actual_filepath
 
 
 
 
66
 
67
  except Exception as e:
68
+ st.error(f"Error during download: {str(e)}")
69
+ print(f"Download error details: {str(e)}")
70
  return None