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

jq6vz3qz  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(128)

我希望2个或更多(数量不等)的视频被"合并"在一起(只是这样一个播放后,对方)
我尝试的是:

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)

但是由于某种原因,后来列表不再由VideoFileClips组成,而是由包含Path的String组成,这使得以后获取剪辑的持续时间变得混乱。
有没有别的办法?

1l5u6lss

1l5u6lss1#

我把这个代码从链接我发现,而从谷歌搜索视频拼接

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)

变元分析程序

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 concatenate_video.py -c zoo.mp4 directed-by-robert.mp4 -o output.mp4

**一个

相关问题