Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
-
from pytube import YouTube
|
3 |
from moviepy.audio.io.AudioFileClip import AudioFileClip
|
|
|
4 |
import os
|
5 |
|
6 |
def download_youtube(url, output_format, resolution, audio_bitrate):
|
7 |
try:
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
os.remove(downloaded_path)
|
37 |
-
else:
|
38 |
-
final_path = downloaded_path
|
39 |
-
|
40 |
-
return f"Downloaded: {output_format}", final_path
|
41 |
|
42 |
except Exception as e:
|
43 |
return f"Error: {str(e)}", None
|
44 |
|
|
|
45 |
formats = ["MP3", "WAV", "MP4"]
|
46 |
resolutions = ["360p", "480p", "720p", "1080p"]
|
47 |
bitrates = ["64k", "128k", "192k", "256k", "320k"]
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from moviepy.audio.io.AudioFileClip import AudioFileClip
|
3 |
+
import yt_dlp
|
4 |
import os
|
5 |
|
6 |
def download_youtube(url, output_format, resolution, audio_bitrate):
|
7 |
try:
|
8 |
+
output_dir = "downloads"
|
9 |
+
os.makedirs(output_dir, exist_ok=True)
|
10 |
+
output_template = f"{output_dir}/%(title)s.%(ext)s"
|
11 |
+
|
12 |
+
ydl_opts = {
|
13 |
+
"format": "bestaudio/best" if output_format in ["MP3", "WAV"] else f"bestvideo[height<={resolution[:-1]}]+bestaudio/best",
|
14 |
+
"outtmpl": output_template,
|
15 |
+
"postprocessors": [],
|
16 |
+
}
|
17 |
+
|
18 |
+
if output_format == "MP3":
|
19 |
+
ydl_opts["postprocessors"].append({
|
20 |
+
'key': 'FFmpegExtractAudio',
|
21 |
+
'preferredcodec': 'mp3',
|
22 |
+
'preferredquality': audio_bitrate.replace("k", ""),
|
23 |
+
})
|
24 |
+
elif output_format == "WAV":
|
25 |
+
ydl_opts["postprocessors"].append({
|
26 |
+
'key': 'FFmpegExtractAudio',
|
27 |
+
'preferredcodec': 'wav',
|
28 |
+
})
|
29 |
+
|
30 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
31 |
+
info_dict = ydl.extract_info(url, download=True)
|
32 |
+
filename = ydl.prepare_filename(info_dict)
|
33 |
+
if output_format in ["MP3", "WAV"]:
|
34 |
+
filename = os.path.splitext(filename)[0] + f".{output_format.lower()}"
|
35 |
+
return "Download complete", filename
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
except Exception as e:
|
38 |
return f"Error: {str(e)}", None
|
39 |
|
40 |
+
|
41 |
formats = ["MP3", "WAV", "MP4"]
|
42 |
resolutions = ["360p", "480p", "720p", "1080p"]
|
43 |
bitrates = ["64k", "128k", "192k", "256k", "320k"]
|