Spaces:
Runtime error
Runtime error
Create video.py
Browse files
video.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from moviepy.editor import VideoFileClip
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
def download_video(url):
|
| 7 |
+
# Download the video from the URL
|
| 8 |
+
response = requests.get(url, stream=True)
|
| 9 |
+
if response.status_code == 200:
|
| 10 |
+
# Save the video to a temporary file
|
| 11 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 12 |
+
with open(temp_file.name, 'wb') as f:
|
| 13 |
+
for chunk in response.iter_content(chunk_size=1024):
|
| 14 |
+
if chunk:
|
| 15 |
+
f.write(chunk)
|
| 16 |
+
return temp_file.name
|
| 17 |
+
else:
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
def process_video(url):
|
| 21 |
+
video_path = download_video(url)
|
| 22 |
+
if video_path:
|
| 23 |
+
# Optional: Verify the video is playable with moviepy
|
| 24 |
+
try:
|
| 25 |
+
clip = VideoFileClip(video_path)
|
| 26 |
+
clip.close()
|
| 27 |
+
return video_path
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Error processing video: {str(e)}"
|
| 30 |
+
else:
|
| 31 |
+
return "Error downloading video."
|
| 32 |
+
|
| 33 |
+
# Gradio UI components
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# Video URL Downloader")
|
| 36 |
+
url_input = gr.Textbox(label="Video URL", placeholder="Enter the video URL here...")
|
| 37 |
+
video_output = gr.Video(label="Downloaded Video")
|
| 38 |
+
submit_button = gr.Button("Download and Display Video")
|
| 39 |
+
|
| 40 |
+
# Action when the button is clicked
|
| 41 |
+
submit_button.click(process_video, inputs=url_input, outputs=video_output)
|
| 42 |
+
|
| 43 |
+
# Launch the Gradio app
|
| 44 |
+
demo.launch()
|