roychao19477 commited on
Commit
b58f36c
·
1 Parent(s): bd9ffb1

Add limitations

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -136,26 +136,28 @@ def extract_resampled_audio(video_path, target_sr=16000):
136
  torchaudio.save(resampled_audio_path, waveform, sample_rate=target_sr)
137
  return resampled_audio_path
138
 
139
- @spaces.GPU
140
- def extract_faces(video_file):
141
- # Step 0: Check resolution
142
- cap = cv2.VideoCapture(video_file)
 
143
  width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
144
  height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
145
  cap.release()
146
 
147
- # Step 1: Downsample if needed
148
- if width > 1280 or height > 720:
149
- resized_path = tempfile.mktemp(suffix=".mp4")
150
- subprocess.run([
151
- "ffmpeg", "-y", "-i", video_file,
152
- "-vf", "scale='min(1280,iw)':-2",
153
- "-c:v", "libx264", "-crf", "28",
154
- "-preset", "fast", "-an", resized_path
155
- ])
156
- video_file = resized_path
157
-
158
- cap = cv2.VideoCapture(video_file)
 
159
  fps = cap.get(cv2.CAP_PROP_FPS)
160
  frames = []
161
 
 
136
  torchaudio.save(resampled_audio_path, waveform, sample_rate=target_sr)
137
  return resampled_audio_path
138
 
139
+ import cv2
140
+ import ffmpeg
141
+
142
+ def downsample_if_needed(video_path):
143
+ cap = cv2.VideoCapture(video_path)
144
  width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
145
  height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
146
  cap.release()
147
 
148
+ if max(width, height) > 720:
149
+ downsampled_path = video_path.replace(".mp4", "_720p.mp4")
150
+ ffmpeg.input(video_path).output(
151
+ downsampled_path, vf="scale='min(720,iw)':-2", **{"c:v": "libx264"}
152
+ ).overwrite_output().run()
153
+ return downsampled_path
154
+ return video_path
155
+
156
+ @spaces.GPU
157
+ def extract_faces(video_file):
158
+ video_path = downsample_if_needed(video_file)
159
+
160
+ cap = cv2.VideoCapture(video_path)
161
  fps = cap.get(cv2.CAP_PROP_FPS)
162
  frames = []
163