简体   繁体   English

连接 Python 中的多个视频片段?

[英]Concatenating multiple videoclips in Python?

I want 2 or more (a varying amount) of videos to be "merged" together (just so one plays after each other)我想要将 2 个或更多(数量不同)的视频“合并”在一起(这样一个视频就可以一个接一个地播放)

What I have tried is:我试过的是:


from moviepy.editor import concatenate_videoclips, VideoFileClip

allvideos = list()

for file in os.listdir(".\\files"):
    allvideos.append(VideoFileClip(".\\files\\" + file))

concatenate_videoclips(allvideos).write_videofile(output)

but for some reason, later, the list does not consist of VideoFileClips anymore but rather of Strings containing the Path, which messes up getting the duration of the Clip later.但出于某种原因,后来,该列表不再包含 VideoFileClips,而是包含包含路径的字符串,这会弄乱稍后获取剪辑的持续时间。

Is there an alternate way of doing this?有没有其他方法可以做到这一点?

I take this code from the link I found while searching video concatenation from google我从从谷歌搜索视频连接时找到的链接中获取这段代码

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)

argument parser参数解析器

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)

example例子

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

https://www.thepythoncode.com/article/concatenate-video-files-in-python https://www.thepythoncode.com/article/concatenate-video-files-in-python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM