Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,79 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
|
3 |
import os
|
4 |
-
import
|
5 |
-
import
|
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 |
-
delay = backoff_factor * (2 ** (retries - 1)) + random.uniform(0, 1)
|
32 |
-
print(f"Retrying in {delay:.2f} seconds... (Attempt {retries}/{max_retries})")
|
33 |
-
time.sleep(delay)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
except Exception as e:
|
47 |
-
return False
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
return result
|
55 |
|
56 |
-
|
57 |
-
iface = gr.Interface(
|
58 |
-
fn=gradio_interface,
|
59 |
-
inputs=[
|
60 |
-
gr.Textbox(label="Enter the YouTube video URL"),
|
61 |
-
gr.Textbox(label="Enter the output directory (leave blank for current directory)")
|
62 |
-
],
|
63 |
-
outputs=gr.Textbox(label="Download Status"),
|
64 |
-
title="YouTube Video Downloader",
|
65 |
-
description="Enter a YouTube video URL and optionally specify an output directory to download the video."
|
66 |
-
)
|
67 |
|
68 |
-
# Launch the
|
69 |
-
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import yt_dlp
|
3 |
import os
|
4 |
+
import re
|
5 |
+
from moviepy.video.io.VideoFileClip import VideoFileClip
|
6 |
|
7 |
+
# Function to sanitize the filename
|
8 |
+
def sanitize_filename(title):
|
9 |
+
# Replace invalid characters with an underscore
|
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' # Folder to store downloads
|
15 |
+
if not os.path.exists(download_path):
|
16 |
+
os.makedirs(download_path)
|
17 |
|
18 |
+
ydl_opts = {
|
19 |
+
'outtmpl': download_path + '/%(title)s.%(ext)s',
|
20 |
+
'format': 'best', # Download the best available quality
|
21 |
+
}
|
22 |
+
|
23 |
+
try:
|
24 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
25 |
+
info_dict = ydl.extract_info(url, download=True) # Set download=True to download the video
|
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") # Adjust the extension as needed
|
30 |
+
return file_path if os.path.exists(file_path) else None # Ensure the file exists before returning
|
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)
|
57 |
+
end_time_input = gr.Number(label="End Time (seconds)", value=10)
|
58 |
|
59 |
+
# File output for downloaded video
|
60 |
+
output_file = gr.File(label="Download Cropped Video")
|
61 |
+
|
62 |
+
# Download and Crop button
|
63 |
+
process_btn = gr.Button("Download and Crop Video")
|
|
|
|
|
64 |
|
65 |
+
# Define the action on button click
|
66 |
+
def process_video(url, start_time, end_time):
|
67 |
+
video_path = download_video(url)
|
68 |
+
if video_path:
|
69 |
+
cropped_video_path = crop_video(video_path, start_time, end_time)
|
70 |
+
return cropped_video_path
|
71 |
+
return None
|
72 |
|
73 |
+
process_btn.click(process_video, inputs=[url_input, start_time_input, end_time_input], outputs=output_file)
|
|
|
74 |
|
75 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
# Launch the interface
|
78 |
+
if __name__ == "__main__":
|
79 |
+
gradio_interface().launch()
|