jbilcke-hf HF staff commited on
Commit
99b8fc2
1 Parent(s): 4b57087

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -23,6 +23,7 @@ import gc
23
  from openai import OpenAI
24
  import base64
25
  from io import BytesIO
 
26
 
27
  # Load Hugging Face token if needed
28
  hf_token = os.getenv("HF_TOKEN")
@@ -287,12 +288,10 @@ def generate_video_from_text(
287
  video_np = images.squeeze(0).permute(1, 2, 3, 0).cpu().float().numpy()
288
  video_np = (video_np * 255).astype(np.uint8)
289
  height, width = video_np.shape[1:3]
290
- out = cv2.VideoWriter(
291
- output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (width, height)
292
- )
293
- for frame in video_np[..., ::-1]:
294
- out.write(frame)
295
- out.release()
296
  # Explicitly delete tensors and clear cache
297
  del images
298
  del video_np
@@ -382,12 +381,10 @@ def generate_video_from_image(
382
  video_np = images.squeeze(0).permute(1, 2, 3, 0).cpu().float().numpy()
383
  video_np = (video_np * 255).astype(np.uint8)
384
  height, width = video_np.shape[1:3]
385
- out = cv2.VideoWriter(
386
- output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (width, height)
387
- )
388
- for frame in video_np[..., ::-1]:
389
- out.write(frame)
390
- out.release()
391
  except Exception as e:
392
  raise gr.Error(
393
  f"An error occurred while generating the video. Please try again. Error: {e}",
 
23
  from openai import OpenAI
24
  import base64
25
  from io import BytesIO
26
+ from moviepy.editor import ImageSequenceClip
27
 
28
  # Load Hugging Face token if needed
29
  hf_token = os.getenv("HF_TOKEN")
 
288
  video_np = images.squeeze(0).permute(1, 2, 3, 0).cpu().float().numpy()
289
  video_np = (video_np * 255).astype(np.uint8)
290
  height, width = video_np.shape[1:3]
291
+
292
+ clip = ImageSequenceClip(list(video_np), fps=frame_rate)
293
+ clip = clip.resize(width=width, height=height)
294
+ clip.write_videofile(output_path, codec="libx264", audio=False)
 
 
295
  # Explicitly delete tensors and clear cache
296
  del images
297
  del video_np
 
381
  video_np = images.squeeze(0).permute(1, 2, 3, 0).cpu().float().numpy()
382
  video_np = (video_np * 255).astype(np.uint8)
383
  height, width = video_np.shape[1:3]
384
+
385
+ clip = ImageSequenceClip(list(video_np), fps=frame_rate)
386
+ clip = clip.resize(width=width, height=height)
387
+ clip.write_videofile(output_path, codec="libx264", audio=False)
 
 
388
  except Exception as e:
389
  raise gr.Error(
390
  f"An error occurred while generating the video. Please try again. Error: {e}",