Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import tempfile
|
5 |
+
import shutil
|
6 |
+
|
7 |
+
def compress_video(video_file):
|
8 |
+
if video_file is None:
|
9 |
+
return None, "No video uploaded"
|
10 |
+
|
11 |
+
# Create temporary directory
|
12 |
+
temp_dir = tempfile.mkdtemp()
|
13 |
+
|
14 |
+
try:
|
15 |
+
# Define input and output paths
|
16 |
+
input_path = os.path.join(temp_dir, "input.mp4")
|
17 |
+
output_path = os.path.join(temp_dir, "output.mp4")
|
18 |
+
|
19 |
+
# Save uploaded video to temp directory
|
20 |
+
shutil.copy(video_file, input_path)
|
21 |
+
|
22 |
+
# Execute ffmpeg command
|
23 |
+
command = [
|
24 |
+
"ffmpeg",
|
25 |
+
"-i", input_path,
|
26 |
+
"-vcodec", "libx264",
|
27 |
+
"-crf", "28",
|
28 |
+
"-vf", "pad=ceil(iw/2)*2:ceil(ih/2)*2",
|
29 |
+
"-y",
|
30 |
+
output_path
|
31 |
+
]
|
32 |
+
|
33 |
+
# Run the command
|
34 |
+
process = subprocess.Popen(
|
35 |
+
command,
|
36 |
+
stdout=subprocess.PIPE,
|
37 |
+
stderr=subprocess.PIPE
|
38 |
+
)
|
39 |
+
stdout, stderr = process.communicate()
|
40 |
+
|
41 |
+
if process.returncode != 0:
|
42 |
+
return None, f"Error: {stderr.decode()}"
|
43 |
+
|
44 |
+
# Return the processed video file
|
45 |
+
return output_path, "Video compressed successfully"
|
46 |
+
|
47 |
+
except Exception as e:
|
48 |
+
return None, f"An error occurred: {str(e)}"
|
49 |
+
|
50 |
+
with gr.Blocks() as app:
|
51 |
+
gr.Markdown("# Video Compression with FFmpeg")
|
52 |
+
gr.Markdown("""
|
53 |
+
This app compresses videos using FFmpeg with the following parameters:
|
54 |
+
```
|
55 |
+
ffmpeg -i input.mp4 -vcodec libx264 -crf 28 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -y output.mp4
|
56 |
+
```
|
57 |
+
- Uses H.264 codec
|
58 |
+
- CRF 28 (higher values = more compression, lower quality)
|
59 |
+
- Pads dimensions to ensure they're even numbers (required by some codecs)
|
60 |
+
""")
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
with gr.Column():
|
64 |
+
input_video = gr.Video(label="Upload Video")
|
65 |
+
compress_btn = gr.Button("Compress Video", variant="primary")
|
66 |
+
|
67 |
+
with gr.Column():
|
68 |
+
output_video = gr.Video(label="Compressed Video")
|
69 |
+
status = gr.Markdown("Upload a video and click 'Compress Video'")
|
70 |
+
|
71 |
+
compress_btn.click(
|
72 |
+
fn=compress_video,
|
73 |
+
inputs=[input_video],
|
74 |
+
outputs=[output_video, status]
|
75 |
+
)
|
76 |
+
|
77 |
+
if __name__ == "__main__":
|
78 |
+
app.launch()
|