Convert a video to a folder of images. Args: input_path (str): video file path output_folder (str): output folder to store the images resolution (Optional[Tuple[int, int]], optional): (height, width) of output. defaults to None. img_format (str, optio
(input_path: str,
output_folder: str,
resolution: Optional[Union[Tuple[int, int],
Tuple[float, float]]] = None,
img_format: str = '%06d.png',
start: int = 0,
end: Optional[int] = None,
disable_log: bool = False)
| 603 | |
| 604 | |
| 605 | def video_to_images(input_path: str, |
| 606 | output_folder: str, |
| 607 | resolution: Optional[Union[Tuple[int, int], |
| 608 | Tuple[float, float]]] = None, |
| 609 | img_format: str = '%06d.png', |
| 610 | start: int = 0, |
| 611 | end: Optional[int] = None, |
| 612 | disable_log: bool = False) -> None: |
| 613 | """Convert a video to a folder of images. |
| 614 | |
| 615 | Args: |
| 616 | input_path (str): video file path |
| 617 | output_folder (str): output folder to store the images |
| 618 | resolution (Optional[Tuple[int, int]], optional): |
| 619 | (height, width) of output. defaults to None. |
| 620 | img_format (str, optional): format of images to be read. |
| 621 | Defaults to '%06d.png'. |
| 622 | start (int, optional): start frame index. Inclusive. |
| 623 | If < 0, will be converted to frame_index range in [0, frame_num]. |
| 624 | Defaults to 0. |
| 625 | end (int, optional): end frame index. Exclusive. |
| 626 | Could be positive int or negative int or None. |
| 627 | If None, all frames from start till the last frame are included. |
| 628 | Defaults to None. |
| 629 | disable_log (bool, optional): whether close the ffmepg command info. |
| 630 | Defaults to False. |
| 631 | Raises: |
| 632 | FileNotFoundError: check the input path |
| 633 | FileNotFoundError: check the output path |
| 634 | |
| 635 | Returns: |
| 636 | None |
| 637 | """ |
| 638 | check_input_path( |
| 639 | input_path, |
| 640 | allowed_suffix=['.mp4'], |
| 641 | tag='input video', |
| 642 | path_type='file') |
| 643 | prepare_output_path( |
| 644 | output_folder, |
| 645 | allowed_suffix=[], |
| 646 | tag='output image folder', |
| 647 | path_type='dir', |
| 648 | overwrite=True) |
| 649 | info = vid_info_reader(input_path) |
| 650 | num_frames = int(info['nb_frames']) |
| 651 | start = (min(start, num_frames - 1) + num_frames) % num_frames |
| 652 | end = (min(end, num_frames - 1) + |
| 653 | num_frames) % num_frames if end is not None else num_frames |
| 654 | |
| 655 | command = [ |
| 656 | 'ffmpeg', '-i', input_path, '-filter_complex', |
| 657 | f'[0]trim=start_frame={start}:end_frame={end}[v0]', '-map', '[v0]', |
| 658 | '-f', 'image2', '-v', 'error', '-start_number', '0', '-threads', '1', |
| 659 | f'{output_folder}/{img_format}' |
| 660 | ] |
| 661 | if resolution: |
| 662 | height, width = resolution |
no test coverage detected