我們在工作或?qū)W習(xí)時,偶爾需要將視頻分解成圖片,只取其中一段的圖片就行了,而在網(wǎng)上下載轉(zhuǎn)換工具又3比較麻煩,現(xiàn)在我們用python實(shí)現(xiàn)視頻分解成圖片的功能。
"""
-------------------------------------------------
File Name: 01-video2jpg.py
Description :
Author : 小戀莫小哀
Email: xiaowen0392@qq.com
date: 2019/5/26
-------------------------------------------------
Change Activity:
2019/5/26:
-------------------------------------------------
"""
import cv2
mp4 = cv2.VideoCapture("1.mp4") # 讀取視頻
is_opened = mp4.isOpened() # 判斷是否打開
print(is_opened)
fps = mp4.get(cv2.CAP_PROP_FPS) # 獲取視頻的幀率
print(fps)
widght = mp4.get(cv2.CAP_PROP_FRAME_WIDTH) # 獲取視頻的寬度
height = mp4.get(cv2.CAP_PROP_FRAME_HEIGHT) # 獲取視頻的高度
print(str(widght) + "x" + str(height))
i = 0
while is_opened:
if i == 10: # 截取前10張圖片
break
else:
i += 1
(flag, frame) = mp4.read() # 讀取圖片
file_name = "iamge" + str(i) + ".jpg"
print(file_name)
if flag == True:
cv2.imwrite(file_name, frame, [cv2.IMWRITE_JPEG_QUALITY]) # 保存圖片
print("轉(zhuǎn)換完成")