ferhatbou commited on
Commit
4d8c4e0
Β·
1 Parent(s): 6bced3b

Fix import issue

Browse files
Files changed (1) hide show
  1. video_accent_analyzer.py +41 -11
video_accent_analyzer.py CHANGED
@@ -87,6 +87,10 @@ class VideoAccentAnalyzer:
87
  self.model_loaded = False
88
  self._load_model()
89
 
 
 
 
 
90
  def _load_model(self):
91
  """Load the accent classification model with error handling"""
92
  print("πŸ€– Loading accent classification model...")
@@ -128,8 +132,29 @@ class VideoAccentAnalyzer:
128
  print(f"❌ Trimming exception: {e}")
129
  return input_path
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  def download_video(self, url, max_duration=None):
132
- """Download video using yt-dlp with improved error handling"""
133
  is_valid, result = self._validate_url(url)
134
  if not is_valid:
135
  print(f"❌ {result}")
@@ -138,23 +163,28 @@ class VideoAccentAnalyzer:
138
  url = result
139
  output_path = os.path.join(self.temp_dir, "video.%(ext)s")
140
 
141
- # Enhanced yt-dlp options for better compatibility
142
  ydl_opts = {
143
  'outtmpl': output_path,
144
- 'format': 'worst[ext=mp4]/worst', # Use worst quality for faster download and better compatibility
145
- 'quiet': False, # Show some output for debugging
146
  'no_warnings': False,
147
  'socket_timeout': 60,
148
- 'retries': 5,
149
- 'fragment_retries': 5,
150
- 'extract_flat': False,
151
- 'writeinfojson': False,
152
- 'writethumbnail': False,
153
- 'writesubtitles': False,
154
  }
155
 
 
 
 
 
 
 
 
 
 
 
 
156
  if max_duration:
157
- # More generous time limit for download
158
  ydl_opts['match_filter'] = lambda info: None if info.get('duration', 0) <= 200000 else "Video too long"
159
 
160
  try:
 
87
  self.model_loaded = False
88
  self._load_model()
89
 
90
+
91
+
92
+
93
+
94
  def _load_model(self):
95
  """Load the accent classification model with error handling"""
96
  print("πŸ€– Loading accent classification model...")
 
132
  print(f"❌ Trimming exception: {e}")
133
  return input_path
134
 
135
+ def _get_youtube_cookies(self):
136
+ """Get YouTube cookies from browser"""
137
+ import browser_cookie3
138
+
139
+ try:
140
+ # Try Firefox first
141
+ cookies = browser_cookie3.firefox(domain_name='.youtube.com')
142
+ except:
143
+ try:
144
+ # Try Chrome as fallback
145
+ cookies = browser_cookie3.chrome(domain_name='.youtube.com')
146
+ except:
147
+ print("⚠️ Could not get cookies from browser")
148
+ return None
149
+
150
+ return cookies
151
+
152
+
153
+
154
+
155
+
156
  def download_video(self, url, max_duration=None):
157
+ """Download video using yt-dlp with cookie support"""
158
  is_valid, result = self._validate_url(url)
159
  if not is_valid:
160
  print(f"❌ {result}")
 
163
  url = result
164
  output_path = os.path.join(self.temp_dir, "video.%(ext)s")
165
 
166
+ # Enhanced yt-dlp options
167
  ydl_opts = {
168
  'outtmpl': output_path,
169
+ 'format': 'worst[ext=mp4]/worst',
170
+ 'quiet': False,
171
  'no_warnings': False,
172
  'socket_timeout': 60,
173
+ 'retries': 5
 
 
 
 
 
174
  }
175
 
176
+ # Add cookies for YouTube URLs
177
+ if 'youtube.com' in url or 'youtu.be' in url:
178
+ cookies = self._get_youtube_cookies()
179
+ if cookies:
180
+ cookie_file = os.path.join(self.temp_dir, 'cookies.txt')
181
+ with open(cookie_file, 'w') as f:
182
+ f.write('# Netscape HTTP Cookie File\n')
183
+ for cookie in cookies:
184
+ f.write(f'.youtube.com\tTRUE\t/\tFALSE\t{cookie.expires}\t{cookie.name}\t{cookie.value}\n')
185
+ ydl_opts['cookiefile'] = cookie_file
186
+
187
  if max_duration:
 
188
  ydl_opts['match_filter'] = lambda info: None if info.get('duration', 0) <= 200000 else "Video too long"
189
 
190
  try: