import gradio as gr from moviepy.editor import VideoFileClip import os import tempfile def compress_video(video): if video is None: return None with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_input: temp_input.write(video.read()) input_path = temp_input.name output_path = input_path.replace(".mp4", "_compressed.mp4") try: clip = VideoFileClip(input_path) clip_resized = clip.resize(height=720) # giảm độ phân giải clip_resized.write_videofile(output_path, bitrate="500k", codec="libx264") return output_path except Exception as e: return f"Lỗi: {e}" with gr.Blocks() as demo: gr.Markdown("## 🎬 Nén video trực tuyến - Giảm dung lượng MP4") with gr.Row(): video_input = gr.Video(label="Tải video lên", format="mp4") with gr.Row(): output = gr.Video(label="Video sau khi nén") with gr.Row(): compress_btn = gr.Button("⚙️ Bắt đầu nén") compress_btn.click(fn=compress_video, inputs=video_input, outputs=output) demo.launch()