File size: 1,816 Bytes
de88cae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()