Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import yt_dlp
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import string
|
6 |
+
|
7 |
+
# Function to generate a random alphabet name for the video file
|
8 |
+
def generate_random_name(length=10):
|
9 |
+
letters = string.ascii_lowercase
|
10 |
+
return ''.join(random.choice(letters) for _ in range(length))
|
11 |
+
|
12 |
+
# Function to download YouTube video using yt-dlp
|
13 |
+
def download_youtube_video(url):
|
14 |
+
# Set yt-dlp options
|
15 |
+
ydl_opts = {
|
16 |
+
'outtmpl': f'{generate_random_name()}.mp4' # Save video with a random name
|
17 |
+
}
|
18 |
+
|
19 |
+
# Download video
|
20 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
21 |
+
info_dict = ydl.extract_info(url, download=True)
|
22 |
+
filename = ydl.prepare_filename(info_dict)
|
23 |
+
|
24 |
+
return filename
|
25 |
+
|
26 |
+
# Gradio Interface
|
27 |
+
def yt_downloader(url):
|
28 |
+
# Download video
|
29 |
+
video_file = download_youtube_video(url)
|
30 |
+
|
31 |
+
# Return video file
|
32 |
+
return gr.outputs.File(video_file)
|
33 |
+
|
34 |
+
# Delete the video file after returning it
|
35 |
+
def delete_video_file(video_file):
|
36 |
+
os.remove(video_file)
|
37 |
+
|
38 |
+
# Gradio Interface with deletion functionality
|
39 |
+
def yt_downloader_with_deletion(url):
|
40 |
+
# Download video
|
41 |
+
video_file = download_youtube_video(url)
|
42 |
+
|
43 |
+
# Return video file and delete it after returning
|
44 |
+
return gr.outputs.File(video_file, label="Downloaded Video"), delete_video_file, video_file
|
45 |
+
|
46 |
+
# Interface for Gradio app
|
47 |
+
gr.Interface(
|
48 |
+
fn=yt_downloader_with_deletion,
|
49 |
+
inputs="text",
|
50 |
+
outputs=["file", "label", "file"],
|
51 |
+
title="YouTube Video Downloader",
|
52 |
+
description="Enter a YouTube video link to download the video."
|
53 |
+
).launch()
|