在影视以及抖音中,我们都能看到各种慢动作效果。比如武打片为了动作的特效,往往会放慢播放速度等等。今天,我们就通过OpenCV来实现其慢动作效果。
慢动作实现原理:
1.读取视频,将视频分为3部分。第一部分为慢动作前的视频片段,第二部分为慢动作视频片段,第三部分为慢动作后的视频片段2.将这3部分同时保存为视频,但是前后两个片段帧率与原视频相同,慢动作片段将视频的帧率设置为103.最后,将3个短视频合并,合并时,设置慢动作片段某帧循环添加几次。具体循环次数由原视频帧率除于慢动作帧率的取整数决定。
实现慢动作视频既然,我们已经了解了慢动作视频的制作原理。下面,我们直接用代码来实现慢动作的效果。具体代码如下所示:
# 慢动作实现#videoFileName需要实现慢动作的原视频文件名(包括路径)#fps_start慢动作的开始帧率#fps_end慢动作的结束帧率#fps_slow慢动作视频的原始帧率#最后保存视频的名字(包括路径)def video_slow_motion_effect(videoFileName, fps_start, fps_end, fps_slow, outputFileName): cap = cv2.VideoCapture(videoFileName) fps = cap.get(cv2.CAP_PROP_FPS) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'MJPG') videoWriter = cv2.VideoWriter(outputFileName + ".avi", fourcc, fps, (width, height)) video_start_writer = cv2.VideoWriter("video1.avi", fourcc, fps, (width, height)) video_slow_writer = cv2.VideoWriter("video2.avi", fourcc, fps_slow, (width, height)) video_end_writer = cv2.VideoWriter("video3.avi", fourcc, fps, (width, height)) i = 0 while (cap.isOpened()): ret, frame = cap.read() if ret: if i >= fps_start and i <= fps_end: video_slow_writer.write(frame) elif i < fps_start: video_start_writer.write(frame) elif i > fps_end: video_end_writer.write(frame) i += 1 c = cv2.waitKey(1) if c == 27: break else: break cap.release() vc1 = cv2.VideoCapture("video1.avi") vc2 = cv2.VideoCapture("video2.avi") vc3 = cv2.VideoCapture("video3.avi") while vc1.isOpened(): ret, frame = vc1.read() if ret: videoWriter.write(frame) else: break while vc2.isOpened(): ret, frame = vc2.read() if ret: for i in range(int(fps/vc2.get(cv2.CAP_PROP_FPS))): videoWriter.write(frame) else: break while vc3.isOpened(): ret, frame = vc3.read() if ret: videoWriter.write(frame) else: break vc1.release() vc2.release() vc3.release() videoWriter.release()if __name__ == "__main__": video_slow_motion_effect("45.mp4", 20, 50, 5, "output1")
以上就是抖音重复镜头慢动作的详细内容,更多抖音重复镜头慢动作怎么弄内容请关注金符游戏其它相关文章!