ビデオを画像に変換する 画像をビデオに変換する
import cv2
from cv2 import VideoWriter, VideoWriter_fourcc, imread, resize
import os
from PIL import Image
def Pic2Video():
imgPath = "./video/img/"
videoPath = "./video/outvideo/out6.mp4"
images = os.listdir(imgPath)
images.sort()
fps = 5
fourcc = VideoWriter_fourcc(*'mp4v')
h, w, _ = cv2.imread(os.path.join(imgPath, images[1])).shape
videoWriter = cv2.VideoWriter(videoPath, fourcc, fps, (w, h))
for im_name in range(len(images)):
frame = cv2.imread(imgPath + images[im_name])
print(im_name)
videoWriter.write(frame)
print("图片转视频结束!")
videoWriter.release()
cv2.destroyAllWindows()
def Video2Pic():
videoPath = "./video/input/12s.mp4"
imgPath = "./video/outimg2/"
cap = cv2.VideoCapture(videoPath)
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))
suc = cap.isOpened()
frame_count = 0
while suc:
frame_count += 1
suc, frame = cap.read()
cv2.imwrite(imgPath + "%d.png" % frame_count, frame)
cv2.waitKey(1)
cap.release()
print("视频转图片结束!")
if __name__ == "__main__":
Pic2Video()