moviepy初体验

替换视频音频,裁剪视频:

from moviepy.editor import *
tid='111'
video = VideoFileClip(r"./{}/{}.mp4".format(tid, tid), audio=False)
v_duration = video.duration #视频时间长短
s_d = 0 
v_d = v_duration 
audio_n = AudioFileClip(r"./a.mp3")
audio = audio_n.subclip(s_d + 20, v_d + s_d + 20) 

v_w, v_h = video.size #视频长宽
h__w = v_h / v_w
if v_h == v_w:
	#正方形的剪成规定尺寸
    video = video.subclip(s_d, v_d + s_d).resize((1280, 1280))
    video = video.crop(y_center=640, height=720)
elif h__w == 0.5625:
    video = video.subclip(s_d, v_d + s_d).resize((1280, 720))
elif v_h / v_w < 0.5625:
	#不符合比例的,先resize,然后再crop裁剪
    video = video.subclip(s_d, v_d + s_d).resize(height=720)
    video = video.crop(x1=0,width=1280, y1=0,height=720)
elif v_h / v_w > 0.5625:
    video = video.subclip(s_d, v_d + s_d).resize(width=1280)
    video = video.crop(y_center=video.size[1]/2, height=720)
result = CompositeVideoClip([video]).set_audio(audio)
#保存文件
result.write_videofile(r"./{}/{}_30.mp4".format(tid, tid), fps=25)
#再拿result流裁剪,不用再重新打开文件。
video_5 = result.subclip(4, random.randint(8, 9))
result_5 = CompositeVideoClip([video_5])
result_5.write_videofile(r"./{}/{}_5.mp4".format(tid, tid), fps=25, codec=None)
#关闭视频流
video.reader.close()
del video.reader

旋转视频:

from moviepy.video.fx.rotate import rotate
#这里旋转完之后,长宽没变化,可能是个bug,参考:https://github.com/Zulko/moviepy/issues/586
video= rotate(video, 90)
#需要重新调整大小
video = video.resize(video.size[::-1])

参考:

http://blog.csdn.net/summermaoz/article/details/56058056# python+ffmpeg截取视频段
https://baijiahao.baidu.com/s?id=1561527904480690&wfr=spider&for=pc#使用Python对视频的简单截取
http://blog.csdn.net/xlianglei/article/details/68925377#win10 + anaconda +moviepy + mayavi + ffmpeg
http://blog.csdn.net/myboyliu2007/article/details/53928746#通过python安装ffmpge
http://www.cppblog.com/socketref/archive/2014/04/14/206584.html#python中利用ctype包装ffmpeg
http://zulko.github.io/moviepy/examples/examples.html#examples
bug:
https://www.youtube.com/watch?v=3Md9MkOLYL4
https://stackoverflow.com/questions/43966523/getting-oserror-winerror-6-the-handle-is-invalid-in-videofileclip-function

 

 

你可能感兴趣的:(moviepy初体验)