Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,7 @@ import re
|
|
| 5 |
import time
|
| 6 |
from moviepy.video.io.VideoFileClip import VideoFileClip
|
| 7 |
|
| 8 |
-
# Function to
|
| 9 |
-
def sanitize_filename(title):
|
| 10 |
-
# Replace invalid characters with an underscore
|
| 11 |
-
return re.sub(r'[<>:"/\\|?*]', '_', title)
|
| 12 |
-
|
| 13 |
-
# Function to download video and return the file path
|
| 14 |
def download_video(url):
|
| 15 |
download_path = './downloads' # Folder to store downloads
|
| 16 |
if not os.path.exists(download_path):
|
|
@@ -18,62 +13,34 @@ def download_video(url):
|
|
| 18 |
|
| 19 |
ydl_opts = {
|
| 20 |
'outtmpl': download_path + '/%(title)s.%(ext)s',
|
| 21 |
-
'format': 'best', # Download the best available quality
|
| 22 |
}
|
| 23 |
-
|
| 24 |
try:
|
| 25 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 26 |
-
info_dict = ydl.extract_info(url, download=
|
| 27 |
title = info_dict.get('title', None)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
file_path = os.path.join(download_path, f"{sanitized_title}.mp4") # Adjust the extension as needed
|
| 31 |
-
return file_path if os.path.exists(file_path) else None # Ensure the file exists before returning
|
| 32 |
-
return None
|
| 33 |
-
except Exception as e:
|
| 34 |
-
return str(e)
|
| 35 |
-
|
| 36 |
-
# Function to crop the video
|
| 37 |
-
def crop_video(input_path, start_time, end_time):
|
| 38 |
-
output_path = input_path.replace('.mp4', '_cropped.mp4')
|
| 39 |
-
try:
|
| 40 |
-
with VideoFileClip(input_path) as video:
|
| 41 |
-
cropped_video = video.subclip(start_time, end_time)
|
| 42 |
-
cropped_video.write_videofile(output_path, codec='libx264')
|
| 43 |
-
return output_path
|
| 44 |
except Exception as e:
|
| 45 |
-
return
|
| 46 |
|
| 47 |
# Create Gradio interface
|
| 48 |
def gradio_interface():
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
-
gr.Markdown("# YouTube Video Downloader
|
| 51 |
-
gr.Markdown("Enter the YouTube URL to download
|
| 52 |
|
| 53 |
# Input for YouTube URL
|
| 54 |
url_input = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video URL here...")
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# File output for downloaded video
|
| 61 |
-
output_file = gr.File(label="Download Cropped Video")
|
| 62 |
|
| 63 |
-
# Download
|
| 64 |
-
|
| 65 |
|
| 66 |
# Define the action on button click
|
| 67 |
-
|
| 68 |
-
video_path = download_video(url)
|
| 69 |
-
if video_path:
|
| 70 |
-
cropped_video_path = crop_video(video_path, start_time, end_time)
|
| 71 |
-
if cropped_video_path and os.path.exists(cropped_video_path):
|
| 72 |
-
time.sleep(1) # Add a slight delay to ensure file is fully written
|
| 73 |
-
return cropped_video_path
|
| 74 |
-
return None
|
| 75 |
-
|
| 76 |
-
process_btn.click(process_video, inputs=[url_input, start_time_input, end_time_input], outputs=output_file)
|
| 77 |
|
| 78 |
return demo
|
| 79 |
|
|
|
|
| 5 |
import time
|
| 6 |
from moviepy.video.io.VideoFileClip import VideoFileClip
|
| 7 |
|
| 8 |
+
# Function to download video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def download_video(url):
|
| 10 |
download_path = './downloads' # Folder to store downloads
|
| 11 |
if not os.path.exists(download_path):
|
|
|
|
| 13 |
|
| 14 |
ydl_opts = {
|
| 15 |
'outtmpl': download_path + '/%(title)s.%(ext)s',
|
|
|
|
| 16 |
}
|
|
|
|
| 17 |
try:
|
| 18 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 19 |
+
info_dict = ydl.extract_info(url, download=False)
|
| 20 |
title = info_dict.get('title', None)
|
| 21 |
+
ydl.download([url])
|
| 22 |
+
return f"Downloaded successfully: {title}", f"/downloads/{title}.mp4"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
+
return f"Error: {e}", None
|
| 25 |
|
| 26 |
# Create Gradio interface
|
| 27 |
def gradio_interface():
|
| 28 |
with gr.Blocks() as demo:
|
| 29 |
+
gr.Markdown("# YouTube Video Downloader")
|
| 30 |
+
gr.Markdown("Enter the YouTube URL to download the video.")
|
| 31 |
|
| 32 |
# Input for YouTube URL
|
| 33 |
url_input = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video URL here...")
|
| 34 |
|
| 35 |
+
# Output display
|
| 36 |
+
output = gr.Textbox(label="Status")
|
| 37 |
+
video_link = gr.File(label="Download Link")
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Download button
|
| 40 |
+
download_btn = gr.Button("Download Video")
|
| 41 |
|
| 42 |
# Define the action on button click
|
| 43 |
+
download_btn.click(download_video, inputs=url_input, outputs=[output, video_link])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
return demo
|
| 46 |
|