dev #1
@@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -27,11 +28,18 @@ class VideoMaker:
|
|||||||
video_numerator: str = "video_%08d.mp4",
|
video_numerator: str = "video_%08d.mp4",
|
||||||
):
|
):
|
||||||
"""Concatenates a sequence of videos using ffmpeg."""
|
"""Concatenates a sequence of videos using ffmpeg."""
|
||||||
cmd = f"ffmpeg -f concat -safe 0 -i <(for f in {videos_path / video_numerator}; do echo \"file '$f'\"; done) -c copy {output_path}"
|
|
||||||
|
videos = sorted(videos_path.glob("*.mp4"))
|
||||||
|
file = "file.txt"
|
||||||
|
with open(file, "w") as f:
|
||||||
|
for video in videos:
|
||||||
|
f.write(f"file '{video}'\n")
|
||||||
|
cmd = f"ffmpeg -f concat -safe 0 -i {file} -c copy {output_path}"
|
||||||
logging.info(f"Running command: {cmd}")
|
logging.info(f"Running command: {cmd}")
|
||||||
result = self.run_command(cmd)
|
result = self.run_command(cmd)
|
||||||
if result != 0:
|
if result != 0:
|
||||||
logging.error(f"Failed to concatenate videos. Command returned {result}")
|
logging.error(f"Failed to concatenate videos. Command returned {result}")
|
||||||
|
os.remove(file)
|
||||||
|
|
||||||
def get_fps(self, video_path: Path) -> float:
|
def get_fps(self, video_path: Path) -> float:
|
||||||
"""Gets the frames per second (FPS) of a video."""
|
"""Gets the frames per second (FPS) of a video."""
|
||||||
@@ -65,7 +73,9 @@ class VideoMaker:
|
|||||||
logging.error(f"Command failed with error: {e}")
|
logging.error(f"Command failed with error: {e}")
|
||||||
return e.returncode
|
return e.returncode
|
||||||
|
|
||||||
def video_to_frames_generator(self, video_path: Path, output_dir: Path, chunk_seconds: int = 10) -> Generator[tuple[Path, ...], None, None]:
|
def video_to_frames_generator(
|
||||||
|
self, video_path: Path, output_dir: Path, chunk_seconds: int = 10
|
||||||
|
) -> Generator[tuple[Path, ...], None, None]:
|
||||||
"""Extracts frames from a video and saves them to disk, yielding paths to the saved frames."""
|
"""Extracts frames from a video and saves them to disk, yielding paths to the saved frames."""
|
||||||
|
|
||||||
cap = cv2.VideoCapture(str(video_path))
|
cap = cv2.VideoCapture(str(video_path))
|
||||||
@@ -93,4 +103,4 @@ class VideoMaker:
|
|||||||
paths.append(frame_path)
|
paths.append(frame_path)
|
||||||
frame_index += 1
|
frame_index += 1
|
||||||
|
|
||||||
yield tuple(paths)
|
yield tuple(paths)
|
||||||
|
|||||||
Reference in New Issue
Block a user