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|
+
import os
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import shutil
|
| 7 |
+
|
| 8 |
+
def compress_video(input_video, resolution, quality):
|
| 9 |
+
if input_video is None:
|
| 10 |
+
return None, "Please upload a video file first."
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
# Create temporary files
|
| 14 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=Path(input_video).name) as tmp_input:
|
| 15 |
+
shutil.copy2(input_video, tmp_input.name)
|
| 16 |
+
input_path = tmp_input.name
|
| 17 |
+
|
| 18 |
+
output_path = input_path.replace(Path(input_video).suffix, "_compressed.mp4")
|
| 19 |
+
|
| 20 |
+
# Map quality to CRF (Constant Rate Factor)
|
| 21 |
+
crf_map = {"Low": 28, "Medium": 23, "High": 18}
|
| 22 |
+
crf = crf_map[quality]
|
| 23 |
+
|
| 24 |
+
# Map resolution to scale filter
|
| 25 |
+
scale_map = {
|
| 26 |
+
"Original": "",
|
| 27 |
+
"480p": "scale=-1:480",
|
| 28 |
+
"720p": "scale=-1:720",
|
| 29 |
+
"1080p": "scale=-1:1080"
|
| 30 |
+
}
|
| 31 |
+
scale = scale_map[resolution]
|
| 32 |
+
|
| 33 |
+
# Build FFmpeg command
|
| 34 |
+
cmd = ["ffmpeg", "-i", input_path]
|
| 35 |
+
|
| 36 |
+
if scale:
|
| 37 |
+
cmd.extend(["-vf", scale])
|
| 38 |
+
|
| 39 |
+
cmd.extend([
|
| 40 |
+
"-c:v", "libx264",
|
| 41 |
+
"-crf", str(crf),
|
| 42 |
+
"-preset", "slow",
|
| 43 |
+
"-c:a", "aac",
|
| 44 |
+
"-b:a", "128k",
|
| 45 |
+
"-movflags", "+faststart",
|
| 46 |
+
"-y", # Overwrite output file if exists
|
| 47 |
+
output_path
|
| 48 |
+
])
|
| 49 |
+
|
| 50 |
+
# Run FFmpeg
|
| 51 |
+
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
|
| 52 |
+
|
| 53 |
+
# Calculate file sizes
|
| 54 |
+
original_size = os.path.getsize(input_path) / (1024 * 1024)
|
| 55 |
+
compressed_size = os.path.getsize(output_path) / (1024 * 1024)
|
| 56 |
+
compression_ratio = (1 - compressed_size / original_size) * 100
|
| 57 |
+
|
| 58 |
+
info_text = f"""
|
| 59 |
+
Compression Complete!
|
| 60 |
+
|
| 61 |
+
Original size: {original_size:.2f} MB
|
| 62 |
+
Compressed size: {compressed_size:.2f} MB
|
| 63 |
+
Compression ratio: {compression_ratio:.1f}%
|
| 64 |
+
|
| 65 |
+
Settings used:
|
| 66 |
+
- Resolution: {resolution}
|
| 67 |
+
- Quality: {quality}
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
return output_path, info_text
|
| 71 |
+
|
| 72 |
+
except subprocess.CalledProcessError as e:
|
| 73 |
+
return None, f"Compression failed: {e.stderr}"
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return None, f"An error occurred: {str(e)}"
|
| 76 |
+
finally:
|
| 77 |
+
# Clean up input file
|
| 78 |
+
if 'input_path' in locals() and os.path.exists(input_path):
|
| 79 |
+
os.unlink(input_path)
|
| 80 |
+
|
| 81 |
+
# Create the Gradio interface
|
| 82 |
+
with gr.Blocks(title="Video Compressor", theme=gr.themes.Soft()) as demo:
|
| 83 |
+
gr.Markdown("# 🎬 Video Compressor")
|
| 84 |
+
gr.Markdown("Upload a video file, choose compression settings, and download the compressed version.")
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
with gr.Column(scale=1):
|
| 88 |
+
# Input section
|
| 89 |
+
input_video = gr.File(
|
| 90 |
+
label="Upload Video",
|
| 91 |
+
file_types=["video"],
|
| 92 |
+
file_count="single"
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Settings section
|
| 96 |
+
with gr.Group():
|
| 97 |
+
gr.Markdown("### Compression Settings")
|
| 98 |
+
|
| 99 |
+
resolution = gr.Dropdown(
|
| 100 |
+
choices=["Original", "480p", "720p", "1080p"],
|
| 101 |
+
value="Original",
|
| 102 |
+
label="Resolution",
|
| 103 |
+
info="Lower resolution = smaller file size"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
quality = gr.Dropdown(
|
| 107 |
+
choices=["Low", "Medium", "High"],
|
| 108 |
+
value="Medium",
|
| 109 |
+
label="Quality",
|
| 110 |
+
info="Lower quality = smaller file size"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Compress button
|
| 114 |
+
compress_btn = gr.Button("Compress Video", variant="primary", size="lg")
|
| 115 |
+
|
| 116 |
+
with gr.Column(scale=1):
|
| 117 |
+
# Output section
|
| 118 |
+
output_video = gr.File(
|
| 119 |
+
label="Compressed Video",
|
| 120 |
+
visible=False
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
output_info = gr.Markdown(
|
| 124 |
+
label="Compression Info",
|
| 125 |
+
value="Upload a video and click 'Compress Video' to begin."
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Examples section
|
| 129 |
+
gr.Examples(
|
| 130 |
+
examples=[
|
| 131 |
+
["sample.mp4", "720p", "Medium"],
|
| 132 |
+
["sample.mp4", "480p", "Low"],
|
| 133 |
+
],
|
| 134 |
+
inputs=[input_video, resolution, quality],
|
| 135 |
+
cache_examples=False,
|
| 136 |
+
label="Example Settings (you'll need to provide your own video file)"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Set up the compression function
|
| 140 |
+
compress_btn.click(
|
| 141 |
+
fn=compress_video,
|
| 142 |
+
inputs=[input_video, resolution, quality],
|
| 143 |
+
outputs=[output_video, output_info]
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# Auto-compress when settings change (optional)
|
| 147 |
+
input_video.change(
|
| 148 |
+
fn=lambda x: gr.update(visible=False) if x is None else gr.update(),
|
| 149 |
+
inputs=[input_video],
|
| 150 |
+
outputs=[output_video]
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
if __name__ == "__main__":
|
| 154 |
+
demo.launch()
|