small movie updates

This commit is contained in:
lugo 2022-11-25 15:34:50 +01:00
parent 9fe1559b1d
commit 635cf0445f
1 changed files with 16 additions and 9 deletions

View File

@ -27,6 +27,7 @@ class MovieSaver():
self, self,
fp_out: str, fp_out: str,
fps: int = 24, fps: int = 24,
shape_hw: List[int] = None,
crf: int = 24, crf: int = 24,
codec: str = 'libx264', codec: str = 'libx264',
preset: str ='fast', preset: str ='fast',
@ -42,6 +43,8 @@ class MovieSaver():
Output file name. If it already exists, it will be deleted. Output file name. If it already exists, it will be deleted.
fps: int fps: int
Frames per second. Frames per second.
shape_hw: List[int, int]
Output shape, optional argument. Can be initialized automatically when first frame is written.
crf: int crf: int
ffmpeg doc: the range of the CRF scale is 051, where 0 is lossless ffmpeg doc: the range of the CRF scale is 051, where 0 is lossless
(for 8 bit only, for 10 bit use -qp 0), 23 is the default, and 51 is worst quality possible. (for 8 bit only, for 10 bit use -qp 0), 23 is the default, and 51 is worst quality possible.
@ -65,6 +68,7 @@ class MovieSaver():
silent_ffmpeg: bool silent_ffmpeg: bool
Surpress the output from ffmpeg. Surpress the output from ffmpeg.
""" """
assert os.path.isdir(os.path.split(fp_out)[0]), "Directory does not exist!"
self.fp_out = fp_out self.fp_out = fp_out
self.fps = fps self.fps = fps
@ -79,7 +83,14 @@ class MovieSaver():
self.init_done = False self.init_done = False
self.nmb_frames = 0 self.nmb_frames = 0
self.shape_hw = [-1, -1] if shape_hw is None:
self.shape_hw = [-1, 1]
else:
if len(shape_hw) == 2:
shape_hw.append(3)
self.shape_hw = shape_hw
self.initialize()
print(f"MovieSaver initialized. fps={fps} crf={crf} pix_fmt={pix_fmt} codec={codec} preset={preset}") print(f"MovieSaver initialized. fps={fps} crf={crf} pix_fmt={pix_fmt} codec={codec} preset={preset}")
@ -97,7 +108,8 @@ class MovieSaver():
else: else:
self.ffmpg_process = subprocess.Popen(args, stdin=subprocess.PIPE) self.ffmpg_process = subprocess.Popen(args, stdin=subprocess.PIPE)
self.init_done = True self.init_done = True
print(f"First frame initialization done. Movie shape: {self.shape_hw}") self.shape_hw = tuple(self.shape_hw)
print(f"Initialization done. Movie shape: {self.shape_hw}")
def write_frame(self, out_frame: np.ndarray): def write_frame(self, out_frame: np.ndarray):
@ -119,7 +131,7 @@ class MovieSaver():
self.shape_hw = out_frame.shape self.shape_hw = out_frame.shape
self.initialize() self.initialize()
assert self.shape_hw == out_frame.shape, "You cannot change the image size after init. Initialized with {self.shape_hw}, out_frame {out_frame.shape}" assert self.shape_hw == out_frame.shape, f"You cannot change the image size after init. Initialized with {self.shape_hw}, out_frame {out_frame.shape}"
# write frame # write frame
self.ffmpg_process.stdin.write( self.ffmpg_process.stdin.write(
@ -176,7 +188,7 @@ def concatenate_movies(fp_final: str, list_fp_movies: List[str]):
cmd = f'ffmpeg -f concat -safe 0 -i {fp_list} -c copy {fp_final}' cmd = f'ffmpeg -f concat -safe 0 -i {fp_list} -c copy {fp_final}'
subprocess.call(cmd, shell=True, cwd=dp_movie) subprocess.call(cmd, shell=True, cwd=dp_movie)
os.remove(fp_list) os.remove(fp_list)
print(f"concatenate_movies: success! Watch here: \n{fp_final}") print(f"concatenate_movies: success! Watch here: {fp_final}")
class MovieReader(): class MovieReader():
@ -202,11 +214,6 @@ class MovieReader():
#%% #%%
if __name__ == "__main__": if __name__ == "__main__":
ms = MovieSaver("/tmp/bubu.mp4", fps=fps)
for img in list_imgs_interp:
ms.write_frame(img)
ms.finalize()
if False:
fps=2 fps=2
list_fp_movies = [] list_fp_movies = []
for k in range(4): for k in range(4):