moviepy 视频剪切,拼接,音频处理

官网

使用matplotlib — moviepy-cn 文档

案例

from moviepy.editor import *
from moviepy.video.fx import resize
from PIL import Image

file1=r"D:\xy_fs_try\video_to_deal\spider_video\file\vedeo3.mp4"
file2=r"D:\xy_fs_try\video_to_deal\spider_video\file\vedeo4.mp4"
file4=r'D:\xy_fs_try\video_to_deal\spider_video\file\qianlong.mp4'

file3=r'D:\xy_fs_try\video_to_deal\spider_video\file\source.mp4'
file5=r'D:\xy_fs_try\video_to_deal\spider_video\file\source1.mp4'
file6=r'D:\xy_fs_try\video_to_deal\spider_video\file\source2.mp4'

def extract_frames_on_original(video_path, output_path, fps=1):
    original_clip = VideoFileClip(video_path)
    frames = original_clip.iter_frames(fps=fps)
    extracted_clips = []

    for frame in frames:
        extracted_clip = VideoFileClip(video_path).set_duration(1/fps).set_start(len(extracted_clips) / fps)
        extracted_clips.append(extracted_clip)

    # 将原视频和抽帧视频拼接起来
    final_clip = clips_array([[original_clip], [clips_array([extracted_clips])]])
    final_clip.write_videofile(output_path, codec='libx264')

# 使用示例
# video_file = r"D:\xy_fs_try\video_to_deal\spider_video\file\vedeo3.mp4"  # 替换为你的视频文件路径
# output_video = "./extracted_video.mp4"  # 替换为你想要保存的抽帧后的视频路径
# extract_frames_on_original(video_file, output_video, fps=1)  # 抽取帧并生成视频,每秒一帧

def concatenate_videos(video1_path, video2_path, output_path):
    '''视频拼接'''
    audio=VideoFileClip(file3).audio
    video = VideoFileClip(file3).without_audio()
    video2 = VideoFileClip(file5).without_audio()
    video3 = VideoFileClip(file6).without_audio()
    re_video=video.subclip(1,25)
    ba_video=video.subclip(26,56.7)
    # # 拼接两个视频剪辑
    final_clip = concatenate_videoclips([re_video, video2,video3,ba_video],method='compose')
    final_clip=final_clip.set_audio(audio)
    final_clip.write_videofile(output_path, codec='libx264')
    final_clip.reader.close()
    final_clip.audio.reader.close_proc()

video1_file = file3  # 替换为第一个视频文件路径
video2_file = file4  # 替换为第二个视频文件路径
output_video = "./concatenated_video.mp4"  # 替换为输出的合并视频路径
concatenate_videos(video1_file, video2_file, output_video)  # 将两个视频拼接成一个新视频



总结

moviepy使用的还是比较方便的,比起opencv需要自己去遍历处理,视频拼接原理与列表切分原理是一样的,灵活性比较高,内置一些其他的函数叠加,使用还是比较方便的

你可能感兴趣的:(音视频,moviepy)