Omar-youssef commited on
Commit
116ad44
·
1 Parent(s): 6ae0a89
Files changed (2) hide show
  1. app.py +1 -1
  2. link_processor.py +51 -34
app.py CHANGED
@@ -229,7 +229,7 @@ with tab1:
229
  st.header("Process Video from URL")
230
  with st.expander("Instructions", expanded=True):
231
  st.markdown("""
232
- 1. Paste a video URL from YouTube, Facebook, or other supported platforms
233
  2. Click the **Process URL** button
234
  3. Download the processed video or vocals
235
  """)
 
229
  st.header("Process Video from URL")
230
  with st.expander("Instructions", expanded=True):
231
  st.markdown("""
232
+ 1. Paste a video URL from Facebook, or other supported platforms
233
  2. Click the **Process URL** button
234
  3. Download the processed video or vocals
235
  """)
link_processor.py CHANGED
@@ -3,7 +3,7 @@ import yt_dlp
3
  import streamlit as st
4
  import re
5
  import os
6
- import tempfile
7
 
8
 
9
  class LinkDownloader:
@@ -12,61 +12,78 @@ class LinkDownloader:
12
  self.output_dir.mkdir(parents=True, exist_ok=True)
13
 
14
  def sanitize_filename(self, title):
15
- """Sanitize filename by removing special characters and limiting length"""
16
  clean_name = re.sub(r'[^a-zA-Z0-9.]', '_', title)
17
  if len(clean_name) > 50:
18
  clean_name = clean_name[:50]
19
  return clean_name
20
 
21
  def download_from_url(self, url):
22
- """Download video from URL using yt-dlp"""
23
  try:
24
  temp_filename = 'downloaded_video.%(ext)s'
25
  temp_filepath = str(self.output_dir / temp_filename)
26
 
27
- # Configure yt-dlp options
 
 
 
 
 
 
 
28
  ydl_opts = {
29
- 'format': 'best',
30
  'outtmpl': temp_filepath,
31
- 'quiet': False,
32
- 'no_warnings': False,
33
- 'extract_audio': False,
34
- 'ignoreerrors': True, # Continue on download errors
35
- 'no_check_certificate': True, # Skip HTTPS certificate validation
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  }
37
 
38
- # Try to extract video information
39
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
40
  try:
 
41
  info = ydl.extract_info(url, download=False)
42
- if not info:
43
- raise ValueError("Failed to extract video information.")
44
 
45
- # Download video
46
  ydl.download([url])
47
 
48
- except yt_dlp.utils.DownloadError as e:
49
- if "Sign in" in str(e):
50
- st.warning("Authentication required. Please pass cookies from your browser.")
51
- st.info(
52
- "Follow the instructions at "
53
- "[yt-dlp FAQ](https://github.com/yt-dlp/yt-dlp/wiki/FAQ#how-do-i-pass-cookies-to-yt-dlp) "
54
- "to pass cookies for authentication."
55
- )
56
- return None
57
- else:
58
- raise e
59
 
60
- # Get the actual extension and filepath
61
- ext = info.get('ext', 'mp4')
62
- actual_filepath = str(self.output_dir / f"downloaded_video.{ext}")
 
 
 
63
 
64
- # Verify file exists
65
- if not os.path.exists(actual_filepath):
66
- raise FileNotFoundError(f"Downloaded file not found at {actual_filepath}")
67
 
68
- return actual_filepath
 
 
 
 
 
69
 
70
  except Exception as e:
71
- st.error(f"Error during download: {str(e)}")
72
- return None
 
 
3
  import streamlit as st
4
  import re
5
  import os
6
+ from random import choice
7
 
8
 
9
  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