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|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
import shutil
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
def save_video(video_path, filename=None):
|
| 9 |
+
"""Save video to a temporary file for download"""
|
| 10 |
+
if video_path is None:
|
| 11 |
+
return "❌ Please upload or record a video first!", None
|
| 12 |
+
|
| 13 |
+
# Generate filename if not provided
|
| 14 |
+
if not filename:
|
| 15 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 16 |
+
if hasattr(video_path, 'name'):
|
| 17 |
+
original_name = Path(video_path.name).stem
|
| 18 |
+
filename = f"{original_name}_{timestamp}.mp4"
|
| 19 |
+
else:
|
| 20 |
+
filename = f"video_{timestamp}.mp4"
|
| 21 |
+
elif not filename.endswith('.mp4'):
|
| 22 |
+
filename += '.mp4'
|
| 23 |
+
|
| 24 |
+
# Create temp directory for the download file
|
| 25 |
+
temp_dir = tempfile.mkdtemp()
|
| 26 |
+
dest_path = os.path.join(temp_dir, filename)
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# Copy the video to temp location
|
| 30 |
+
if isinstance(video_path, str) and os.path.exists(video_path):
|
| 31 |
+
shutil.copy2(video_path, dest_path)
|
| 32 |
+
else:
|
| 33 |
+
# Handle Gradio's temporary file object
|
| 34 |
+
with open(video_path, 'rb') as src:
|
| 35 |
+
with open(dest_path, 'wb') as dst:
|
| 36 |
+
shutil.copyfileobj(src, dst)
|
| 37 |
+
|
| 38 |
+
return f"✅ Video ready for download: {filename}", dest_path
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"❌ Error processing video: {str(e)}", None
|
| 42 |
+
|
| 43 |
+
def clear_all():
|
| 44 |
+
"""Clear all inputs and outputs"""
|
| 45 |
+
return None, None, None, ""
|
| 46 |
+
|
| 47 |
+
def update_video_display(video_path):
|
| 48 |
+
"""Update the video display when a new video is uploaded/recorded"""
|
| 49 |
+
if video_path:
|
| 50 |
+
return video_path, f"📹 Video loaded: {os.path.basename(video_path) if hasattr(video_path, 'name') else 'Recorded video'}"
|
| 51 |
+
return None, ""
|
| 52 |
+
|
| 53 |
+
# Create the Gradio interface
|
| 54 |
+
with gr.Blocks(
|
| 55 |
+
title="🎥 Simple Video Recorder",
|
| 56 |
+
theme=gr.themes.Soft(),
|
| 57 |
+
css="""
|
| 58 |
+
.gradio-container {max-width: 900px !important;}
|
| 59 |
+
.video-container {border-radius: 10px; overflow: hidden;}
|
| 60 |
+
.status-box {background: #f0f7ff; padding: 15px; border-radius: 8px; border-left: 4px solid #4a90e2;}
|
| 61 |
+
"""
|
| 62 |
+
) as demo:
|
| 63 |
+
|
| 64 |
+
gr.Markdown("""
|
| 65 |
+
# 🎥 Simple Video Recorder
|
| 66 |
+
Record a video using your webcam or upload an existing video file. Then download it with a custom filename!
|
| 67 |
+
""")
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
with gr.Column(scale=1):
|
| 71 |
+
gr.Markdown("### 📤 Input")
|
| 72 |
+
|
| 73 |
+
video_input = gr.Video(
|
| 74 |
+
label="Record or Upload Video",
|
| 75 |
+
sources=["webcam", "upload"],
|
| 76 |
+
interactive=True,
|
| 77 |
+
include_audio=True,
|
| 78 |
+
show_download_button=False,
|
| 79 |
+
height=300,
|
| 80 |
+
elem_classes="video-container"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
filename_input = gr.Textbox(
|
| 84 |
+
label="Download Filename (optional)",
|
| 85 |
+
placeholder="my_video.mp4 or leave blank for auto-name",
|
| 86 |
+
info="Leave blank for automatic naming: originalname_timestamp.mp4"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
process_btn = gr.Button("📥 Prepare for Download", variant="primary", scale=2)
|
| 91 |
+
clear_btn = gr.Button("🗑️ Clear All", variant="secondary", scale=1)
|
| 92 |
+
|
| 93 |
+
with gr.Column(scale=1):
|
| 94 |
+
gr.Markdown("### 📥 Output")
|
| 95 |
+
|
| 96 |
+
status_output = gr.Textbox(
|
| 97 |
+
label="Status",
|
| 98 |
+
value="👋 Ready to record or upload a video!",
|
| 99 |
+
interactive=False,
|
| 100 |
+
lines=3,
|
| 101 |
+
elem_classes="status-box"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
video_display = gr.Video(
|
| 105 |
+
label="Preview",
|
| 106 |
+
interactive=False,
|
| 107 |
+
show_download_button=False,
|
| 108 |
+
height=300,
|
| 109 |
+
elem_classes="video-container"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
download_file = gr.File(
|
| 113 |
+
label="Download Video",
|
| 114 |
+
interactive=False,
|
| 115 |
+
file_types=[".mp4", ".mov", ".avi", ".webm"]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Update video display and status when video is uploaded/recorded
|
| 119 |
+
video_input.change(
|
| 120 |
+
fn=update_video_display,
|
| 121 |
+
inputs=[video_input],
|
| 122 |
+
outputs=[video_display, status_output]
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Process video for download
|
| 126 |
+
process_btn.click(
|
| 127 |
+
fn=save_video,
|
| 128 |
+
inputs=[video_input, filename_input],
|
| 129 |
+
outputs=[status_output, download_file]
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# Clear all inputs and outputs
|
| 133 |
+
clear_btn.click(
|
| 134 |
+
fn=clear_all,
|
| 135 |
+
outputs=[video_input, video_display, download_file, status_output]
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# Instructions
|
| 139 |
+
gr.Markdown("""
|
| 140 |
+
## 📋 How to Use:
|
| 141 |
+
1. **Record**: Click the webcam icon to record a video using your camera
|
| 142 |
+
2. **Upload**: Click the upload button to select a video file from your computer
|
| 143 |
+
3. **Optional**: Enter a custom filename for download (or leave blank for auto-naming)
|
| 144 |
+
4. **Prepare**: Click "Prepare for Download" to generate the downloadable file
|
| 145 |
+
5. **Download**: Click the download button that appears in the Download section
|
| 146 |
+
|
| 147 |
+
## 📝 Notes:
|
| 148 |
+
- Supported formats: MP4, MOV, AVI, WebM
|
| 149 |
+
- Videos are processed temporarily and can be downloaded immediately
|
| 150 |
+
- Use "Clear All" to start fresh
|
| 151 |
+
""")
|
| 152 |
+
|
| 153 |
+
# Launch configuration
|
| 154 |
+
if __name__ == "__main__":
|
| 155 |
+
demo.launch(
|
| 156 |
+
share=False, # Set to True for public sharing link
|
| 157 |
+
debug=True,
|
| 158 |
+
server_name="0.0.0.0",
|
| 159 |
+
server_port=7860,
|
| 160 |
+
show_error=True
|
| 161 |
+
)
|