import gradio as gr import subprocess import os def enhance_video_gradio(input_file): """ Hàm xử lý video đầu vào và trả về video đã tăng cường. """ # Tạo đường dẫn tạm thời cho file đầu vào và đầu ra input_path = "input.mp4" output_path = "enhanced_output.mp4" # Lưu file tải lên vào đường dẫn tạm thời with open(input_path, "wb") as f: f.write(input_file.read()) # Định nghĩa các bộ lọc ffmpeg filters = [ "unsharp=5:5:1.0", # Làm sắc nét video "hqdn3d=4:3:6:4", # Giảm nhiễu "eq=contrast=1.2:brightness=0.1" # Điều chỉnh tương phản và độ sáng ] # Kết hợp các bộ lọc thành chuỗi filter_chain = ",".join(filters) # Xây dựng lệnh ffmpeg command = [ "ffmpeg", "-i", input_path, # File đầu vào "-vf", filter_chain, # Áp dụng các bộ lọc "-c:a", "copy", # Sao chép âm thanh nguyên bản output_path # File đầu ra ] try: # Thực thi lệnh ffmpeg subprocess.run(command, check=True) return output_path except subprocess.CalledProcessError as e: return f"Lỗi khi xử lý video: {e}" # Tạo giao diện Gradio with gr.Blocks() as demo: gr.Markdown("# Video Enhancement with FFmpeg") gr.Markdown("Upload a video and get an enhanced version with improved quality.") with gr.Row(): input_video = gr.File(label="Upload Video") output_video = gr.Video(label="Enhanced Video") submit_button = gr.Button("Enhance Video") submit_button.click(enhance_video_gradio, inputs=input_video, outputs=output_video) # Khởi chạy ứng dụng demo.launch()