File size: 3,368 Bytes
af28c41
a74c154
5099e75
e6ca1ab
 
5099e75
e6ca1ab
 
 
 
 
a74c154
e6ca1ab
a74c154
 
5099e75
a74c154
e6ca1ab
 
a74c154
e6ca1ab
a74c154
 
e6ca1ab
a74c154
e6ca1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
cd153ea
e6ca1ab
a74c154
e6ca1ab
5099e75
a74c154
 
 
e6ca1ab
 
5099e75
a74c154
 
 
e6ca1ab
 
 
 
 
 
 
 
 
 
 
 
5099e75
a74c154
e6ca1ab
 
 
 
 
 
 
cd153ea
e6ca1ab
 
 
 
cd153ea
e6ca1ab
cd153ea
5099e75
a74c154
5099e75
a74c154
 
 
1
2
3
4
5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
import yt_dlp
import os
import re
from moviepy.video.io.VideoFileClip import VideoFileClip

# Function to sanitize the filename
def sanitize_filename(title):
    return re.sub(r'[<>:"/\\|?*]', '_', title)

# Function to download video and return the file path
def download_video(url):
    download_path = './downloads'
    if not os.path.exists(download_path):
        os.makedirs(download_path)

    ydl_opts = {
        'outtmpl': os.path.join(download_path, '%(title)s.%(ext)s'),
        'format': 'best',
    }

    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            info_dict = ydl.extract_info(url, download=True)
            title = info_dict.get('title', None)
            if title:
                sanitized_title = sanitize_filename(title)
                file_path = os.path.join(download_path, f"{sanitized_title}.mp4")
                return file_path if os.path.exists(file_path) else None
            return None
    except Exception as e:
        return str(e)

# Function to crop the video
def crop_video(input_path, start_time, end_time):
    output_path = input_path.replace('.mp4', '_cropped.mp4')
    try:
        with VideoFileClip(input_path) as video:
            cropped_video = video.subclip(start_time, end_time)
            cropped_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
        return output_path
    except Exception as e:
        return str(e)

# Create Gradio interface
def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("# YouTube Video Downloader and Cropper")
        gr.Markdown("Enter the YouTube URL to download and crop the video.")

        # Input for YouTube URL
        url_input = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video URL here...")

        # Inputs for cropping
        start_time_input = gr.Number(label="Start Time (seconds)", value=0, interactive=True)
        end_time_input = gr.Number(label="End Time (seconds)", value=10, interactive=True)

        # File output for downloaded video
        output_file = gr.File(label="Download Cropped Video")

        # Status message for user feedback
        status_message = gr.Textbox(label="Status", placeholder="Processing...", interactive=False)

        # Download and Crop button
        process_btn = gr.Button("Download and Crop Video")

        # Define the action on button click
        def process_video(url, start_time, end_time):
            status_message.value = "Downloading video..."
            video_path = download_video(url)
            if video_path:
                status_message.value = "Cropping video..."
                cropped_video_path = crop_video(video_path, start_time, end_time)
                if cropped_video_path and os.path.exists(cropped_video_path):
                    return cropped_video_path, "Done! You can download your video below."
                else:
                    status_message.value = "Error cropping video."
            else:
                status_message.value = "Error downloading video. Please check the URL."
            return None, status_message.value

        process_btn.click(process_video, inputs=[url_input, start_time_input, end_time_input], outputs=[output_file, status_message])

    return demo

# Launch the interface
if __name__ == "__main__":
    gradio_interface().launch()