Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import yt_dlp
|
| 4 |
+
|
| 5 |
+
def download_media(url, download_video):
|
| 6 |
+
download_dir = 'downloads'
|
| 7 |
+
os.makedirs(download_dir, exist_ok=True)
|
| 8 |
+
|
| 9 |
+
if download_video:
|
| 10 |
+
ydl_opts = {
|
| 11 |
+
'format': 'bestvideo+bestaudio/best',
|
| 12 |
+
'outtmpl': f'{download_dir}/%(title)s.%(ext)s',
|
| 13 |
+
}
|
| 14 |
+
else:
|
| 15 |
+
ydl_opts = {
|
| 16 |
+
'format': 'bestaudio/best',
|
| 17 |
+
'postprocessors': [{
|
| 18 |
+
'key': 'FFmpegExtractAudio',
|
| 19 |
+
'preferredcodec': 'mp3',
|
| 20 |
+
'preferredquality': '192',
|
| 21 |
+
}],
|
| 22 |
+
'outtmpl': f'{download_dir}/%(title)s.%(ext)s',
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 26 |
+
info_dict = ydl.extract_info(url, download=True)
|
| 27 |
+
file_title = ydl.prepare_filename(info_dict)
|
| 28 |
+
|
| 29 |
+
if download_video:
|
| 30 |
+
output_file = file_title
|
| 31 |
+
else:
|
| 32 |
+
output_file = file_title.rsplit('.', 1)[0] + '.mp3'
|
| 33 |
+
|
| 34 |
+
return output_file
|
| 35 |
+
|
| 36 |
+
def download_handler(url, download_video):
|
| 37 |
+
if url:
|
| 38 |
+
output_file = download_media(url, download_video)
|
| 39 |
+
return "Download completed!", output_file
|
| 40 |
+
else:
|
| 41 |
+
return "Please enter a YouTube URL", None
|
| 42 |
+
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# Download From YOUTUBE")
|
| 45 |
+
gr.Markdown("## Download MP3/MP4 from YouTube URL")
|
| 46 |
+
|
| 47 |
+
url = gr.Textbox(label="YouTube URL")
|
| 48 |
+
download_video = gr.Checkbox(label="Download Video", value=False)
|
| 49 |
+
download_btn = gr.Button("Download")
|
| 50 |
+
|
| 51 |
+
output_text = gr.Markdown()
|
| 52 |
+
output_file = gr.File()
|
| 53 |
+
|
| 54 |
+
download_btn.click(download_handler, inputs=[url, download_video], outputs=[output_text, output_file])
|
| 55 |
+
|
| 56 |
+
with gr.Sidebar():
|
| 57 |
+
gr.Image("profile.jpg", label="Profile Image", width=200)
|
| 58 |
+
gr.Markdown("### AI/ML Engineer")
|
| 59 |
+
gr.Markdown("Build AI with Haseeb")
|
| 60 |
+
gr.Markdown("### Made with ❤ by [Haseeb Ahmed](https://www.linkedin.com/in/muhammad-haseeb-ahmed-1954b5230/)")
|
| 61 |
+
|
| 62 |
+
demo.launch()
|