Ejemplo n.º 1
0
def concat_videos_reencode(in_paths: List[str],
                           out_path: str,
                           debug: bool = False) -> bool:
    if len(in_paths) == 0:
        return False
    elif len(in_paths) == 1:
        sh.cp(in_paths[0], out_path)

        return path.exists(out_path)

    cmd = 'ffmpeg -y'

    for path_ in in_paths:
        cmd += ' -i ' + sh.path(path_)

    cmd += ' -filter_complex "'

    for i in range(len(in_paths)):
        cmd += '[{}:v] [{}:a] '.format(i, i)

    cmd += 'concat=n={}:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" '.format(
        len(in_paths)) + sh.path(out_path)

    sh.sh(cmd, debug=debug)

    return path.exists(out_path)
Ejemplo n.º 2
0
def concat_videos_copy(in_paths: List[str],
                       out_path: str,
                       debug: bool = False) -> bool:
    if len(in_paths) == 0:
        return False
    elif len(in_paths) == 1:
        sh.cp(in_paths[0], out_path)

        return True

    temp_txt_path = path.join(kpath.folder_path_of_file(out_path),
                              '__temp_list.txt')
    temp_txt_content = ''

    for in_path in in_paths:
        if len(temp_txt_content) > 0:
            temp_txt_content += '\n'

        temp_txt_content += 'file \'' + in_path + '\''

    with open(temp_txt_path, 'w') as f:
        f.write(temp_txt_content)

    sh.sh('ffmpeg -y -f concat -safe 0 -i ' + sh.path(temp_txt_path) +
          ' -c copy ' + sh.path(out_path),
          debug=debug)
    kpath.remove(temp_txt_path)

    return path.exists(out_path)
Ejemplo n.º 3
0
def convert_video_to_16_9(in_path: str,
                          out_path: str,
                          debug: bool = False) -> bool:
    w, h = ffprobe.video_resolution(in_path)

    if h is None or w is None:
        return False

    if h == 1080 and w == 1920:
        sh.cp(in_path, out_path)

        return True

    if w / h < 16 / 9:
        sh.sh(
            'ffmpeg -y -i ' + sh.path(in_path) +
            " -vf 'split[original][copy];[copy]scale=1920:-1,setsar=1:1,crop=h=1080,gblur=sigma=75[blurred];[original]scale=-1:1080[original_resized];[blurred][original_resized]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2' "
            + sh.path(out_path),
            debug=debug)
    else:
        sh.sh(
            'ffmpeg -y -i ' + sh.path(in_path) +
            " -vf 'split[original][copy];[copy]scale=-1:1080,setsar=1:1,crop=w=1920,gblur=sigma=75[blurred];[original]scale=1920:-1[original_resized];[blurred][original_resized]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2' "
            + sh.path(out_path),
            debug=debug)

    return path.exists(out_path)
Ejemplo n.º 4
0
def create_video_from_image_paths(image_paths: List[str],
                                  output_file_path: str,
                                  seconds_per_image: float = 3.0,
                                  temp_folder_path: Optional[str] = None,
                                  debug: bool = False) -> bool:
    temp_folder_path = temp_folder_path or path.join(
        kpath.folder_path_of_file(), '.__ffmpeg_images_temp')
    import os
    os.makedirs(temp_folder_path, exist_ok=True)

    file_base_name = 'image'
    i = 1
    file_extension = kpath.extension(image_paths[0], include_dot=True)

    for image_path in image_paths:
        new_image_path = path.join(
            temp_folder_path,
            file_base_name + '{:03d}'.format(i) + file_extension)
        sh.cp(image_path, new_image_path)
        i += 1

    res = create_video_from_images(temp_folder_path,
                                   output_file_path,
                                   seconds_per_image=seconds_per_image,
                                   file_base_name=file_base_name,
                                   file_extension=file_extension,
                                   debug=debug)
    kpath.remove(temp_folder_path)

    return res
Ejemplo n.º 5
0
def rotate_video(path_in: str,
                 path_out: str,
                 times_90_degrees: int,
                 debug: bool = False) -> bool:
    times_90_degrees = times_90_degrees % 4

    rot_val = 1 if times_90_degrees > 0 else 2

    if times_90_degrees == 0:
        sh.cp(path_in, path_out)
    else:
        sh.sh('ffmpeg -y -i {} -vf "{}" {}'.format(
            sh.path(path_in), ','.join([
                'transpose={}'.format(rot_val) for _ in range(times_90_degrees)
            ]), sh.path(path_out)),
              debug=debug)

    return path.exists(path_out)
Ejemplo n.º 6
0
def concat_videos_loop(in_paths: List[str],
                       out_path: str,
                       video_duration: float,
                       randomize_videos: Optional[bool] = False,
                       debug: bool = False) -> bool:
    if len(in_paths) == 0:
        return False
    elif len(in_paths) == 1:
        sh.cp(in_paths[0], out_path)

        return True

    temp_txt_path = path.join(kpath.folder_path_of_file(out_path),
                              '__temp_video_paths.txt')
    current_video_duration = 0
    final_text = ''

    while current_video_duration < video_duration:
        if randomize_videos:
            random.shuffle(in_paths)

        for vid_path in in_paths:
            final_text += 'file ' + '\'' + vid_path + '\'\n'
            subvid_duration = ffprobe.get_duration(vid_path)
            current_video_duration += subvid_duration

            if current_video_duration >= video_duration:
                with open(temp_txt_path, 'w') as f:
                    f.write(final_text)

                break

    sh.sh('ffmpeg -y -f concat -safe 0 -i ' + sh.path(temp_txt_path) +
          ' -c copy ' + sh.path(out_path),
          debug=debug)
    kpath.remove(temp_txt_path)

    return path.exists(out_path)