mimbres commited on
Commit
be9d9c7
1 Parent(s): f5a93fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -65,36 +65,36 @@ def prepare_media(source_path_or_url: os.PathLike,
65
  if source_type == 'audio_filepath':
66
  audio_file = source_path_or_url
67
  elif source_type == 'youtube_url':
68
- proxy_handler = {"http": "http://108.61.175.7:31802", "https":"http://190.187.201.26:8080", "https":"http://38.54.71.67:80"}
69
- yt = YouTube(source_path_or_url, proxies=proxy_handler) #use_oauth=True, allow_oauth_cache=False)
70
- audio_stream = min(yt.streams.filter(only_audio=True), key=lambda s: s.bitrate)
71
- mp4_file = audio_stream.download(output_path='downloaded') # ./downloaded
72
- audio_file = mp4_file[:-3] + 'mp3'
73
- subprocess.run(['ffmpeg', '-i', mp4_file, '-ac', '1', audio_file])
74
- os.remove(mp4_file)
75
  # Download from youtube
76
- # try:
77
- # # Try PyTube first
78
- # # proxy_handler = {"http": "http://127.0.0.1:1087", "https":"http://127.0.0.1:1087"}
79
- # # yt = YouTube(source_path_or_url, proxies=proxy_handler)
80
- # yt = YouTube(source_path_or_url)
81
- # audio_stream = min(yt.streams.filter(only_audio=True), key=lambda s: s.bitrate)
82
- # mp4_file = audio_stream.download(output_path='downloaded') # ./downloaded
83
- # audio_file = mp4_file[:-3] + 'mp3'
84
- # subprocess.run(['ffmpeg', '-i', mp4_file, '-ac', '1', audio_file])
85
- # os.remove(mp4_file)
86
- # except Exception as e:
87
- # try:
88
- # # Try alternative
89
- # print(f"Failed with PyTube, error: {e}. Trying yt-dlp...")
90
- # audio_file = './downloaded/yt_audio'
91
- # subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
92
- # '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
93
- # '--force-overwrites', '--cookies', 'amt/src/extras/c.txt'])
94
- # audio_file += '.mp3'
95
- # except Exception as e:
96
- # print(f"Alternative downloader failed, error: {e}. Please try again later!")
97
- # return None
98
  else:
99
  raise ValueError(source_type)
100
 
 
65
  if source_type == 'audio_filepath':
66
  audio_file = source_path_or_url
67
  elif source_type == 'youtube_url':
68
+ # proxy_handler = {"http": "http://108.61.175.7:31802", "https":"http://190.187.201.26:8080", "https":"http://38.54.71.67:80"}
69
+ # yt = YouTube(source_path_or_url, proxies=proxy_handler) #use_oauth=True, allow_oauth_cache=False)
70
+ # audio_stream = min(yt.streams.filter(only_audio=True), key=lambda s: s.bitrate)
71
+ # mp4_file = audio_stream.download(output_path='downloaded') # ./downloaded
72
+ # audio_file = mp4_file[:-3] + 'mp3'
73
+ # subprocess.run(['ffmpeg', '-i', mp4_file, '-ac', '1', audio_file])
74
+ # os.remove(mp4_file)
75
  # Download from youtube
76
+ try:
77
+ # Try PyTube first
78
+ # proxy_handler = {"http": "http://127.0.0.1:1087", "https":"http://127.0.0.1:1087"}
79
+ # yt = YouTube(source_path_or_url, proxies=proxy_handler)
80
+ yt = YouTube(source_path_or_url)
81
+ audio_stream = min(yt.streams.filter(only_audio=True), key=lambda s: s.bitrate)
82
+ mp4_file = audio_stream.download(output_path='downloaded') # ./downloaded
83
+ audio_file = mp4_file[:-3] + 'mp3'
84
+ subprocess.run(['ffmpeg', '-i', mp4_file, '-ac', '1', audio_file])
85
+ os.remove(mp4_file)
86
+ except Exception as e:
87
+ try:
88
+ # Try alternative
89
+ print(f"Failed with PyTube, error: {e}. Trying yt-dlp...")
90
+ audio_file = './downloaded/yt_audio'
91
+ subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio', '-6',
92
+ '-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
93
+ '--force-overwrites', '--cookies', 'amt/src/extras/c.txt'])
94
+ audio_file += '.mp3'
95
+ except Exception as e:
96
+ print(f"Alternative downloader failed, error: {e}. Please try again later!")
97
+ return None
98
  else:
99
  raise ValueError(source_type)
100