Spaces:
Sleeping
Sleeping
| import os | |
| import subprocess | |
| import gradio as gr | |
| def check_video_size(video_path, max_size_mb=1000): # Allow up to 1 GB | |
| size_mb = os.path.getsize(video_path) / (1024 * 1024) | |
| if size_mb > max_size_mb: | |
| raise gr.Error(f"Video is too large (max {max_size_mb} MB allowed). Please compress the video before uploading.") | |
| def split_video_into_chunks(video_path, chunk_duration=6, output_dir="video_chunks"): | |
| # Create output directory | |
| os.makedirs(output_dir, exist_ok=True) | |
| # Get video duration using FFmpeg | |
| cmd_duration = [ | |
| "ffprobe", "-v", "error", "-show_entries", "format=duration", | |
| "-of", "default=noprint_wrappers=1:nokey=1", video_path | |
| ] | |
| duration = float(subprocess.run(cmd_duration, capture_output=True, text=True).stdout.strip()) | |
| # Calculate the number of chunks | |
| num_chunks = int(duration // chunk_duration) | |
| # Split the video into chunks using FFmpeg | |
| chunk_files = [] | |
| for i in range(num_chunks): | |
| start_time = i * chunk_duration | |
| output_file = os.path.join(output_dir, f"chunk_{i+1}.mp4") | |
| # FFmpeg command to extract the chunk (using -c copy for speed) | |
| command = [ | |
| "ffmpeg", "-i", video_path, | |
| "-ss", str(start_time), | |
| "-t", str(chunk_duration), | |
| "-c", "copy", # Directly copy video and audio streams (no re-encoding) | |
| output_file | |
| ] | |
| subprocess.run(command, check=True) | |
| chunk_files.append(output_file) | |
| return chunk_files | |
| def process_video(video, chunk_duration): | |
| # Check video size (up to 1 GB allowed) | |
| check_video_size(video, max_size_mb=1000) | |
| # Split the video into chunks with the specified duration | |
| chunk_files = split_video_into_chunks(video, chunk_duration=chunk_duration) | |
| # Return the list of chunk files for download | |
| return chunk_files | |
| # Gradio Interface | |
| def gradio_interface(video, chunk_duration, progress=gr.Progress()): | |
| progress(0, desc="Uploading video... (This may take a while for large files)") | |
| progress(0.5, desc="Processing video...") | |
| chunk_files = process_video(video, chunk_duration) | |
| progress(1.0, desc="Done!") | |
| return chunk_files | |
| # Define Gradio app | |
| iface = gr.Interface( | |
| fn=gradio_interface, | |
| inputs=[ | |
| gr.Video(label="Upload Video"), | |
| gr.Slider(minimum=1, maximum=60, value=6, step=1, label="Chunk Duration (seconds)") | |
| ], | |
| outputs=gr.Files(label="Download Chunks"), | |
| title="Video Splitter with Custom Chunk Duration", | |
| description="Upload a video (max 1 GB) and choose the chunk duration in seconds. The app will split the video into chunks for download. **Tip:** Compress your video before uploading for faster processing." | |
| ) | |
| # Launch the app | |
| iface.launch() |