欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)視頻分解成圖片+圖片合成視頻

 更新時(shí)間:2022年04月02日 08:40:45   作者:用余生去守護(hù)  
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)視頻分解成圖片以及將圖片合成為視頻,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

一、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ù)越多,所顯示的動(dòng)作就會(huì)越流暢
    frame2video(im_dir, video_dir, fps)

提示:路徑中不要出現(xiàn)中文和特殊字符,且書(shū)寫(xiě)要規(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')  #打開(kāi)一個(gè)視頻
isOpened = cap.isOpened() #判斷是否打開(kāi)
print(isOpened)

#獲取視頻的相關(guān)信息,視頻的每一幀圖片的寬度都是一致的
fps = cap.get(cv2.CAP_PROP_FPS) #幀率,即每秒鐘由多少?gòu)垐D片組成
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):
    #讀取視頻的前兩秒的圖像,共計(jì)2*int(fps)張
    if i ==int(fps)*2 :  
        break
    else:
        i = i+1
    (flag,frame) = cap.read() #讀取每一張 flag frame
    filename = 'image'+str(i)+'.jpg'
    #將讀取的圖片寫(xiě)入文件中,
    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])  #與默認(rèn)不同,opencv使用 height在前,width在后,所有需要自己重新排序
print(size)

#創(chuàng)建寫(xiě)入對(duì)象,包括 新建視頻名稱,每秒鐘多少幀圖片(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表示灰度圖  
    
    #直接寫(xiě)入圖片對(duì)應(yīng)的數(shù)據(jù)
    videoWrite.write(img)  

videoWrite.release() #關(guān)閉寫(xiě)入對(duì)象
print('end')
     

三、python視頻拆分(源碼三)

import cv2 #導(dǎo)入opencv模塊
import os
import time
 
def video_split(video_path,save_path):
    '''
    對(duì)視頻文件切割成幀
    '''
    '''
    @param video_path:視頻路徑
    @param save_path:保存切分后幀的路徑
    '''
    vc=cv2.VideoCapture(video_path)
    #一幀一幀的分割 需要幾幀寫(xiě)幾
    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
    # 對(duì)每視頻數(shù)據(jù)進(jìn)行遍歷
    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實(shí)現(xiàn)視頻分解成圖片+圖片合成視頻的文章就介紹到這了,更多相關(guān)Python 視頻分解與合成內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論