|
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. |
|
""" |
|
|
|
if not input_file: |
|
return "Không có file video nào được tải lên." |
|
|
|
|
|
input_path = input_file |
|
output_path = "enhanced_output.mp4" |
|
|
|
|
|
filters = [ |
|
"scale='if(gt(a,16/9),1920,-1)':'if(gt(a,16/9),-1,1080)'", |
|
"pad=1920:1080:(ow-iw)/2:(oh-ih)/2:black", |
|
"nlmeans=s=3:p=3:r=3", |
|
"unsharp=5:5:1.0", |
|
"eq=contrast=1.2:brightness=0.1", |
|
"yadif=mode=1" |
|
] |
|
|
|
|
|
filter_chain = ",".join(filters) |
|
|
|
|
|
command = [ |
|
"ffmpeg", |
|
"-y", |
|
"-i", input_path, |
|
"-vf", filter_chain, |
|
"-c:v", "libx265", |
|
"-crf", "23", |
|
"-preset", "medium", |
|
"-c:a", "copy", |
|
output_path |
|
] |
|
|
|
try: |
|
|
|
subprocess.run(command, check=True) |
|
return output_path |
|
except subprocess.CalledProcessError as e: |
|
return f"Lỗi khi xử lý video: {e}" |
|
|
|
|
|
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) |
|
|
|
|
|
demo.launch() |