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

Function slice_video

detrsmpl/utils/ffmpeg_utils.py:1052–1098  ·  view source on GitHub ↗

Temporally crop a video/gif into another video/gif. Args: input_path (str): input video or gif file path. output_path (str): output video of gif file path. start (int, optional): start frame index. Defaults to 0. end (int, optional): end frame index. Exclusive.

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

Source from the content-addressed store, hash-verified

1050
1051
1052def slice_video(input_path: str,
1053 output_path: str,
1054 start: int = 0,
1055 end: Optional[int] = None,
1056 resolution: Optional[Union[Tuple[int, int],
1057 Tuple[float, float]]] = None,
1058 disable_log: bool = False) -> None:
1059 """Temporally crop a video/gif into another video/gif.
1060
1061 Args:
1062 input_path (str): input video or gif file path.
1063 output_path (str): output video of gif file path.
1064 start (int, optional): start frame index. Defaults to 0.
1065 end (int, optional): end frame index. Exclusive.
1066 Could be positive int or negative int or None.
1067 If None, all frames from start till the last frame are included.
1068 Defaults to None.
1069 resolution (Optional[Union[Tuple[int, int], Tuple[float, float]]],
1070 optional): (height, width) of output. Defaults to None.
1071 disable_log (bool, optional): whether close the ffmepg command info.
1072 Defaults to False.
1073 Raises:
1074 FileNotFoundError: check the input path.
1075 FileNotFoundError: check the output path.
1076
1077 Returns:
1078 NoReturn
1079 """
1080 info = vid_info_reader(input_path)
1081 num_frames = int(info['nb_frames'])
1082 start = (min(start, num_frames - 1) + num_frames) % num_frames
1083 end = (min(end, num_frames - 1) +
1084 num_frames) % num_frames if end is not None else num_frames
1085 command = [
1086 'ffmpeg', '-y', '-i', input_path, '-filter_complex',
1087 f'[0]trim=start_frame={start}:end_frame={end}[v0]', '-map', '[v0]',
1088 '-loglevel', 'error', '-vcodec', 'libx264', output_path
1089 ]
1090 if resolution:
1091 height, width = resolution
1092 width += width % 2
1093 height += height % 2
1094 command.insert(1, '-s')
1095 command.insert(2, '%dx%d' % (width, height))
1096 if not disable_log:
1097 print(f'Running \"{" ".join(command)}\"')
1098 subprocess.call(command)
1099
1100
1101def spatial_concat_video(input_path_list: List[str],

Callers

nothing calls this directly

Calls 2

vid_info_readerClass · 0.85
printFunction · 0.50

Tested by

no test coverage detected