File size: 3,216 Bytes
2e0ef7a
 
 
fbefe88
 
e1c295b
fbefe88
e1c295b
 
 
 
fbefe88
 
 
 
 
 
e1c295b
fbefe88
 
 
e1c295b
fbefe88
e1c295b
 
 
 
 
 
 
 
 
 
 
 
fbefe88
e1c295b
 
 
 
 
 
 
 
 
 
 
fbefe88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e0ef7a
e1c295b
2e0ef7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1c295b
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import streamlit as st
import tempfile
import os
import cv2
import numpy as np
import subprocess

# Ensure FFmpeg is installed
os.system("apt-get update && apt-get install -y ffmpeg")

# Function to remove watermark from frames
def remove_watermark(frame):
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    _, mask = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
    result = cv2.inpaint(frame, mask, 3, cv2.INPAINT_TELEA)
    return result

# Function to enhance resolution to 1080p
def enhance_resolution(input_path, output_path):
    os.system(f"ffmpeg -i {input_path} -vf scale=1920:1080 {output_path}")

# Function to get video duration using subprocess (fix for ffmpeg.probe error)
def get_video_duration(video_path):
    try:
        result = subprocess.run(
            ["ffprobe", "-v", "error", "-show_entries", "format=duration",
             "-of", "default=noprint_wrappers=1:nokey=1", video_path], 
            stdout=subprocess.PIPE, 
            stderr=subprocess.PIPE, 
            text=True
        )
        return float(result.stdout.strip())  # Convert duration to float (seconds)
    except Exception as e:
        print("Error getting video duration:", e)
        return None

# Function to trim last 3 seconds (removes Invideo AI branding)
def trim_invidea_ai_branding(input_path, output_path):
    duration = get_video_duration(input_path)
    
    if duration:
        duration = max(0, duration - 3)  # Trim last 3 seconds
        os.system(f"ffmpeg -i {input_path} -t {duration} {output_path}")
    else:
        os.system(f"cp {input_path} {output_path}")  # Copy file without trimming

# Function to process the video
def process_video(input_video):
    cap = cv2.VideoCapture(input_video)
    if not cap.isOpened():
        return None

    temp_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter(temp_output.name, fourcc, 30.0, (int(cap.get(3)), int(cap.get(4))))

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        processed_frame = remove_watermark(frame)
        out.write(processed_frame)

    cap.release()
    out.release()

    # Enhance resolution
    enhanced_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
    enhance_resolution(temp_output.name, enhanced_output.name)

    # Trim branding
    final_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
    trim_invidea_ai_branding(enhanced_output.name, final_output.name)

    return final_output.name

# Streamlit UI
st.title("AI Video Enhancement App 🎥")

uploaded_file = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])

if uploaded_file:
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
        temp_file.write(uploaded_file.read())
        temp_file_path = temp_file.name

    st.write("Processing video...")
    processed_video_path = process_video(temp_file_path)

    if processed_video_path:
        st.video(processed_video_path)
        with open(processed_video_path, "rb") as file:
            st.download_button("Download Processed Video", file, "enhanced_video.mp4", "video/mp4")