jhj0517 commited on
Commit
4ddb4d7
·
1 Parent(s): 353443c

Add video util function

Browse files
Files changed (1) hide show
  1. modules/utils/video_helper.py +32 -1
modules/utils/video_helper.py CHANGED
@@ -1,6 +1,7 @@
1
  import subprocess
2
  import os
3
  from typing import List, Optional, Union
 
4
  from PIL import Image
5
  import numpy as np
6
  from dataclasses import dataclass
@@ -8,7 +9,8 @@ import re
8
  from pathlib import Path
9
 
10
  from modules.utils.constants import SOUND_FILE_EXT, VIDEO_FILE_EXT, IMAGE_FILE_EXT
11
- from modules.utils.paths import TEMP_VIDEO_FRAMES_DIR, TEMP_VIDEO_OUT_FRAMES_DIR, OUTPUTS_VIDEOS_DIR
 
12
 
13
 
14
  @dataclass
@@ -234,6 +236,35 @@ def create_video_from_frames(
234
  return output_path
235
 
236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  def get_frames_from_dir(vid_dir: str,
238
  available_extensions: Optional[Union[List, str]] = None,
239
  as_numpy: bool = False) -> List:
 
1
  import subprocess
2
  import os
3
  from typing import List, Optional, Union
4
+ import cv2
5
  from PIL import Image
6
  import numpy as np
7
  from dataclasses import dataclass
 
9
  from pathlib import Path
10
 
11
  from modules.utils.constants import SOUND_FILE_EXT, VIDEO_FILE_EXT, IMAGE_FILE_EXT
12
+ from modules.utils.paths import (TEMP_VIDEO_FRAMES_DIR, TEMP_VIDEO_OUT_FRAMES_DIR, OUTPUTS_VIDEOS_DIR,
13
+ get_auto_incremental_file_path)
14
 
15
 
16
  @dataclass
 
236
  return output_path
237
 
238
 
239
+ def create_video_from_numpy_list(frame_list: List[np.ndarray],
240
+ frame_rate: Optional[int] = None,
241
+ sound_path: Optional[str] = None,
242
+ output_dir: Optional[str] = None
243
+ ):
244
+ if output_dir is None:
245
+ output_dir = OUTPUTS_VIDEOS_DIR
246
+ os.makedirs(output_dir, exist_ok=True)
247
+ output_path = get_auto_incremental_file_path(output_dir, "mp4")
248
+
249
+ if frame_rate is None:
250
+ frame_rate = 25
251
+
252
+ if sound_path is None:
253
+ temp_sound = os.path.join(TEMP_VIDEO_FRAMES_DIR, "sound.mp3")
254
+ if os.path.exists(temp_sound):
255
+ sound_path = temp_sound
256
+
257
+ height, width, layers = frame_list[0].shape
258
+ fourcc = cv2.VideoWriter.fourcc(*'mp4v')
259
+
260
+ out = cv2.VideoWriter(output_path, fourcc, frame_rate, (width, height))
261
+
262
+ for frame in frame_list:
263
+ out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
264
+
265
+ out.release()
266
+
267
+
268
  def get_frames_from_dir(vid_dir: str,
269
  available_extensions: Optional[Union[List, str]] = None,
270
  as_numpy: bool = False) -> List: