er1t0 commited on
Commit
f03af96
1 Parent(s): 8fc6b8d

downscaling input video

Browse files
Files changed (1) hide show
  1. app.py +29 -8
app.py CHANGED
@@ -104,26 +104,47 @@ def remove_directory_contents(directory):
104
  @spaces.GPU
105
  @torch.inference_mode()
106
  @torch.autocast(device_type="cuda", dtype=torch.bfloat16)
107
- def process_video(video_path, prompt):
108
  try:
109
  # Get video info
110
  probe = ffmpeg.probe(video_path)
111
  video_info = next(s for s in probe['streams'] if s['codec_type'] == 'video')
112
  width = int(video_info['width'])
113
  height = int(video_info['height'])
114
- num_frames = int(video_info['nb_frames'])
115
- fps = eval(video_info['r_frame_rate'])
116
 
117
- print(f"Video info: {width}x{height}, {num_frames} frames, {fps} fps")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
- # Read frames
120
  out, _ = (
121
  ffmpeg
122
  .input(video_path)
 
 
123
  .output('pipe:', format='rawvideo', pix_fmt='rgb24')
124
  .run(capture_stdout=True)
125
  )
126
- frames = np.frombuffer(out, np.uint8).reshape([-1, height, width, 3])
127
 
128
  print(f"Read {len(frames)} frames")
129
 
@@ -196,7 +217,7 @@ def process_video(video_path, prompt):
196
  output_path = "segmented_video.mp4"
197
  process = (
198
  ffmpeg
199
- .input('pipe:', format='rawvideo', pix_fmt='rgb24', s=f'{width}x{height}', r=fps)
200
  .output(output_path, pix_fmt='yuv420p')
201
  .overwrite_output()
202
  .run_async(pipe_stdin=True)
@@ -219,7 +240,7 @@ def process_video(video_path, prompt):
219
  print(traceback.format_exc()) # This will print the full stack trace
220
  return None
221
 
222
- @spaces.GPU(duration=300)
223
  def segment_video(video_file, prompt):
224
  if video_file is None:
225
  return None
 
104
  @spaces.GPU
105
  @torch.inference_mode()
106
  @torch.autocast(device_type="cuda", dtype=torch.bfloat16)
107
+ def process_video(video_path, prompt, target_fps=30, max_dimension=640):
108
  try:
109
  # Get video info
110
  probe = ffmpeg.probe(video_path)
111
  video_info = next(s for s in probe['streams'] if s['codec_type'] == 'video')
112
  width = int(video_info['width'])
113
  height = int(video_info['height'])
114
+ original_fps = eval(video_info['r_frame_rate'])
 
115
 
116
+ # Calculate new dimensions while maintaining aspect ratio
117
+ if width > height:
118
+ if width > max_dimension:
119
+ new_width = max_dimension
120
+ new_height = int(height * (max_dimension / width))
121
+ else:
122
+ new_width = width
123
+ new_height = height
124
+ else:
125
+ if height > max_dimension:
126
+ new_height = max_dimension
127
+ new_width = int(width * (max_dimension / height))
128
+ else:
129
+ new_width = width
130
+ new_height = height
131
+
132
+ # Determine target fps
133
+ fps = min(original_fps, target_fps)
134
+
135
+ print(f"Original video: {width}x{height}, {original_fps} fps")
136
+ print(f"Processing at: {new_width}x{new_height}, {fps} fps")
137
 
138
+ # Read and resize frames
139
  out, _ = (
140
  ffmpeg
141
  .input(video_path)
142
+ .filter('fps', fps=fps)
143
+ .filter('scale', width=new_width, height=new_height)
144
  .output('pipe:', format='rawvideo', pix_fmt='rgb24')
145
  .run(capture_stdout=True)
146
  )
147
+ frames = np.frombuffer(out, np.uint8).reshape([-1, new_height, new_width, 3])
148
 
149
  print(f"Read {len(frames)} frames")
150
 
 
217
  output_path = "segmented_video.mp4"
218
  process = (
219
  ffmpeg
220
+ .input('pipe:', format='rawvideo', pix_fmt='rgb24', s=f'{new_width}x{new_height}', r=fps)
221
  .output(output_path, pix_fmt='yuv420p')
222
  .overwrite_output()
223
  .run_async(pipe_stdin=True)
 
240
  print(traceback.format_exc()) # This will print the full stack trace
241
  return None
242
 
243
+ @spaces.GPU(duration=600)
244
  def segment_video(video_file, prompt):
245
  if video_file is None:
246
  return None