Spaces:
Runtime error
Runtime error
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() | |