Spaces:
Runtime error
Runtime error
jhj0517
commited on
Commit
·
cfc3ad2
1
Parent(s):
29f86be
Add `get_frames_from_dir()`
Browse files- modules/video_utils.py +21 -2
modules/video_utils.py
CHANGED
@@ -1,18 +1,22 @@
|
|
1 |
import subprocess
|
2 |
import os
|
|
|
3 |
|
4 |
from modules.logger_util import get_logger
|
|
|
5 |
|
6 |
logger = get_logger()
|
7 |
|
8 |
|
9 |
def extract_frames(
|
10 |
vid_input: str,
|
11 |
-
output_temp_dir: str,
|
12 |
quality: int = 2,
|
13 |
start_number: int = 0
|
14 |
):
|
15 |
-
"""
|
|
|
|
|
16 |
os.makedirs(output_temp_dir, exist_ok=True)
|
17 |
output_path = os.path.join(output_temp_dir, "%05d.jpg")
|
18 |
|
@@ -30,3 +34,18 @@ def extract_frames(
|
|
30 |
logger.exception("Error occured while extracting frames from the video")
|
31 |
raise f"An error occurred: {str(e)}"
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import subprocess
|
2 |
import os
|
3 |
+
from typing import List, Optional
|
4 |
|
5 |
from modules.logger_util import get_logger
|
6 |
+
from modules.paths import TEMP_DIR
|
7 |
|
8 |
logger = get_logger()
|
9 |
|
10 |
|
11 |
def extract_frames(
|
12 |
vid_input: str,
|
13 |
+
output_temp_dir: str = TEMP_DIR,
|
14 |
quality: int = 2,
|
15 |
start_number: int = 0
|
16 |
):
|
17 |
+
"""
|
18 |
+
Extract frames and save them into output_temp_dir. This needs FFmpeg installed.
|
19 |
+
"""
|
20 |
os.makedirs(output_temp_dir, exist_ok=True)
|
21 |
output_path = os.path.join(output_temp_dir, "%05d.jpg")
|
22 |
|
|
|
34 |
logger.exception("Error occured while extracting frames from the video")
|
35 |
raise f"An error occurred: {str(e)}"
|
36 |
|
37 |
+
|
38 |
+
def get_frames_from_dir(vid_dir: str,
|
39 |
+
available_extensions: Optional[List, str] = None) -> List:
|
40 |
+
if available_extensions is None:
|
41 |
+
available_extensions = [".jpg", ".jpeg", ".JPG", ".JPEG"]
|
42 |
+
|
43 |
+
if isinstance(available_extensions, str):
|
44 |
+
available_extensions = [available_extensions]
|
45 |
+
|
46 |
+
frame_names = [
|
47 |
+
p for p in os.listdir(vid_dir)
|
48 |
+
if os.path.splitext(p)[-1] in available_extensions
|
49 |
+
]
|
50 |
+
frame_names.sort(key=lambda x: int(os.path.splitext(x)[0]))
|
51 |
+
return frame_names
|