Spaces:
Sleeping
Sleeping
File size: 2,763 Bytes
e689d92 2201270 a5bc70f e689d92 d04ba1b 1efe4c2 d04ba1b e689d92 a5bc70f e689d92 2201270 e689d92 2201270 e689d92 1733048 2201270 a5bc70f 2201270 1733048 2201270 e689d92 d04ba1b e689d92 1733048 1efe4c2 20b761b 1733048 e689d92 d04ba1b e689d92 d04ba1b 1efe4c2 e689d92 1efe4c2 e689d92 |
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
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() |