definitively going somewhere.
This commit is contained in:
1 parent
7179ff5fc1
commit
d7017c756b
7 files changed
+139
-41
No files matched your search
@@ -0,0 +1,67 @@
|
||||
import abc
|
||||
from pathlib import Path
|
||||
|
||||
import cv2
|
||||
|
||||
def create_video_writer(video_cap: cv2.VideoCapture, output_filename: Path) -> cv2.VideoWriter:
|
||||
|
||||
# grab the width, height, and fps of the frames in the video stream.
|
||||
frame_width = int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
||||
frame_height = int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
||||
fps = int(video_cap.get(cv2.CAP_PROP_FPS))
|
||||
|
||||
# initialize the FourCC and a video writer object
|
||||
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
||||
writer = cv2.VideoWriter(str(output_filename.absolute()),
|
||||
fourcc,
|
||||
fps,
|
||||
(frame_width, frame_height))
|
||||
|
||||
return writer
|
||||
|
||||
|
||||
class VideoWriter(abc.ABC):
|
||||
|
||||
@abc.abstractmethod
|
||||
def write_frame(self, frame) -> None:
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
def close(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class NoopVideoWriter(VideoWriter):
|
||||
|
||||
def write_frame(self, frame) -> None:
|
||||
pass
|
||||
|
||||
def close(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class MP4VVideoWriter(VideoWriter):
|
||||
|
||||
def __init__(self, video_writer: cv2.VideoWriter) -> None:
|
||||
self._video_writer = video_writer
|
||||
|
||||
def write_frame(self, frame) -> None:
|
||||
self._video_writer.write(frame)
|
||||
|
||||
def close(self) -> None:
|
||||
self._video_writer.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
@classmethod
|
||||
def from_frame(cls, input_: Path, output: Path) -> VideoWriter:
|
||||
video_cap = cv2.VideoCapture(str(input_))
|
||||
try:
|
||||
return cls(create_video_writer(video_cap, output))
|
||||
finally:
|
||||
video_cap.release()
|
||||
|
||||
|
||||
class MP4VVideoWriterWithFrame(MP4VVideoWriter):
|
||||
def write_frame(self, frame) -> None:
|
||||
super().write_frame(frame)
|
||||
cv2.imshow("Frame", frame)
|
||||
Reference in new issue
Block a user