Spaces:
Paused
Paused
| import gradio as gr | |
| from pytube import YouTube | |
| from moviepy.editor import VideoFileClip | |
| import os | |
| def download_youtube(url, format_choice, resolution): | |
| try: | |
| yt = YouTube(url) | |
| if format_choice == "MP4": | |
| # pilih resolusi | |
| if resolution == "Highest": | |
| stream = yt.streams.get_highest_resolution() | |
| else: | |
| stream = yt.streams.filter(res=resolution, file_extension='mp4').first() | |
| if not stream: | |
| return "Resolusi tidak tersedia" | |
| output_path = stream.download() | |
| return f"Video berhasil diunduh: {output_path}" | |
| elif format_choice == "MP3": | |
| # ambil audio | |
| stream = yt.streams.filter(only_audio=True).first() | |
| output_path = stream.download(filename="temp_audio.mp4") | |
| # convert ke mp3 | |
| mp3_path = output_path.replace(".mp4", ".mp3") | |
| clip = VideoFileClip(output_path) | |
| clip.audio.write_audiofile(mp3_path) | |
| clip.close() | |
| os.remove(output_path) | |
| return f"Audio berhasil diunduh: {mp3_path}" | |
| else: | |
| return "Format tidak didukung" | |
| except Exception as e: | |
| return f"Error: {e}" | |
| # UI Gradio | |
| url_input = gr.Textbox(label="YouTube URL") | |
| format_input = gr.Dropdown(["MP4", "MP3"], label="Format") | |
| resolution_input = gr.Dropdown(["Highest", "144p", "240p", "360p", "480p", "720p", "1080p"], label="Resolusi (untuk MP4)") | |
| gr.Interface(download_youtube, | |
| inputs=[url_input, format_input, resolution_input], | |
| outputs="text", | |
| title="YouTube Downloader", | |
| description="Download video YouTube sebagai MP4 atau MP3, pilih resolusi jika MP4").launch() |