赞
踩
moviepy包是一个可以用于处理视频的很有用的库,由于国内资料比较少,在此做一个记录。首先,需要导入moviepy的包
from moviepy.editor import *
vfc = VideoFileClip(path)# path为输入视频路径
由此得到了一个VideoFileClip的对象,可通过对此对象的操作,得到想要的输出视频结果。
vfc.subclip(time_1, time_2)
time_1和time_2为需要剪切视频时长的两个端点,不需要time_1在time_2之前
concatente_videoclips(vfc_list, method='compose')
vfc_list为VideoFileClip的对象组成的list。注意:method=‘compose’是必要的,它使得各种编码方式不同的视频也可以进行拼接,否则,如果输入编码方式不同的视频会报错。
vfc.crop(x_center=x_center, y_center=y_center, width=width, height=height)
vfc.resize(newsize=(width, height))
clip = ImageSequenceClip(pic_list, fps=fps)
clip_1.set_position([0, 0])
clip_2.set_position([clip_1.w, 0])
CompositeVideoClip([clip_1, clip_2], size=(clip_1.w+clip_2.w, clip_1.h))
注意:.set_position([x1, y1])中的x1,y1为视频左上角的坐标
clip.write_videofile(path, codec=‘mpeg4’, verbose=False, audio=False)
其中,verbose为打印详细信息与否,audio为是否写入声音。
使用pip install OpenCV-Python
安装。
本地图片,使用img = cv2.imread(path)
网络图片,使用
# 这种方式读取进来通道和skimage一致,RGB格式
import imageio
img = imageio.imread(url)
或者
# 这种方式读取进来通道和opencv一致,BGR格式
import imutils
img = imutils.url_to_image(url)
多线程读取图片,使用
from multiprocessing.dummy import Pool as ThreadPool
from tqdm import tqdm
def getf(url):
...
results = []
with ThreadPool(100) as p:
results = list(tqdm(p.imap(getf, urllist), total=num))
cap = cv2.VideoCapture(path)
while (cap.isOpened()):
ret, frame = cap.read()
import numpy as np import cv2,sys font = cv2.FONT_HERSHEY_SIMPLEX cap = cv2.VideoCapture(sys.argv[1]) total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) fc = 0 speed = 1 while (cap.isOpened() and fc < total): ret, frame = cap.read() fc += 1 if fc % speed ==0: # 第二、三个参数是矩形框角点位置 cv2.rectangle(frame,eval(sys.argv[2]),eval(sys.argv[3]),(255,0,0),2) cv2.putText(frame,'%d|%d|%d'%(speed,total,fc),(50,50),font,1.2,(255,0,0),2) cv2.imshow('frame', frame) key = cv2.waitKey(4) & 0xFF if key == ord('q'): # 按q退出 break if key == ord("w"): #按w键增加跳帧 speed+=1 if key == ord("s"): #按s键减少跳帧 if speed > 1: speed-=1 cap.release() cv2.destroyAllWindows()
import cv2
fps = 8 #保存视频的FPS,可以适当调整
size=(1920,1080)
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
videoWriter = cv2.VideoWriter('3.avi',fourcc,fps,size)
for i in range(1,100):
frame = cv2.imread('3-out/'+str(i)+'.jpg')
videoWriter.write(frame)
videoWriter.release()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。