Omar-youssef commited on
Commit
34245f9
·
1 Parent(s): d4e3b06

add Youtube

Browse files
Files changed (2) hide show
  1. link_processor.py +35 -28
  2. requirements.txt +6 -6
link_processor.py CHANGED
@@ -1,11 +1,9 @@
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:
@@ -15,9 +13,7 @@ class LinkDownloader:
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
@@ -25,44 +21,55 @@ class LinkDownloader:
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)}")
 
1
  from pathlib import Path
2
  import yt_dlp
 
3
  import streamlit as st
 
 
4
  import re
5
  import os
6
+ import tempfile
7
 
8
 
9
  class LinkDownloader:
 
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
 
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 with more robust authentication handling
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
+ 'username': None, # Will be filled by user if needed
37
+ 'password': None, # Will be filled by user if needed
38
  }
39
 
40
+ # First try without authentication
41
+ try:
42
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
43
+ info = ydl.extract_info(url, download=False)
44
+ if info:
45
+ ydl.download([url])
46
+ except yt_dlp.utils.DownloadError as e:
47
+ if "Sign in" in str(e):
48
+ # If authentication is required, prompt for credentials
49
+ st.warning("Authentication required. Please enter your YouTube credentials.")
50
+ username = st.text_input("YouTube Email", key="yt_email")
51
+ password = st.text_input("YouTube Password", type="password", key="yt_pass")
52
 
53
+ if username and password:
54
+ ydl_opts['username'] = username
55
+ ydl_opts['password'] = password
 
 
 
56
 
57
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
58
+ info = ydl.extract_info(url, download=True)
59
+ else:
60
+ raise e
61
 
62
+ # Get the actual extension and filepath
63
+ ext = info.get('ext', 'mp4')
64
+ actual_filepath = str(self.output_dir / f"downloaded_video.{ext}")
65
 
66
+ # Verify file exists
67
+ if not os.path.exists(actual_filepath):
68
+ print(f"Files in directory: {os.listdir(self.output_dir)}")
69
+ raise FileNotFoundError(f"Downloaded file not found at {actual_filepath}")
70
 
71
+ print(f"File size: {os.path.getsize(actual_filepath)} bytes")
72
+ return actual_filepath
73
 
74
  except Exception as e:
75
  st.error(f"Error during download: {str(e)}")
requirements.txt CHANGED
@@ -1,10 +1,10 @@
1
- numpy==1.23.5
2
- pandas==1.5.3
3
- streamlit==1.29.0
4
- torch==2.1.0
5
- torchaudio==2.1.0
6
  demucs==4.0.1
7
  soundfile==0.12.1
8
  diffq==0.2.4
9
  ffmpeg-python==0.2.0
10
- yt-dlp==2024.12.23
 
1
+ numpy==1.26.3
2
+ pandas==2.2.0
3
+ streamlit==1.30.0
4
+ torch==2.1.2
5
+ torchaudio==2.1.2
6
  demucs==4.0.1
7
  soundfile==0.12.1
8
  diffq==0.2.4
9
  ffmpeg-python==0.2.0
10
+ yt-dlp==2024.1.7