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

Python實現(xiàn)視頻分幀的方法分享

 更新時間:2023年03月24日 10:35:22   作者:像風(fēng)一樣的男人@  
這篇文章主要為大家詳細(xì)介紹了如何通過Python語言實現(xiàn)視頻分幀的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起嘗試一下

下載依賴

pip install opencv-python==4.0.0.21

實現(xiàn)

方法一

def video_to_frames(video_path, outPutDirName):
    """
    抽取視頻幀存為圖片
    :param video_path:
    :param outPutDirName:
    :return:
    """
    times = 0

    # 提取視頻的頻率,每1幀提取一個
    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è)置間隔存儲視頻幀
        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)
    # 對提取到的圖片名稱進(jìn)行排序
    image_names.sort(key=lambda n: int(n[:-4]))
    # 設(shè)置寫入格式
    fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
    # 設(shè)置每秒幀數(shù)
    fps = fps
    # 讀取第一個圖片獲取大小尺寸,因為需要轉(zhuǎn)換成視頻的圖片大小尺寸是一樣的
    image = Image.open(os.path.join(image_path, image_names[0]))
    # 初始化媒體寫入對象
    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)
    # 釋放媒體寫入對象
    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)  # 獲取到一個視頻
    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還可以將幀合成視頻流,下面是實現(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實現(xiàn)視頻分幀的方法分享的文章就介紹到這了,更多相關(guān)Python視頻分幀內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python 繪圖模塊matplotlib的使用簡介

    python 繪圖模塊matplotlib的使用簡介

    這篇文章主要介紹了python 繪圖模塊matplotlib的使用簡介,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • python用opencv將標(biāo)注提取畫框到對應(yīng)的圖像中

    python用opencv將標(biāo)注提取畫框到對應(yīng)的圖像中

    這篇文章主要介紹了python用opencv將標(biāo)注提取畫框到對應(yīng)的圖像中,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • 淺談python字典多鍵值及重復(fù)鍵值的使用

    淺談python字典多鍵值及重復(fù)鍵值的使用

    下面小編就為大家?guī)硪黄獪\談python字典多鍵值及重復(fù)鍵值的使用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • Flask之pipenv虛擬環(huán)境的實現(xiàn)

    Flask之pipenv虛擬環(huán)境的實現(xiàn)

    這篇文章主要介紹了Flask之pipenv虛擬環(huán)境的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Python中XlsxWriter模塊簡介與用法分析

    Python中XlsxWriter模塊簡介與用法分析

    這篇文章主要介紹了Python中XlsxWriter模塊用法,簡單描述了XlsxWriter模塊的功能并結(jié)合實例形式分析了Python使用XlsxWriter模塊操作xls文件的數(shù)據(jù)插入、直方圖等相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • pandas讀取csv文件,分隔符參數(shù)sep的實例

    pandas讀取csv文件,分隔符參數(shù)sep的實例

    今天小編就為大家分享一篇pandas讀取csv文件,分隔符參數(shù)sep的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python批量處理txt文件的實例代碼

    python批量處理txt文件的實例代碼

    這篇文章主要介紹了python批量處理txt文件的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • Python 實用技巧之利用Shell通配符做字符串匹配

    Python 實用技巧之利用Shell通配符做字符串匹配

    這篇文章主要介紹了Python 實用技巧之利用Shell通配符做字符串匹配的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • Pandas 連接合并函數(shù)merge()詳解

    Pandas 連接合并函數(shù)merge()詳解

    這篇文章主要介紹了Pandas 連接合并函數(shù)merge()詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • python中extend功能用法舉例

    python中extend功能用法舉例

    這篇文章主要給大家介紹了關(guān)于python中extend功能的相關(guān)資料,Python中的extend()方法是一種非常有用的列表操作,它可以將一個列表中的元素添加到另一個列表的末尾,需要的朋友可以參考下
    2023-08-08

最新評論