Python實現(xiàn)視頻分解成圖片+圖片合成視頻
一、python視頻拆分+圖片合成(源碼一)

1.python視頻拆分
import cv2
def video2frame(videos_path,frames_save_path,time_interval):
'''
:param videos_path: 視頻的存放路徑
:param frames_save_path: 視頻切分成幀之后圖片的保存路徑
:param time_interval: 保存間隔
:return:
'''
vidcap = cv2.VideoCapture(videos_path)
success, image = vidcap.read()
count = 0
while success:
success, image = vidcap.read()
count += 1
if count % time_interval == 0:
cv2.imencode('.jpg', image)[1].tofile(frames_save_path + "/frame%d.jpg" % count)
# if count == 20:
# break
print(count)
if __name__ == '__main__':
videos_path = r'E:\py\python3.7\test\test98youhuashiping\shipingchaifen\1.mp4'
frames_save_path = r'E:\py\python3.7\test\test98youhuashiping\shipingchaifen'
time_interval = 2#隔一幀保存一次
video2frame(videos_path, frames_save_path, time_interval)
2.python圖片合成
import cv2
import os
import numpy as np
from PIL import Image
def frame2video(im_dir,video_dir,fps):
im_list = os.listdir(im_dir)
im_list.sort(key=lambda x: int(x.replace("frame","").split('.')[0])) #最好再看看圖片順序?qū)Σ?
img = Image.open(os.path.join(im_dir,im_list[0]))
img_size = img.size #獲得圖片分辨率,im_dir文件夾下的圖片分辨率需要一致
# fourcc = cv2.cv.CV_FOURCC('M','J','P','G') #opencv版本是2
fourcc = cv2.VideoWriter_fourcc(*'XVID') #opencv版本是3
videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)
# count = 1
for i in im_list:
im_name = os.path.join(im_dir+i)
frame = cv2.imdecode(np.fromfile(im_name, dtype=np.uint8), -1)
videoWriter.write(frame)
# count+=1
# if (count == 200):
# print(im_name)
# break
videoWriter.release()
print('finish')
if __name__ == '__main__':
im_dir = r'E:\py\python3.7\test\test98youhuashiping\shipingchaifen\pho/'#幀存放路徑
video_dir = r'E:\py\python3.7\test\test98youhuashiping\shipingchaifen/test.mp4' #合成視頻存放的路徑
fps = 30 #幀率,每秒鐘幀數(shù)越多,所顯示的動作就會越流暢
frame2video(im_dir, video_dir, fps)
提示:路徑中不要出現(xiàn)中文和特殊字符,且書寫要規(guī)范??!
二、python視頻拆分+圖片合成(源碼二)

import cv2
import numpy as np
import os
os.chdir(r'E:\py\python3.7\test\test98youhuashiping\chaifen')
##讀取視頻,并逐幀分解成圖片
cap = cv2.VideoCapture('1.mp4') #打開一個視頻
isOpened = cap.isOpened() #判斷是否打開
print(isOpened)
#獲取視頻的相關(guān)信息,視頻的每一幀圖片的寬度都是一致的
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)) #獲取高度
print(fps,width,height) #輸出相關(guān)信息
i = 0
while (isOpened):
#讀取視頻的前兩秒的圖像,共計2*int(fps)張
if i ==int(fps)*2 :
break
else:
i = i+1
(flag,frame) = cap.read() #讀取每一張 flag frame
filename = 'image'+str(i)+'.jpg'
#將讀取的圖片寫入文件中,
if flag == True:
cv2.imwrite(filename,frame,[cv2.IMWRITE_JPEG_QUALITY,100]) #確定圖片質(zhì)量,100算是高的
print('end!')
##讀取零散圖片(上面分解的圖片),并將其合成視頻
img = cv2.imread('image1.jpg')
imginfo = img.shape
size = (imginfo[1],imginfo[0]) #與默認不同,opencv使用 height在前,width在后,所有需要自己重新排序
print(size)
#創(chuàng)建寫入對象,包括 新建視頻名稱,每秒鐘多少幀圖片(10張) ,size大小
#一般人眼最低分辨率為19幀/秒
videoWrite = cv2.VideoWriter('2.mp4',-1,10,size)
for i in range(1,40):
filename = 'image'+str(i)+'.jpg'
img = cv2.imread(filename,1) #1 表示彩圖,0表示灰度圖
#直接寫入圖片對應的數(shù)據(jù)
videoWrite.write(img)
videoWrite.release() #關(guān)閉寫入對象
print('end')
三、python視頻拆分(源碼三)

