stevafernandes commited on
Commit
8c19cb9
·
verified ·
1 Parent(s): 0b3ec51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -2
app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  import tempfile
5
  import time
6
  import mimetypes
 
7
  from pathlib import Path
8
 
9
  # --- Get API key from environment variable or user input ---
@@ -20,6 +21,21 @@ class VideoProcessor:
20
  genai.configure(api_key=api_key)
21
  self.model = genai.GenerativeModel("gemini-2.0-flash")
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def upload_video(self, video_path, display_name="uploaded_video"):
24
  try:
25
  return genai.upload_file(path=video_path, display_name=display_name)
@@ -132,6 +148,7 @@ def main():
132
  # Process new video if different from current
133
  if st.session_state.video_name != uploaded_file.name:
134
  tmp_path = None
 
135
  try:
136
  # Initialize processor if needed
137
  if not st.session_state.video_processor:
@@ -149,9 +166,14 @@ def main():
149
  with buffering_container.container():
150
  show_buffering_animation()
151
 
 
 
 
 
 
152
  # Upload video
153
  status_text.markdown("**Uploading video...**")
154
- video_file = st.session_state.video_processor.upload_video(tmp_path, uploaded_file.name)
155
 
156
  # Process video with status updates
157
  processed_file = st.session_state.video_processor.wait_for_processing(video_file, status_text)
@@ -174,9 +196,11 @@ def main():
174
  st.session_state.video_name = None
175
 
176
  finally:
177
- # Clean up temporary file
178
  if tmp_path and os.path.exists(tmp_path):
179
  os.unlink(tmp_path)
 
 
180
 
181
  # Display video player
182
  st.video(uploaded_file.getvalue())
 
4
  import tempfile
5
  import time
6
  import mimetypes
7
+ import subprocess
8
  from pathlib import Path
9
 
10
  # --- Get API key from environment variable or user input ---
 
21
  genai.configure(api_key=api_key)
22
  self.model = genai.GenerativeModel("gemini-2.0-flash")
23
 
24
+ def reduce_resolution(self, input_path, output_path, target_height=480):
25
+ """Reduce video resolution to speed up processing."""
26
+ try:
27
+ command = [
28
+ 'ffmpeg', '-i', input_path,
29
+ '-vf', f'scale=-2:{target_height}',
30
+ '-c:a', 'copy',
31
+ '-y', output_path
32
+ ]
33
+ subprocess.run(command, check=True, capture_output=True)
34
+ return output_path
35
+ except subprocess.CalledProcessError:
36
+ # If ffmpeg fails, return original path
37
+ return input_path
38
+
39
  def upload_video(self, video_path, display_name="uploaded_video"):
40
  try:
41
  return genai.upload_file(path=video_path, display_name=display_name)
 
148
  # Process new video if different from current
149
  if st.session_state.video_name != uploaded_file.name:
150
  tmp_path = None
151
+ reduced_path = None
152
  try:
153
  # Initialize processor if needed
154
  if not st.session_state.video_processor:
 
166
  with buffering_container.container():
167
  show_buffering_animation()
168
 
169
+ # Reduce video resolution
170
+ status_text.markdown("**Reducing video resolution...**")
171
+ reduced_path = tmp_path.replace(Path(tmp_path).suffix, f"_reduced{Path(tmp_path).suffix}")
172
+ video_to_upload = st.session_state.video_processor.reduce_resolution(tmp_path, reduced_path)
173
+
174
  # Upload video
175
  status_text.markdown("**Uploading video...**")
176
+ video_file = st.session_state.video_processor.upload_video(video_to_upload, uploaded_file.name)
177
 
178
  # Process video with status updates
179
  processed_file = st.session_state.video_processor.wait_for_processing(video_file, status_text)
 
196
  st.session_state.video_name = None
197
 
198
  finally:
199
+ # Clean up temporary files
200
  if tmp_path and os.path.exists(tmp_path):
201
  os.unlink(tmp_path)
202
+ if reduced_path and os.path.exists(reduced_path):
203
+ os.unlink(reduced_path)
204
 
205
  # Display video player
206
  st.video(uploaded_file.getvalue())