Skip to content

Commit

Permalink
Add CLI args for setting video codec and pixel forma
Browse files Browse the repository at this point in the history
  • Loading branch information
3b1b committed Feb 3, 2023
1 parent 12dc124 commit e1bb360
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
22 changes: 21 additions & 1 deletion manimlib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ def parse_cli():
action="store_true",
help="Render to a movie file with an alpha channel",
)
parser.add_argument(
"--vcodec",
help="Video codec to use with ffmpeg",
)
parser.add_argument(
"--pix_fmt",
help="Pixel format to use for the output of ffmpeg, defaults to `yuv420p`",
)
parser.add_argument(
"-q", "--quiet",
action="store_true",
Expand Down Expand Up @@ -392,7 +400,7 @@ def get_output_directory(args: Namespace, custom_config: dict) -> str:


def get_file_writer_config(args: Namespace, custom_config: dict) -> dict:
return {
result = {
"write_to_movie": not args.skip_animations and args.write_file,
"break_into_partial_movies": custom_config["break_into_partial_movies"],
"save_last_frame": args.skip_animations and args.write_file,
Expand All @@ -408,6 +416,18 @@ def get_file_writer_config(args: Namespace, custom_config: dict) -> dict:
"quiet": args.quiet,
}

if args.vcodec:
result["video_codec"] = args.vcodec
elif args.transparent:
result["video_codec"] = 'prores_ks'
elif args.gif:
result["video_codec"] = ''

if args.pix_fmt:
result["pix_fmt"] = args.pix_fmt

return result


def get_window_config(args: Namespace, custom_config: dict, camera_config: dict) -> dict:
# Default to making window half the screen size
Expand Down
25 changes: 12 additions & 13 deletions manimlib/scene/scene_file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def __init__(
quiet: bool = False,
total_frames: int = 0,
progress_description_len: int = 40,
video_codec: str = "libx264",
pixel_format: str = "yuvj422p",
):
self.scene: Scene = scene
self.write_to_movie = write_to_movie
Expand All @@ -63,6 +65,8 @@ def __init__(
self.quiet = quiet
self.total_frames = total_frames
self.progress_description_len = progress_description_len
self.video_codec = video_codec
self.pixel_format = pixel_format

# State during file writing
self.writing_process: sp.Popen | None = None
Expand Down Expand Up @@ -262,19 +266,10 @@ def open_movie_pipe(self, file_path: str) -> None:
'-an', # Tells FFMPEG not to expect any audio
'-loglevel', 'error',
]
if self.movie_file_extension == ".mov":
# This is if the background of the exported
# video should be transparent.
command += [
'-vcodec', 'prores_ks',
]
elif self.movie_file_extension == ".gif":
command += []
else:
command += [
'-vcodec', 'libx264',
'-pix_fmt', 'yuv420p',
]
if self.video_codec:
command += ['-vcodec', self.video_codec]
if self.pixel_format:
command += ['-pix_fmt', self.pixel_format]
command += [self.temp_file_path]
self.writing_process = sp.Popen(command, stdin=sp.PIPE)

Expand All @@ -287,6 +282,10 @@ def open_movie_pipe(self, file_path: str) -> None:
)
self.set_progress_display_description()

def use_fast_encoding(self):
self.video_codec = "libx264rgb"
self.pixel_format = "rgb32"

def begin_insert(self):
# Begin writing process
self.write_to_movie = True
Expand Down

0 comments on commit e1bb360

Please sign in to comment.