Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,88 @@
|
|
1 |
import gradio as gr
|
2 |
import yt_dlp
|
3 |
import os
|
|
|
4 |
import time
|
|
|
5 |
|
6 |
-
# Function to
|
|
|
|
|
|
|
|
|
7 |
def download_video(url):
|
8 |
-
download_path = './downloads'
|
9 |
if not os.path.exists(download_path):
|
10 |
os.makedirs(download_path)
|
11 |
|
12 |
ydl_opts = {
|
13 |
-
'outtmpl': download_path
|
|
|
14 |
}
|
|
|
15 |
try:
|
16 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
17 |
-
info_dict = ydl.extract_info(url, download=
|
18 |
title = info_dict.get('title', None)
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
except Exception as e:
|
28 |
-
return
|
29 |
|
30 |
# Create Gradio interface
|
31 |
def gradio_interface():
|
32 |
with gr.Blocks() as demo:
|
33 |
-
gr.Markdown("# YouTube Video Downloader")
|
34 |
-
gr.Markdown("Enter the YouTube URL to download the video.")
|
35 |
|
36 |
# Input for YouTube URL
|
37 |
url_input = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video URL here...")
|
38 |
-
|
39 |
-
# Output display
|
40 |
-
output = gr.Textbox(label="Status")
|
41 |
-
video_link = gr.File(label="Download Link")
|
42 |
|
43 |
-
#
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Define the action on button click
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
return demo
|
50 |
|
|
|
1 |
import gradio as gr
|
2 |
import yt_dlp
|
3 |
import os
|
4 |
+
import re
|
5 |
import time
|
6 |
+
from moviepy.video.io.VideoFileClip import VideoFileClip
|
7 |
|
8 |
+
# Function to sanitize the filename
|
9 |
+
def sanitize_filename(title):
|
10 |
+
return re.sub(r'[<>:"/\\|?*]', '_', title)
|
11 |
+
|
12 |
+
# Function to download video and return the file path
|
13 |
def download_video(url):
|
14 |
+
download_path = './downloads'
|
15 |
if not os.path.exists(download_path):
|
16 |
os.makedirs(download_path)
|
17 |
|
18 |
ydl_opts = {
|
19 |
+
'outtmpl': os.path.join(download_path, '%(title)s.%(ext)s'),
|
20 |
+
'format': 'best',
|
21 |
}
|
22 |
+
|
23 |
try:
|
24 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
25 |
+
info_dict = ydl.extract_info(url, download=True)
|
26 |
title = info_dict.get('title', None)
|
27 |
+
if title:
|
28 |
+
sanitized_title = sanitize_filename(title)
|
29 |
+
file_path = os.path.join(download_path, f"{sanitized_title}.mp4")
|
30 |
+
return file_path if os.path.exists(file_path) else None
|
31 |
+
return None
|
32 |
+
except Exception as e:
|
33 |
+
return str(e)
|
34 |
+
|
35 |
+
# Function to crop the video
|
36 |
+
def crop_video(input_path, start_time, end_time):
|
37 |
+
output_path = input_path.replace('.mp4', '_cropped.mp4')
|
38 |
+
try:
|
39 |
+
with VideoFileClip(input_path) as video:
|
40 |
+
cropped_video = video.subclip(start_time, end_time)
|
41 |
+
cropped_video.write_videofile(output_path, codec='libx264')
|
42 |
+
return output_path
|
43 |
except Exception as e:
|
44 |
+
return str(e)
|
45 |
|
46 |
# Create Gradio interface
|
47 |
def gradio_interface():
|
48 |
with gr.Blocks() as demo:
|
49 |
+
gr.Markdown("# YouTube Video Downloader and Cropper")
|
50 |
+
gr.Markdown("Enter the YouTube URL to download and crop the video.")
|
51 |
|
52 |
# Input for YouTube URL
|
53 |
url_input = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video URL here...")
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
# Inputs for cropping
|
56 |
+
start_time_input = gr.Number(label="Start Time (seconds)", value=0, interactive=True)
|
57 |
+
end_time_input = gr.Number(label="End Time (seconds)", value=10, interactive=True)
|
58 |
+
|
59 |
+
# File output for downloaded video
|
60 |
+
output_file = gr.File(label="Download Cropped Video")
|
61 |
+
|
62 |
+
# Status message for user feedback
|
63 |
+
status_message = gr.Textbox(label="Status", placeholder="Processing...", interactive=False)
|
64 |
+
|
65 |
+
# Download and Crop button
|
66 |
+
process_btn = gr.Button("Download and Crop Video")
|
67 |
|
68 |
# Define the action on button click
|
69 |
+
def process_video(url, start_time, end_time):
|
70 |
+
status_message.value = "Downloading video..."
|
71 |
+
video_path = download_video(url)
|
72 |
+
if video_path:
|
73 |
+
status_message.value = "Cropping video..."
|
74 |
+
cropped_video_path = crop_video(video_path, start_time, end_time)
|
75 |
+
if cropped_video_path and os.path.exists(cropped_video_path):
|
76 |
+
time.sleep(1) # Add a slight delay to ensure file is fully written
|
77 |
+
status_message.value = "Done! You can download your video below."
|
78 |
+
return cropped_video_path
|
79 |
+
else:
|
80 |
+
status_message.value = "Error cropping video."
|
81 |
+
else:
|
82 |
+
status_message.value = "Error downloading video. Please check the URL."
|
83 |
+
return None
|
84 |
+
|
85 |
+
process_btn.click(process_video, inputs=[url_input, start_time_input, end_time_input], outputs=[output_file])
|
86 |
|
87 |
return demo
|
88 |
|