Python實(shí)現(xiàn)視頻分幀的方法分享
下載依賴
pip install opencv-python==4.0.0.21
實(shí)現(xiàn)
方法一
def video_to_frames(video_path, outPutDirName):
"""
抽取視頻幀存為圖片
:param video_path:
:param outPutDirName:
:return:
"""
times = 0
# 提取視頻的頻率,每1幀提取一個(gè)
frame_frequency = 1
# 如果文件目錄不存在則創(chuàng)建目錄
if not os.path.exists(outPutDirName):
os.makedirs(outPutDirName)
# 讀取視頻幀
camera = cv2.VideoCapture(video_path)
while True:
times = times + 1
res, image = camera.read()
if not res:
print('not res , not image')
break
# 按照設(shè)置間隔存儲(chǔ)視頻幀
if times % frame_frequency == 0:
create_path = os.path.join(outPutDirName, f"{str(times)}.jpg")
cv2.imwrite(create_path, image)
logger.info('圖片提取結(jié)束')
# 釋放攝像頭設(shè)備
camera.release()
def image_to_video(image_path, media_path, fps):
'''
圖片合成視頻函數(shù)
:param image_path: 圖片路徑
:param media_path: 合成視頻保存路徑
:return:
'''
# 獲取圖片路徑下面的所有圖片名稱
image_names = os.listdir(image_path)
# 對(duì)提取到的圖片名稱進(jìn)行排序
image_names.sort(key=lambda n: int(n[:-4]))
# 設(shè)置寫入格式
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
# 設(shè)置每秒幀數(shù)
fps = fps
# 讀取第一個(gè)圖片獲取大小尺寸,因?yàn)樾枰D(zhuǎn)換成視頻的圖片大小尺寸是一樣的
image = Image.open(os.path.join(image_path, image_names[0]))
# 初始化媒體寫入對(duì)象
media_writer = cv2.VideoWriter(media_path, fourcc, fps, image.size)
# 遍歷圖片,將每張圖片加入視頻當(dāng)中
for image_name in image_names:
im = cv2.imread(os.path.join(image_path, image_name))
media_writer.write(im)
# 釋放媒體寫入對(duì)象
media_writer.release()
logger.info('無聲視頻寫入完成!')
方法二
import numpy as np
import cv2
import os
import sys
def cut(video_file, target_dir):
cap = cv2.VideoCapture(video_file) # 獲取到一個(gè)視頻
isOpened = cap.isOpened # 判斷是否打開
# 為單張視頻,以視頻名稱所謂文件名,創(chuàng)建文件夾
temp = os.path.split(video_file)[-1]
dir_name = temp.split('.')[0]
single_pic_store_dir = os.path.join(target_dir, dir_name)
if not os.path.exists(single_pic_store_dir):
os.mkdir(single_pic_store_dir)
i = 0
while isOpened:
i += 1
(flag, frame) = cap.read() # 讀取一張圖像
fileName = 'image' + str(i) + ".jpg"
if (flag == True):
# 以下三行 進(jìn)行 旋轉(zhuǎn)
#frame = np.rot90(frame, -1)
#print(fileName)
# 設(shè)置保存路徑
save_path = os.path.join(single_pic_store_dir, fileName)
#print(save_path)
res = cv2.imwrite(save_path, frame, [cv2.IMWRITE_JPEG_QUALITY, 70])
#print(res)
else:
break
return single_pic_store_dir
if __name__ == '__main__':
video_file = 'I:/crack.avi'
cut(video_file, 'I:/data/')
方法三
#!/usr/bin/env python
import cv2
numer = 18
cap=cv2.VideoCapture("/home/linux/work/python/video/"+str(numer)+".mp4")
if cap.isOpened():
ret,frame=cap.read()
else:
ret = False
n=0
i=0
timeF = 40
path='/home/linux/work/python/video/'+str(numer)+'/{}'
while ret:
n = n + 1
ret,frame=cap.read()
if (n%timeF == 0) :
i = i+1
print(i)
filename=str(numer)+"_"+str(i)+".jpg"
cv2.imwrite(path.format(filename),frame)
cv2.waitKey(1)
cap.release()
補(bǔ)充
除了視頻分幀,Python還可以將幀合成視頻流,下面是實(shí)現(xiàn)代碼
#!/usr/bin/env python
import cv2
img = cv2.imread('/home/linux/work/python/img/1_475.jpg')
imginfo = img.shape
size = (imginfo[1],imginfo[0])
print(size)
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
videoWrite = cv2.VideoWriter('/home/linux/work/python/2.mp4',fourcc,10,size)
for i in range(475,2208):
filename = '/home/linux/work/python/img/1_'+str(i)+'.jpg'
img = cv2.imread(filename,1)
videoWrite.write(img)
print(i)
videoWrite.release()
print('end')
到此這篇關(guān)于Python實(shí)現(xiàn)視頻分幀的方法分享的文章就介紹到這了,更多相關(guān)Python視頻分幀內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 繪圖模塊matplotlib的使用簡(jiǎn)介
這篇文章主要介紹了python 繪圖模塊matplotlib的使用簡(jiǎn)介,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03
python用opencv將標(biāo)注提取畫框到對(duì)應(yīng)的圖像中
這篇文章主要介紹了python用opencv將標(biāo)注提取畫框到對(duì)應(yīng)的圖像中,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Flask之pipenv虛擬環(huán)境的實(shí)現(xiàn)
這篇文章主要介紹了Flask之pipenv虛擬環(huán)境的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Python中XlsxWriter模塊簡(jiǎn)介與用法分析
這篇文章主要介紹了Python中XlsxWriter模塊用法,簡(jiǎn)單描述了XlsxWriter模塊的功能并結(jié)合實(shí)例形式分析了Python使用XlsxWriter模塊操作xls文件的數(shù)據(jù)插入、直方圖等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
pandas讀取csv文件,分隔符參數(shù)sep的實(shí)例
今天小編就為大家分享一篇pandas讀取csv文件,分隔符參數(shù)sep的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python 實(shí)用技巧之利用Shell通配符做字符串匹配
這篇文章主要介紹了Python 實(shí)用技巧之利用Shell通配符做字符串匹配的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08

