MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / video_to_gif

Function video_to_gif

detrsmpl/utils/ffmpeg_utils.py:549–602  ·  view source on GitHub ↗

Convert a video to a gif file. Args: input_path (str): video file path. output_path (str): gif file path. resolution (Optional[Union[Tuple[int, int], Tuple[float, float]]], optional): (height, width) of the output video. Defaults to None.

(
    input_path: str,
    output_path: str,
    resolution: Optional[Union[Tuple[int, int], Tuple[float, float]]] = None,
    fps: Union[float, int] = 15,
    disable_log: bool = False,
)

Source from the content-addressed store, hash-verified

547
548
549def video_to_gif(
550 input_path: str,
551 output_path: str,
552 resolution: Optional[Union[Tuple[int, int], Tuple[float, float]]] = None,
553 fps: Union[float, int] = 15,
554 disable_log: bool = False,
555) -> None:
556 """Convert a video to a gif file.
557
558 Args:
559 input_path (str): video file path.
560 output_path (str): gif file path.
561 resolution (Optional[Union[Tuple[int, int], Tuple[float, float]]],
562 optional): (height, width) of the output video.
563 Defaults to None.
564 fps (Union[float, int], optional): frames per second. Defaults to 15.
565 disable_log (bool, optional): whether close the ffmepg command info.
566 Defaults to False.
567
568 Raises:
569 FileNotFoundError: check the input path.
570 FileNotFoundError: check the output path.
571
572 Returns:
573 None.
574 """
575 check_input_path(
576 input_path,
577 allowed_suffix=['.mp4'],
578 tag='input video',
579 path_type='file')
580 prepare_output_path(
581 output_path,
582 allowed_suffix=['.gif'],
583 tag='output gif',
584 path_type='file',
585 overwrite=True)
586
587 info = vid_info_reader(input_path)
588 duration = info['duration']
589 if resolution:
590 height, width = resolution
591 else:
592 width, height = int(info['width']), int(info['height'])
593
594 command = [
595 'ffmpeg', '-r',
596 str(info['r_frame_rate']), '-i', input_path, '-r', f'{fps}', '-s',
597 f'{width}x{height}', '-loglevel', 'error', '-t', f'{duration}',
598 '-threads', '4', '-y', output_path
599 ]
600 if not disable_log:
601 print(f'Running \"{" ".join(command)}\"')
602 subprocess.call(command)
603
604
605def video_to_images(input_path: str,

Callers

nothing calls this directly

Calls 4

check_input_pathFunction · 0.90
prepare_output_pathFunction · 0.90
vid_info_readerClass · 0.85
printFunction · 0.50

Tested by

no test coverage detected