enhancedv / app.py
TDN-M's picture
Update app.py
77a112d verified
import gradio as gr
import subprocess
import os
def enhance_video(input_file):
"""
Hàm xử lý video đầu vào và trả về video đã tăng cường.
"""
# Kiểm tra xem input_file có tồn tại không
if not input_file:
return "Không có file video nào được tải lên."
# Sử dụng đường dẫn file do Gradio cung cấp
input_path = input_file
output_path = "enhanced_output.mp4"
# Định nghĩa các bộ lọc ffmpeg
filters = [
"scale='if(gt(a,16/9),1920,-1)':'if(gt(a,16/9),-1,1080)'", # Giữ tỷ lệ màn hình
"pad=1920:1080:(ow-iw)/2:(oh-ih)/2:black", # Đệm viền đen nếu cần
"nlmeans=s=3:p=3:r=3", # Giảm nhiễu
"unsharp=5:5:1.0", # Làm sắc nét
"eq=contrast=1.2:brightness=0.1", # Điều chỉnh tương phản/sáng
"yadif=mode=1" # Deinterlace
]
# Kết hợp các bộ lọc thành chuỗi
filter_chain = ",".join(filters)
# Xây dựng lệnh ffmpeg với tùy chọn -y để ghi đè file đầu ra
command = [
"ffmpeg",
"-y", # Tự động ghi đè file đầu ra
"-i", input_path, # File đầu vào
"-vf", filter_chain, # Áp dụng các bộ lọc
"-c:v", "libx265", # Codec HEVC (hiệu quả hơn H.264)
"-crf", "23", # Chất lượng video (thấp hơn = tốt hơn)
"-preset", "medium", # Tốc độ mã hóa (medium là cân bằng)
"-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 # Trả về file đầu ra
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, inputs=input_video, outputs=output_video)
# Khởi chạy ứng dụng
demo.launch()