File size: 1,021 Bytes
f7b3397
db6d4d7
da8f353
087c136
db6d4d7
 
5806219
db6d4d7
 
5806219
1d6256c
db6d4d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import subprocess
import os

def download_media(url):
    # Determine if the URL is for audio or video
    ydl_opts = {
        'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',  # Prefer mp4 format
        'outtmpl': 'downloaded_media.%(ext)s',  # Output file name
    }

    # Download using yt-dlp
    subprocess.run(['yt-dlp', url, '--merge-output-format', 'mp4', '--recode-video', 'mp4'])

    # Check if downloaded file exists
    if os.path.exists('downloaded_media.mp4'):
        return gr.outputs.Video("downloaded_media.mp4")
    elif os.path.exists('downloaded_media.m4a'):
        return gr.outputs.Audio("downloaded_media.m4a")
    else:
        return "Media could not be downloaded or unsupported format."

# Create Gradio interface
iface = gr.Interface(
    fn=download_media,
    inputs="text",
    outputs="auto",
    title="YouTube Downloader",
    description="Enter a YouTube video URL to download its audio or video."
)

# Launch the interface
iface.launch()