import cv2 #導入opencv模塊
import os
import time
def video_split(video_path,save_path):
'''
對視頻文件切割成幀
'''
'''
@param video_path:視頻路徑
@param save_path:保存切分后幀的路徑
'''
vc=cv2.VideoCapture(video_path)
#一幀一幀的分割 需要幾幀寫幾
c=0
if vc.isOpened():
rval,frame=vc.read()
else:
rval=False
while rval:
rval,frame=vc.read()
# 每秒提取2幀圖片
if c % 2 == 0:
cv2.imwrite(save_path + "/" + str('%06d'%c)+'.jpg',frame)
cv2.waitKey(1)
c=c+1
DATA_DIR = r"E:\py\python3.7\test\test98youhuashiping\ceshi\mp4" #視頻數(shù)據(jù)主目錄
SAVE_DIR = r"E:\py\python3.7\test\test98youhuashiping\ceshi\pho2" #幀文件保存目錄
start_time = time.time()
for parents,dirs,filenames in os.walk(DATA_DIR):
#if parents == DATA_DIR:
# continue
print("正在處理文件夾",parents)
path = parents.replace("\\","http://")
f = parents.split("\\")[1]
save_path = SAVE_DIR + "http://" + f
# 對每視頻數(shù)據(jù)進行遍歷
for file in filenames:
file_name = file.split(".")[0]
save_path_ = save_path + "/" + file_name
if not os.path.isdir(save_path_):
os.makedirs(save_path_)
video_path = path + "/" + file
video_split(video_path,save_path_)
end_time = time.time()
print("Cost time",start_time - end_time)
到此這篇關(guān)于Python實現(xiàn)視頻分解成圖片+圖片合成視頻的文章就介紹到這了,更多相關(guān)Python 視頻分解與合成內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python cs架構(gòu)實現(xiàn)簡單文件傳輸
這篇文章主要為大家詳細介紹了python cs架構(gòu)實現(xiàn)簡單文件傳輸,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Python OpenCV Hough直線檢測算法的原理實現(xiàn)
這篇文章主要介紹了Python OpenCV Hough直線檢測算法的原理實現(xiàn),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
淺談Python3中datetime不同時區(qū)轉(zhuǎn)換介紹與踩坑
最近的項目需要根據(jù)用戶所屬時區(qū)制定一些特定策略,學習、應用了若干python3的時區(qū)轉(zhuǎn)換相關(guān)知識,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Python?Traceback(most?recent?call?last)報錯信息:示例解讀
這篇文章主要介紹了Python?Traceback(most?recent?call?last)報錯信息:示例解讀,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
詳解python3中用HTMLTestRunner.py報ImportError: No module named ''
這篇文章主要介紹了詳解python3中用HTMLTestRunner.py報ImportError: No module named 'StringIO'如何解決,感興趣的可以了解一下2019-08-08
Python實現(xiàn)信息轟炸工具(再也不怕說不過別人了)
不知道各位小伙伴有沒有遇到過這樣的一個故事,發(fā)現(xiàn)自己直接噴不過,打字速度不夠給力.下面這篇文章就能解決自己噴不過的苦惱,話不多說,上才藝,需要的朋友可以參考下2021-06-06
win10下tensorflow和matplotlib安裝教程
這篇文章主要為大家詳細介紹了win10下tensorflow和matplotlib安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09

