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

时间:2014-03-29 13:56:28

标签: python video video-processing

是否有一种简单的方法可以在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

2 个答案:

答案 0 :(得分:3)

找到解决方案:

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包装器中。

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

答案 1 :(得分:0)

电影是完成这些任务的一个不错的模块。

示例代码:

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/

中找到