Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,59 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import tempfile
|
| 3 |
import os
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
st.title("AI Video Enhancement App 🎥")
|
| 7 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import tempfile
|
| 3 |
import os
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
def remove_watermark(frame):
|
| 8 |
+
# Convert to grayscale
|
| 9 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 10 |
+
# Thresholding to create a mask
|
| 11 |
+
_, mask = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
|
| 12 |
+
# Inpaint to remove watermark
|
| 13 |
+
result = cv2.inpaint(frame, mask, 3, cv2.INPAINT_TELEA)
|
| 14 |
+
return result
|
| 15 |
+
|
| 16 |
+
def enhance_resolution(input_path, output_path):
|
| 17 |
+
os.system(f"ffmpeg -i {input_path} -vf scale=1920:1080 {output_path}")
|
| 18 |
+
|
| 19 |
+
def trim_invidea_ai_branding(input_path, output_path, duration=None):
|
| 20 |
+
if duration is None:
|
| 21 |
+
duration = get_video_duration(input_path) - 3 # Trim last 3 seconds
|
| 22 |
+
os.system(f"ffmpeg -i {input_path} -t {duration} {output_path}")
|
| 23 |
+
|
| 24 |
+
def get_video_duration(video_path):
|
| 25 |
+
import ffmpeg
|
| 26 |
+
probe = ffmpeg.probe(video_path)
|
| 27 |
+
return float(probe['format']['duration'])
|
| 28 |
+
|
| 29 |
+
def process_video(input_video):
|
| 30 |
+
cap = cv2.VideoCapture(input_video)
|
| 31 |
+
if not cap.isOpened():
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 35 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 36 |
+
out = cv2.VideoWriter(temp_output.name, fourcc, 30.0, (int(cap.get(3)), int(cap.get(4))))
|
| 37 |
+
|
| 38 |
+
while cap.isOpened():
|
| 39 |
+
ret, frame = cap.read()
|
| 40 |
+
if not ret:
|
| 41 |
+
break
|
| 42 |
+
processed_frame = remove_watermark(frame)
|
| 43 |
+
out.write(processed_frame)
|
| 44 |
+
|
| 45 |
+
cap.release()
|
| 46 |
+
out.release()
|
| 47 |
+
|
| 48 |
+
# Enhance resolution
|
| 49 |
+
enhanced_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 50 |
+
enhance_resolution(temp_output.name, enhanced_output.name)
|
| 51 |
+
|
| 52 |
+
# Trim branding
|
| 53 |
+
final_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 54 |
+
trim_invidea_ai_branding(enhanced_output.name, final_output.name)
|
| 55 |
+
|
| 56 |
+
return final_output.name
|
| 57 |
|
| 58 |
st.title("AI Video Enhancement App 🎥")
|
| 59 |
|