Ujeshhh commited on
Commit
7614bef
·
verified ·
1 Parent(s): 11349d6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pytube import YouTube
3
+ from moviepy.editor import AudioFileClip
4
+ import os
5
+
6
+ def download_youtube(url, output_format, resolution, audio_bitrate):
7
+ try:
8
+ yt = YouTube(url)
9
+ title = yt.title.replace(" ", "_").replace("/", "_")
10
+
11
+ # Set output filename and path
12
+ output_filename = f"{title}.{output_format.lower()}"
13
+ output_path = f"downloads/{output_filename}"
14
+
15
+ # Get stream
16
+ if output_format == "MP4":
17
+ stream = yt.streams.filter(progressive=True, file_extension="mp4", res=resolution).first()
18
+ else: # Audio
19
+ stream = yt.streams.filter(only_audio=True).order_by("abr").desc().first()
20
+
21
+ if not stream:
22
+ return "Selected quality not available", None
23
+
24
+ # Download
25
+ downloaded_path = stream.download(output_path="downloads", filename=title + ".mp4")
26
+
27
+ if output_format in ["MP3", "WAV"]:
28
+ audio = AudioFileClip(downloaded_path)
29
+ if output_format == "MP3":
30
+ final_path = f"downloads/{title}.mp3"
31
+ audio.write_audiofile(final_path, bitrate=audio_bitrate)
32
+ else:
33
+ final_path = f"downloads/{title}.wav"
34
+ audio.write_audiofile(final_path)
35
+ audio.close()
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"]
48
+
49
+ interface = gr.Interface(
50
+ fn=download_youtube,
51
+ inputs=[
52
+ gr.Text(label="YouTube Video URL"),
53
+ gr.Radio(formats, label="Select Format"),
54
+ gr.Dropdown(resolutions, label="Video Resolution (for MP4)", value="720p"),
55
+ gr.Dropdown(bitrates, label="Audio Bitrate (for MP3)", value="128k"),
56
+ ],
57
+ outputs=[
58
+ gr.Text(label="Status"),
59
+ gr.File(label="Download File"),
60
+ ],
61
+ title="🎵 YouTube Downloader",
62
+ description="Download YouTube videos as MP3, WAV, or MP4 with quality selection. Built with Pytube + Gradio."
63
+ )
64
+
65
+ if __name__ == "__main__":
66
+ os.makedirs("downloads", exist_ok=True)
67
+ interface.launch()