EngrNarmeen commited on
Commit
e1c295b
·
verified ·
1 Parent(s): fbefe88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -11
app.py CHANGED
@@ -3,29 +3,48 @@ 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():
@@ -55,6 +74,7 @@ def process_video(input_video):
55
 
56
  return final_output.name
57
 
 
58
  st.title("AI Video Enhancement App 🎥")
59
 
60
  uploaded_file = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
@@ -71,3 +91,4 @@ if uploaded_file:
71
  st.video(processed_video_path)
72
  with open(processed_video_path, "rb") as file:
73
  st.download_button("Download Processed Video", file, "enhanced_video.mp4", "video/mp4")
 
 
3
  import os
4
  import cv2
5
  import numpy as np
6
+ import subprocess
7
 
8
+ # Ensure FFmpeg is installed
9
+ os.system("apt-get update && apt-get install -y ffmpeg")
10
+
11
+ # Function to remove watermark from frames
12
  def remove_watermark(frame):
 
13
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 
14
  _, mask = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
 
15
  result = cv2.inpaint(frame, mask, 3, cv2.INPAINT_TELEA)
16
  return result
17
 
18
+ # Function to enhance resolution to 1080p
19
  def enhance_resolution(input_path, output_path):
20
  os.system(f"ffmpeg -i {input_path} -vf scale=1920:1080 {output_path}")
21
 
22
+ # Function to get video duration using subprocess (fix for ffmpeg.probe error)
 
 
 
 
23
  def get_video_duration(video_path):
24
+ try:
25
+ result = subprocess.run(
26
+ ["ffprobe", "-v", "error", "-show_entries", "format=duration",
27
+ "-of", "default=noprint_wrappers=1:nokey=1", video_path],
28
+ stdout=subprocess.PIPE,
29
+ stderr=subprocess.PIPE,
30
+ text=True
31
+ )
32
+ return float(result.stdout.strip()) # Convert duration to float (seconds)
33
+ except Exception as e:
34
+ print("Error getting video duration:", e)
35
+ return None
36
 
37
+ # Function to trim last 3 seconds (removes Invideo AI branding)
38
+ def trim_invidea_ai_branding(input_path, output_path):
39
+ duration = get_video_duration(input_path)
40
+
41
+ if duration:
42
+ duration = max(0, duration - 3) # Trim last 3 seconds
43
+ os.system(f"ffmpeg -i {input_path} -t {duration} {output_path}")
44
+ else:
45
+ os.system(f"cp {input_path} {output_path}") # Copy file without trimming
46
+
47
+ # Function to process the video
48
  def process_video(input_video):
49
  cap = cv2.VideoCapture(input_video)
50
  if not cap.isOpened():
 
74
 
75
  return final_output.name
76
 
77
+ # Streamlit UI
78
  st.title("AI Video Enhancement App 🎥")
79
 
80
  uploaded_file = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
 
91
  st.video(processed_video_path)
92
  with open(processed_video_path, "rb") as file:
93
  st.download_button("Download Processed Video", file, "enhanced_video.mp4", "video/mp4")
94
+