def save_video_fragment(
    video: VideoFile,
    start: float,
    end: float,
    output: str,
    format: Optional[str] = None,
) -> VideoFile:
    """
    Saves video interval as a new video file. If output is a remote path,
    the video file will be uploaded to the remote storage.

    Args:
        video (VideoFile): Video file object.
        start (float): Start time in seconds.
        end (float): End time in seconds.
        output (str): Output path, can be a local path or a remote path.
        format (str, optional): Output format (default: None). If not provided,
                                the format will be inferred from the video fragment
                                file extension.

    Returns:
        VideoFile: Video fragment model.
    """
    if start < 0 or end < 0 or start >= end:
        raise ValueError(f"Invalid time range: ({start:.3f}, {end:.3f})")

    if format is None:
        format = video.get_file_ext()

    start_ms = int(start * 1000)
    end_ms = int(end * 1000)
    output_file = posixpath.join(
        output, f"{video.get_file_stem()}_{start_ms:06d}_{end_ms:06d}.{format}"
    )

    temp_dir = tempfile.mkdtemp()
    try:
        output_file_tmp = posixpath.join(temp_dir, posixpath.basename(output_file))
        ffmpeg.input(
            video.get_local_path(),
            ss=start,
            to=end,
        ).output(output_file_tmp).run(quiet=True)

        with open(output_file_tmp, "rb") as f:
            return VideoFile.upload(f.read(), output_file, catalog=video._catalog)
    finally:
        shutil.rmtree(temp_dir)

    Saves video interval as a new video file. If output is a remote path,
    the video file will be uploaded to the remote storage.

    Args:
        video (VideoFile): Video file object.
        start (float): Start time in seconds.
        end (float): End time in seconds.
        output (str): Output path, can be a local path or a remote path.
        format (str, optional): Output format (default: None). If not provided,
                                the format will be inferred from the video fragment
                                file extension.

    Returns:
        VideoFile: Video fragment model.
    
src/datachain/lib/video.py