File size: 2,358 Bytes
7413d5a
db239e6
7413d5a
 
56de98b
7413d5a
 
 
dddb41b
 
 
51131c0
aff5e42
 
db239e6
51131c0
db239e6
 
8ef7fc4
 
 
 
51131c0
8ef7fc4
db239e6
 
 
 
 
 
 
 
8ef7fc4
 
 
 
 
 
 
db239e6
 
 
 
 
aff5e42
db239e6
 
7413d5a
 
 
db239e6
7413d5a
 
 
 
 
 
 
56de98b
7413d5a
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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=1920:-1",               # Upscale lên Full HD, giữ tỷ lệ màn hình
        "pad=1920:1080:(ow-iw)/2:(oh-ih)/2",  # Đệ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
    command = [
        "ffmpeg",
        "-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()