Spaces:
Runtime error
Runtime error
jhj0517
commited on
Commit
·
29f86be
1
Parent(s):
c5a4e30
Add `extract_frames()`
Browse files- modules/video_utils.py +32 -0
modules/video_utils.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""Extract frames and save them into output_temp_dir. This needs FFmpeg installed."""
|
16 |
+
os.makedirs(output_temp_dir, exist_ok=True)
|
17 |
+
output_path = os.path.join(output_temp_dir, "%05d.jpg")
|
18 |
+
|
19 |
+
command = [
|
20 |
+
'ffmpeg',
|
21 |
+
'-i', vid_input,
|
22 |
+
'-q:v', str(quality),
|
23 |
+
'-start_number', str(start_number),
|
24 |
+
f'{output_path}'
|
25 |
+
]
|
26 |
+
|
27 |
+
try:
|
28 |
+
subprocess.run(command, check=True)
|
29 |
+
except subprocess.CalledProcessError as e:
|
30 |
+
logger.exception("Error occured while extracting frames from the video")
|
31 |
+
raise f"An error occurred: {str(e)}"
|
32 |
+
|