Artificial-superintelligence commited on
Commit
e6ca1ab
·
verified ·
1 Parent(s): c764b90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -22
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 download video
 
 
 
 
7
  def download_video(url):
8
- download_path = './downloads' # Folder to store downloads
9
  if not os.path.exists(download_path):
10
  os.makedirs(download_path)
11
 
12
  ydl_opts = {
13
- 'outtmpl': download_path + '/%(title)s.%(ext)s',
 
14
  }
 
15
  try:
16
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
17
- info_dict = ydl.extract_info(url, download=False)
18
  title = info_dict.get('title', None)
19
- video_file = f"{download_path}/{title}.mp4"
20
- ydl.download([url])
21
-
22
- # Wait a moment to ensure the file is fully written
23
- time.sleep(1) # Add a delay for the file to be available
24
-
25
- # Return the download link
26
- return f"Downloaded successfully: {title}", video_file
 
 
 
 
 
 
 
 
27
  except Exception as e:
28
- return f"Error: {e}", None
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
- # Download button
44
- download_btn = gr.Button("Download Video")
 
 
 
 
 
 
 
 
 
 
45
 
46
  # Define the action on button click
47
- download_btn.click(download_video, inputs=url_input, outputs=[output, video_link])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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