mimbres commited on
Commit
dee5f69
·
verified ·
1 Parent(s): 26cce03

Update app.py

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