File size: 1,441 Bytes
06e8caf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
from moviepy.editor import VideoFileClip
import tempfile

def download_video(url):
    # Download the video from the URL
    response = requests.get(url, stream=True)
    if response.status_code == 200:
        # Save the video to a temporary file
        temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
        with open(temp_file.name, 'wb') as f:
            for chunk in response.iter_content(chunk_size=1024):
                if chunk:
                    f.write(chunk)
        return temp_file.name
    else:
        return None

def process_video(url):
    video_path = download_video(url)
    if video_path:
        # Optional: Verify the video is playable with moviepy
        try:
            clip = VideoFileClip(video_path)
            clip.close()
            return video_path
        except Exception as e:
            return f"Error processing video: {str(e)}"
    else:
        return "Error downloading video."

# Gradio UI components
with gr.Blocks() as demo:
    gr.Markdown("# Video URL Downloader")
    url_input = gr.Textbox(label="Video URL", placeholder="Enter the video URL here...")
    video_output = gr.Video(label="Downloaded Video")
    submit_button = gr.Button("Download and Display Video")

    # Action when the button is clicked
    submit_button.click(process_video, inputs=url_input, outputs=video_output)

# Launch the Gradio app
demo.launch()