Python视频编辑-如何修剪视频



是否有一种简单的方法可以在python中修剪视频文件?如果我知道开始时间和结束时间,我希望根据输入将新文件保存为多个更短的文件。

伪代码:

function cut_video(original_vid, start, stop):
    original_vid.start_time(start)
    original_vid.end_time(stop)
    original_vid.trim() #based on times
    new_vid = original_vid
    return new_vid

找到解决方案:

def get_ffmpeg_bin():
    ffmpeg_dir = helper_functions.get_ffmpeg_dir_path()
    FFMPEG_BIN = os.path.join(ffmpeg_dir, "ffmpeg.exe")
    return FFMPEG_BIN

def split_vid_from_path(video_file_path, start_time, durration):
    ffmpeg_binary =  get_ffmpeg_bin()
    output_file_name = get_next_file_name(video_file_path)
    pipe = sp.Popen([ffmpeg_binary,"-v", "quiet", "-y", "-i", video_file_path, "-vcodec", "copy", "-acodec", "copy",
                 "-ss", start_time, "-t", durration, "-sn", output_file_name ])

    pipe.wait()
    return True

sample_vid = os.path.join(get_sample_vid_dir_path(), "Superman-01-The_Mad_Scientist.mp4")
split_vid_from_path(sample_vid, "00:00:00", "00:00:17")

https://www.ffmpeg.org/ffmpeg.html ->本文档允许您在ffmpeg包装器中添加自己的自定义标志。

需要注意的是,您可能想要验证用户是否提供了有效的数据

做这些事情的一个很好的模块是moviepy

示例代码:

from moviepy.editor import *
video = VideoFileClip("myHolidays.mp4").subclip(50,60)
# Make the text. Many more options are available.
txt_clip = ( TextClip("My Holidays 2013",fontsize=70,color='white')
             .set_position('center')
             .set_duration(10) )
result = CompositeVideoClip([video, txt_clip]) # Overlay text on video
result.write_videofile("myHolidays_edited.webm",fps=25) # Many options...

这个模块的文档可以在https://pypi.org/project/moviepy/

找到。

最新更新