如何在Python中连接视频文件?详细实现教程

2021年11月17日14:27:51 发表评论 964 次浏览

Python如何连接视频文件?本文带你了解如何在 Python 中使用 MoviePy 库连接两个或多个视频文件。

如何在Python中连接视频文件?在本教程中,你将学习如何在MoviePy 库的帮助下使用 Python 将两个或多个视频文件连接在一起。

本教程类似于加入音频文件教程,但我们将在本教程中加入视频。

Python连接视频文件示例开始:首先,让我们先安装 MoviePy:

$ pip install moviepy

MoviePy在幕后使用FFmpeg软件,并会在你第一次执行 MoviePy 代码时安装它。打开一个新的 Python 文件并编写以下代码:

def concatenate(video_clip_paths, output_path, method="compose"):
    """Concatenates several video files into one video file
    and save it to `output_path`. Note that extension (mp4, etc.) must be added to `output_path`
    `method` can be either 'compose' or 'reduce':
        `reduce`: Reduce the quality of the video to the lowest quality on the list of `video_clip_paths`.
        `compose`: type help(concatenate_videoclips) for the info"""
    # create VideoFileClip object for each video file
    clips = [VideoFileClip(c) for c in video_clip_paths]
    if method == "reduce":
        # calculate minimum width & height across all clips
        min_height = min([c.h for c in clips])
        min_width = min([c.w for c in clips])
        # resize the videos to the minimum
        clips = [c.resize(newsize=(min_width, min_height)) for c in clips]
        # concatenate the final video
        final_clip = concatenate_videoclips(clips)
    elif method == "compose":
        # concatenate the final video with the compose method provided by moviepy
        final_clip = concatenate_videoclips(clips, method="compose")
    # write the output video file
    final_clip.write_videofile(output_path)

好的,这里有很多内容要介绍。concatenate()我们编写的函数接受视频文件列表(video_clip_paths)、输出视频文件路径和加入方法。

Python如何连接视频文件?首先,我们遍历视频文件列表并使用VideoFileClip()来自 MoviePy 的对象加载它们。方法参数接受两个可能的值:

  • reduce:此方法将视频的质量降低到列表中的最低值。例如,如果一个视频是1280x720另一个是320x240,则生成的文件将为320x240. 这就是为什么我们使用resize()最低高度和宽度的方法。
  • compose:MoviePy 建议我们在对不同质量的视频进行拼接时使用此方法。最终剪辑具有列表中最高剪辑的高度和最宽剪辑的宽度。所有尺寸较小的剪辑都将居中显示。

随意使用两者,看看哪一个最适合你的情况。

现在让我们使用argparse模块来解析命令行参数:

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(
        description="Simple Video Concatenation script in Python with MoviePy Library")
    parser.add_argument("-c", "--clips", nargs="+",
                        help="List of audio or video clip paths")
    parser.add_argument("-r", "--reduce", action="store_true", 
                        help="Whether to use the `reduce` method to reduce to the lowest quality on the resulting clip")
    parser.add_argument("-o", "--output", help="Output file name")
    args = parser.parse_args()
    clips = args.clips
    output_path = args.output
    reduce = args.reduce
    method = "reduce" if reduce else "compose"
    concatenate(clips, output_path, method)

在以上Python连接视频文件示例中,由于我们期望将视频文件列表连接在一起,因此我们需要传递"+"nargs解析器以接受一个或多个视频文件。

让我们通过--help

$ python concatenate_video.py --help

输出:

usage: concatenate_video.py [-h] [-c CLIPS [CLIPS ...]] [-r REDUCE] [-o OUTPUT]

Simple Video Concatenation script in Python with MoviePy Library

optional arguments:
  -h, --help            show this help message and exit
  -c CLIPS [CLIPS ...], --clips CLIPS [CLIPS ...]
                        List of audio or video clip paths
  -r REDUCE, --reduce REDUCE
                        Whether to use the `reduce` method to reduce to the lowest quality on the resulting clip
  -o OUTPUT, --output OUTPUT
                        Output file name

让我们测试一下:

$ python concatenate_video.py -c zoo.mp4 directed-by-robert.mp4 -o output.mp4

Python如何连接视频文件?在这里,我加入zoo.mp4directed-by-robert.mp4文件来生成output.mp4。请注意,顺序很重要,因此你需要按照你想要的顺序传递它们。你可以根据需要传递任意数量的视频文件。在output.mp4将出现在当前目录下,你会看到类似的输出:

Moviepy - Building video output.mp4.
MoviePy - Writing audio in outputTEMP_MPY_wvf_snd.mp3
MoviePy - Done.
Moviepy - Writing video output.mp4

Moviepy - Done !
Moviepy - video ready output.mp4

这是输出视频(视频省略,可以使用自己的视频尝试):

你还可以通过reduce以下命令使用该方法:

$ python concatenate_video.py -c zoo.mp4 directed-by-robert.mp4 --reduce -o output-reduced.mp4

如何在Python中连接视频文件?好吧,以上就是全部内容了,到此完结。我希望本教程对你的编程之旅有所帮助!

木子山

发表评论

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