bivav commited on
Commit
881792e
·
1 Parent(s): 5de10bf

Update ydl options

Browse files
Files changed (1) hide show
  1. app/services/youtube_service.py +16 -16
app/services/youtube_service.py CHANGED
@@ -1,14 +1,18 @@
1
- import asyncio
2
  from urllib.parse import urlparse, parse_qs
3
- from pathlib import Path
4
  import yt_dlp
5
  from ..core.config import settings
 
6
 
7
 
8
  class YouTubeService:
9
  def __init__(self):
10
  self.output_dir = settings.AUDIO_DIR
11
- # Configure yt-dlp with robust but simple defaults
 
 
 
 
 
12
  self.ydl_opts = {
13
  "format": "bestaudio[ext=m4a]/bestaudio/best",
14
  "postprocessors": [
@@ -24,15 +28,15 @@ class YouTubeService:
24
  "ignoreerrors": False,
25
  "no_color": True,
26
  "geo_bypass": True,
27
- "sleep_interval": 1,
28
- "max_sleep_interval": 5,
29
- "retries": 3,
30
- "fragment_retries": 3,
31
  "skip_unavailable_fragments": True,
32
  "keepvideo": False,
33
  "overwrites": True,
34
  "http_headers": {
35
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
36
  "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
37
  "Accept-Language": "en-us,en;q=0.5",
38
  "Sec-Fetch-Mode": "navigate",
@@ -67,41 +71,37 @@ class YouTubeService:
67
  async def convert_to_mp3(self, url: str) -> str:
68
  """Convert YouTube video to MP3 and return the filename"""
69
  try:
70
- # Extract video ID and validate URL
71
  video_id = self._extract_video_id(url)
72
  filename = f"{video_id}.mp3"
73
  output_path = self.output_dir / filename
74
 
75
- # Skip if file already exists
76
  if output_path.exists():
77
  return filename
78
 
79
- # Set output path for this specific download
80
  opts = dict(self.ydl_opts)
81
  opts["outtmpl"] = str(output_path.with_suffix(""))
 
 
 
82
 
83
- # Try download with fallback
84
  try:
85
  with yt_dlp.YoutubeDL(opts) as ydl:
86
  ydl.download([url])
87
  except Exception as e:
88
- # If first attempt fails, try with lower quality
89
  opts["format"] = "worstaudio/worst"
90
  with yt_dlp.YoutubeDL(opts) as ydl:
91
  ydl.download([url])
92
 
93
- # Verify the file was created
94
  if not output_path.exists():
95
  raise Exception("Failed to download and convert the video")
96
 
97
  return filename
98
 
99
  except ValueError as e:
100
- # URL validation errors
101
  print(f"URL validation error: {str(e)}")
102
  raise
103
  except Exception as e:
104
- # All other errors
105
  print(f"Error in convert_to_mp3: {str(e)}")
106
  raise Exception(f"Error converting video: {str(e)}")
107
 
 
 
1
  from urllib.parse import urlparse, parse_qs
 
2
  import yt_dlp
3
  from ..core.config import settings
4
+ import random
5
 
6
 
7
  class YouTubeService:
8
  def __init__(self):
9
  self.output_dir = settings.AUDIO_DIR
10
+ self.user_agents = [
11
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
12
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15",
13
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
14
+ "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1",
15
+ ]
16
  self.ydl_opts = {
17
  "format": "bestaudio[ext=m4a]/bestaudio/best",
18
  "postprocessors": [
 
28
  "ignoreerrors": False,
29
  "no_color": True,
30
  "geo_bypass": True,
31
+ "sleep_interval": 5,
32
+ "max_sleep_interval": 10,
33
+ "retries": 5,
34
+ "fragment_retries": 5,
35
  "skip_unavailable_fragments": True,
36
  "keepvideo": False,
37
  "overwrites": True,
38
  "http_headers": {
39
+ "User-Agent": random.choice(self.user_agents),
40
  "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
41
  "Accept-Language": "en-us,en;q=0.5",
42
  "Sec-Fetch-Mode": "navigate",
 
71
  async def convert_to_mp3(self, url: str) -> str:
72
  """Convert YouTube video to MP3 and return the filename"""
73
  try:
 
74
  video_id = self._extract_video_id(url)
75
  filename = f"{video_id}.mp3"
76
  output_path = self.output_dir / filename
77
 
 
78
  if output_path.exists():
79
  return filename
80
 
 
81
  opts = dict(self.ydl_opts)
82
  opts["outtmpl"] = str(output_path.with_suffix(""))
83
+ opts["http_headers"]["User-Agent"] = random.choice(
84
+ self.user_agents
85
+ ) # Rotate user agents
86
 
 
87
  try:
88
  with yt_dlp.YoutubeDL(opts) as ydl:
89
  ydl.download([url])
90
  except Exception as e:
91
+ print(f"First attempt failed: {str(e)}")
92
  opts["format"] = "worstaudio/worst"
93
  with yt_dlp.YoutubeDL(opts) as ydl:
94
  ydl.download([url])
95
 
 
96
  if not output_path.exists():
97
  raise Exception("Failed to download and convert the video")
98
 
99
  return filename
100
 
101
  except ValueError as e:
 
102
  print(f"URL validation error: {str(e)}")
103
  raise
104
  except Exception as e:
 
105
  print(f"Error in convert_to_mp3: {str(e)}")
106
  raise Exception(f"Error converting video: {str(e)}")
107