如何在Python中反转视频?详细实现示例教程

2021年11月17日14:20:44 发表评论 1,037 次浏览

本文带你了解如何通过使用 Python 中的 MoviePy 库提取帧并以倒序加载它们来反转视频。

视频等数字媒体创建了许多创意选项,我们可以在其中处理视频和音频文件并使用它们创建很棒的效果。许多媒体处理程序和工具都提供倒车功能。

如何在Python中反转视频?在本教程中,你将学习如何使用MoviePy 库在 Python 中反转视频。

Python如何反转视频?你将在本教程中看到的代码背后的基本思想是,我们使用可配置的 fps 参数从视频中提取所有帧,然后以相反的顺序将这些帧加载回视频中。

完整的Python反转视频示例介绍和解析如下:

首先,让我们安装所需的库:

$ pip install tqdm moviepy numpy

现在让我们从导入我们的模块开始:

from moviepy.editor import VideoFileClip, ImageSequenceClip
import numpy as np
import os
from datetime import timedelta, datetime
from glob import glob
from tqdm import tqdm
import shutil

接下来,为了避免冗余,我将从本教程中引入以下代码:

# i.e if video of duration 30 seconds, saves 10 frame per second = 300 frames saved in total
SAVING_FRAMES_PER_SECOND = 30

def format_timedelta(td):
    """Utility function to format timedelta objects in a cool way (e.g 00:00:20.05) 
    omitting microseconds and retaining milliseconds"""
    result = str(td)
    try:
        result, ms = result.split(".")
    except ValueError:
        return result + ".00".replace(":", "-")
    ms = int(ms)
    ms = round(ms / 1e4)
    return f"{result}.{ms:02}".replace(":", "-")


def extract_frames(video_file, verbose=1):
    # load the video clip
    video_clip = VideoFileClip(video_file)
    # make a folder by the name of the video file
    filename, _ = os.path.splitext(video_file)
    if not os.path.isdir(filename):
        os.mkdir(filename)
    # if the SAVING_FRAMES_PER_SECOND is above video FPS, then set it to FPS (as maximum)
    saving_frames_per_second = min(video_clip.fps, SAVING_FRAMES_PER_SECOND)
    # if SAVING_FRAMES_PER_SECOND is set to 0, step is 1/fps, else 1/SAVING_FRAMES_PER_SECOND
    step = 1 / video_clip.fps if saving_frames_per_second == 0 else 1 / saving_frames_per_second
    iteration = np.arange(0, video_clip.duration, step)
    if verbose:
        iteration = tqdm(iteration, desc="Extracting video frames")
    # iterate over each possible frame
    for current_duration in iteration:
        # format the file name and save it
        frame_duration_formatted = format_timedelta(timedelta(seconds=current_duration)).replace(":", "-")
        frame_filename = os.path.join(filename, f"frame{frame_duration_formatted}.jpg")
        # save the frame with the current duration
        video_clip.save_frame(frame_filename, current_duration)
    return filename, video_clip.fps

Python如何反转视频?你将在提取帧教程 中获得很多详细信息。但是,简而言之,该extract_frames()函数接受视频文件路径作为参数,并将相应时长的帧提取到以原始视频文件名命名的文件夹中。最后,它返回该文件夹名称。

现在,让我们创建一个函数,以相反的顺序读取这些提取帧并将它们保存为反转视频,如下Python反转视频示例代码:

def reverse_video(frames_path, video_fps, remove_extracted_frames=True):
    frame_files = glob(os.path.join(frames_path, "*"))
    # sort by duration in descending order
    frame_files = sorted(frame_files, key=lambda d: datetime.strptime(d.split("frame")[1], "%H-%M-%S.%f.jpg"), reverse=True)
    # calculate the FPS, getting the minimum between the original FPS and the parameter we set
    saving_frames_per_second = min(video_fps, SAVING_FRAMES_PER_SECOND)
    if saving_frames_per_second == 0:
        # if the parameter is set to 0, automatically set it to the original video fps
        saving_frames_per_second = video_fps
    print("Saving the video with FPS:", saving_frames_per_second)
    # load the frames into a image sequence clip (MoviePy)
    image_sequence_clip = ImageSequenceClip(frame_files, fps=saving_frames_per_second)
    # write the video file to disk
    output_filename = f"{frames_path}-inverted.mp4"
    image_sequence_clip.write_videofile(output_filename)
    if remove_extracted_frames:
        # if set to True, then remove the folder that contain the extracted frames
        shutil.rmtree(frames_path)

如何在Python中反转视频?该reverse_video()函数需要包含由前一个函数提取的视频帧的文件夹名称作为参数。我们使用glob模块中的glob()函数来获取帧的所有文件名。

接下来,我们按持续时间降序对这些帧文件进行排序。之后,我们将这些帧以相反的顺序ImageSequenceClip()从MoviePy传递给对象,并将FPS 设置为SAVING_FRAMES_PER_SECOND我们在帧提取过程中使用的最小FPS和原始视频FPS,原因是我们设置了比原始FPS 更高的FPS视频 FPS,生成的视频将被加速。

然后我们使用该write_videofile()方法将反转的视频保存到磁盘上的视频文件中。

如果你将 设置remove_extracted_framesTrue(作为默认值),提取的帧所在的文件夹将与其内容一起被删除。

最后,让我们使用这些函数来完成我们的任务:

if __name__ == "__main__":
    import sys
    video_file = sys.argv[1]
    frames_folder_path, video_fps = extract_frames(video_file)
    reverse_video(frames_folder_path, video_fps=video_fps)

我们完成了!让我们用 TENET 电影中的一个场景的 YouTube 视频来试一试:

$ python reverse_video.py Tenet-the-breach-scene-in-forward.mp4

这是Python反转视频示例代码的输出:

Extracting video frames: 100%|██████████████████████████████████████████████████████████████████████████████████████████| 485/485 [00:10<00:00, 47.71it/s]
Moviepy - Building video Tenet-the-breach-scene-in-forward-inverted.mp4.
Moviepy - Writing video Tenet-the-breach-scene-in-forward-inverted.mp4

Moviepy - Done !
Moviepy - video ready Tenet-the-breach-scene-in-forward-inverted.mp4

并且反转视频出现在当前目录!

结论

Python如何反转视频?如果你的视频很长,那么一定要降低FPS(SAVING_FRAMES_PER_SECOND参数),我已经设置低到10,如果你觉得输出视频有延迟可以增加它,这会增加文件的大小视频,以及程序提取帧并以相反顺序加载它们的执行时间。

很明显,输出的视频是没有声音的,你可以使用AudioClip()loaded from audio(可能是从原始视频中提取的音频),简单的设置image_sequence_clip.audio为这个新创建的AudioClip()对象,然后继续保存视频的相同过程。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: