conda install imageio
import imageio
timeF = 10
reader = imageio.get_reader('imageio:cockatoo.mp4')
for i, im in enumerate(reader):
if (i % timeF == 0):
imageio.imwrite('./image/'+str(i)+'.jpg',im[:, :, 1])
import imageio
import glob
img_paths = glob.glob("./image/*.jpg")
writer = imageio.get_writer('./out.mp4', fps=25)
for img_path in img_paths:
im = imageio.imread(img_path)
writer.append_data(im)
writer.close()
import cv2
vc = cv2.VideoCapture('project_video.mp4') # 读入视频文件
c = 1
if vc.isOpened(): # 判断是否正常打开
rval, frame = vc.read()
else:
rval = False
timeF = 10 # 视频帧计数间隔频率
while rval: # 循环读取视频帧
rval, frame = vc.read()
if (c % timeF == 0): # 每隔timeF帧进行存储操作
cv2.imwrite('image/' + str(c) + '.jpg', frame) # 存储为图像
c = c + 1
cv2.waitKey(1)
vc.release()
MoviePy是Python中的一个视频处理库,能够对音频、视频、图片进行剪辑、合并、输出等处理.支持大多数图文格式.
MoviePy主要用于批量自动化的视频处理(比如批量创建GIF作为视频封面图),以及自动化一些繁琐的视频编辑任务.
当然MoviePy也可以用来做逐帧视频处理以及视频格式转换等任务,不过性能不如OpenCV、imageio、ffmpeg等.
moviepy的官方文档在这里:
https://zulko.github.io/moviepy/
源代码仓库在这里:
https://github.com/Zulko/moviepy
moviepy是基于ffmpeg的,所以先要保证机器中装有ffmpeg,如果没有的话可以使用imageio下载ffmpeg
conda install imageio
然后在终端中执行如下代码安装ffmpeg
import imageio
imageio.plugins.ffmpeg.download()
或者,可以参考ffmpeg官网的安装方法.
https://www.ffmpeg.org/
安装moviepy
pip install moviepy
from moviepy.editor import VideoFileClip
output_file = 'output.mp4'
test_clip = VideoFileClip("input.mp4")
new_clip = test_clip.fl_image(process_image)
new_clip.write_videofile(output_file, audio=False)
def process_image(image):
return image