Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pytube import YouTube
|
| 3 |
+
from moviepy.editor import VideoFileClip
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def download_youtube(url, format_choice, resolution):
|
| 7 |
+
try:
|
| 8 |
+
yt = YouTube(url)
|
| 9 |
+
|
| 10 |
+
if format_choice == "MP4":
|
| 11 |
+
# pilih resolusi
|
| 12 |
+
if resolution == "Highest":
|
| 13 |
+
stream = yt.streams.get_highest_resolution()
|
| 14 |
+
else:
|
| 15 |
+
stream = yt.streams.filter(res=resolution, file_extension='mp4').first()
|
| 16 |
+
|
| 17 |
+
if not stream:
|
| 18 |
+
return "Resolusi tidak tersedia"
|
| 19 |
+
|
| 20 |
+
output_path = stream.download()
|
| 21 |
+
return f"Video berhasil diunduh: {output_path}"
|
| 22 |
+
|
| 23 |
+
elif format_choice == "MP3":
|
| 24 |
+
# ambil audio
|
| 25 |
+
stream = yt.streams.filter(only_audio=True).first()
|
| 26 |
+
output_path = stream.download(filename="temp_audio.mp4")
|
| 27 |
+
|
| 28 |
+
# convert ke mp3
|
| 29 |
+
mp3_path = output_path.replace(".mp4", ".mp3")
|
| 30 |
+
clip = VideoFileClip(output_path)
|
| 31 |
+
clip.audio.write_audiofile(mp3_path)
|
| 32 |
+
clip.close()
|
| 33 |
+
os.remove(output_path)
|
| 34 |
+
return f"Audio berhasil diunduh: {mp3_path}"
|
| 35 |
+
|
| 36 |
+
else:
|
| 37 |
+
return "Format tidak didukung"
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error: {e}"
|
| 41 |
+
|
| 42 |
+
# UI Gradio
|
| 43 |
+
url_input = gr.Textbox(label="YouTube URL")
|
| 44 |
+
format_input = gr.Dropdown(["MP4", "MP3"], label="Format")
|
| 45 |
+
resolution_input = gr.Dropdown(["Highest", "144p", "240p", "360p", "480p", "720p", "1080p"], label="Resolusi (untuk MP4)")
|
| 46 |
+
|
| 47 |
+
gr.Interface(download_youtube,
|
| 48 |
+
inputs=[url_input, format_input, resolution_input],
|
| 49 |
+
outputs="text",
|
| 50 |
+
title="YouTube Downloader",
|
| 51 |
+
description="Download video YouTube sebagai MP4 atau MP3, pilih resolusi jika MP4").launch()
|