def prepare_video(
    vid,
    thumbnail_frame_ts=0.0,
    max_size=(1080, 1350),
    aspect_ratios=(4.0 / 5.0, 90.0 / 47.0),
    max_duration=60.0,
    save_path=None,
    skip_reencoding=False,
    **kwargs
):
    """
    Prepares a video file for posting.
    Defaults for size and aspect ratio from https://help.instagram.com/1469029763400082

    :param vid: file path
    :param thumbnail_frame_ts: the frame of clip corresponding to time t (in seconds) to be used as the thumbnail
    :param max_size: tuple of (max_width,  max_height)
    :param aspect_ratios: single float value or tuple of (min_ratio, max_ratio)
    :param max_duration: maximum video duration in seconds
    :param save_path: optional output video file path
    :param skip_reencoding: if set to True, the file will not be re-encoded
        if there are no modifications required. Default: False.
    :param kwargs:
         - **min_size**: tuple of (min_width,  min_height)
         - **progress_bar**: bool flag to show/hide progress bar
         - **save_only**: bool flag to return only the path to the saved video file. Requires save_path be set.
         - **preset**: Sets the time that FFMPEG will spend optimizing the compression.
         Choices are: ultrafast, superfast, veryfast, faster, fast, medium,
         slow, slower, veryslow, placebo. Note that this does not impact
         the quality of the video, only the size of the video file. So
         choose ultrafast when you are in a hurry and file size does not matter.
    :return:
    """
    from moviepy.video.fx.all import crop, resize
    from moviepy.video.io.VideoFileClip import VideoFileClip

    min_size = kwargs.pop("min_size", (612, 320))
    progress_bar = True if kwargs.pop("progress_bar", None) else False
    save_only = kwargs.pop("save_only", False)
    preset = kwargs.pop("preset", "medium")
    if save_only and not save_path:
        raise ValueError('"save_path" cannot be empty.')
    if save_path:
        if not save_path.lower().endswith(".mp4"):
            raise ValueError("You must specify a .mp4 save path")

    vid_is_modified = False  # flag to track if re-encoding can be skipped

    temp_video_file = tempfile.NamedTemporaryFile(
        prefix="ipae_", suffix=".mp4", delete=False
    )

    if is_remote(vid):
        # Download remote file
        res = requests.get(vid, timeout=5)
        temp_video_file.write(res.content)
        video_src_filename = temp_video_file.name
    else:
        shutil.copyfile(vid, temp_video_file.name)
        video_src_filename = vid

    vidclip = VideoFileClip(temp_video_file.name)

    if vidclip.duration < 3 * 1.0:
        raise ValueError("Duration is too short")

    if vidclip.duration > max_duration * 1.0:
        vidclip = vidclip.subclip(0, max_duration)
        vid_is_modified = True

    if thumbnail_frame_ts > vidclip.duration:
        raise ValueError("Invalid thumbnail frame")

    if aspect_ratios:
        crop_box = calc_crop(aspect_ratios, vidclip.size)
        if crop_box:
            vidclip = crop(
                vidclip, x1=crop_box[0], y1=crop_box[1], x2=crop_box[2], y2=crop_box[3]
            )
            vid_is_modified = True

    if max_size or min_size:
        new_size = calc_resize(max_size, vidclip.size, min_size=min_size)
        if new_size:
            vidclip = resize(vidclip, newsize=new_size)
            vid_is_modified = True

    temp_vid_output_file = tempfile.NamedTemporaryFile(
        prefix="ipae_", suffix=".mp4", delete=False
    )
    if vid_is_modified or not skip_reencoding:
        # write out
        vidclip.write_videofile(
            temp_vid_output_file.name,
            codec="libx264",
            audio=True,
            audio_codec="aac",
            verbose=False,
            progress_bar=progress_bar,
            preset=preset,
            remove_temp=True,
        )
    else:
        # no reencoding
        shutil.copyfile(video_src_filename, temp_vid_output_file.name)

    if save_path:
        shutil.copyfile(temp_vid_output_file.name, save_path)

    # Temp thumbnail img filename
    temp_thumbnail_file = tempfile.NamedTemporaryFile(
        prefix="ipae_", suffix=".jpg", delete=False
    )
    vidclip.save_frame(temp_thumbnail_file.name, t=thumbnail_frame_ts)

    video_duration = vidclip.duration
    video_size = vidclip.size
    del vidclip  # clear it out

    video_thumbnail_content = temp_thumbnail_file.read()

    if not save_only:
        video_content_len = os.path.getsize(temp_vid_output_file.name)
        video_content = temp_vid_output_file.read()
    else:
        video_content_len = os.path.getsize(save_path)
        video_content = save_path  # return the file path instead

    if video_content_len > 50 * 1024 * 1000:
        raise ValueError("Video file is too big.")

    return video_content, video_size, video_duration, video_thumbnail_content

    Prepares a video file for posting.
    Defaults for size and aspect ratio from https://help.instagram.com/1469029763400082

    :param vid: file path
    :param thumbnail_frame_ts: the frame of clip corresponding to time t (in seconds) to be used as the thumbnail
    :param max_size: tuple of (max_width,  max_height)
    :param aspect_ratios: single float value or tuple of (min_ratio, max_ratio)
    :param max_duration: maximum video duration in seconds
    :param save_path: optional output video file path
    :param skip_reencoding: if set to True, the file will not be re-encoded
        if there are no modifications required. Default: False.
    :param kwargs:
         - **min_size**: tuple of (min_width,  min_height)
         - **progress_bar**: bool flag to show/hide progress bar
         - **save_only**: bool flag to return only the path to the saved video file. Requires save_path be set.
         - **preset**: Sets the time that FFMPEG will spend optimizing the compression.
         Choices are: ultrafast, superfast, veryfast, faster, fast, medium,
         slow, slower, veryslow, placebo. Note that this does not impact
         the quality of the video, only the size of the video file. So
         choose ultrafast when you are in a hurry and file size does not matter.
    :return:
    
instagrapi/image_util.py