Python 如何在视频剪辑中保存以前的帧?

Python 如何在视频剪辑中保存以前的帧?,python,opencv,Python,Opencv,如何使用OpenCV python在实时视频剪辑中保存上一个视频帧这是您感兴趣的示例代码。希望这能对你有所帮助 import cv2 import os import random import string #Generate a random string of numbers to use as a part of our output file names def rand_string(length): rand_str = ''.join(random.choice(

如何使用OpenCV python在实时视频剪辑中保存上一个视频帧这是您感兴趣的示例代码。希望这能对你有所帮助

import cv2
import os
import random
import string

#Generate a random string of numbers to use as a part of our output file names
def rand_string(length):
    rand_str = ''.join(random.choice(
        string.ascii_lowercase
        + string.ascii_uppercase
        +string.digits)
        for i in range(length))
    return rand_str

#calculating the length of the video / frame count
def length_of_video(video_path):
    cap = cv2.VideoCapture(video_path)
    length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))    #frame count
    #print(length)
    return length

#extraction of video to images
def extracting_frames(video_path, save_path, till = 50):
    print('Extracting frames')
        #param 1: path of the source (video)
        #param 2: where the output images saved
        #param 3: when the extraction should stop 

    #split the path to directory and the file name itself
    _, file_name = os.path.split(video_path)
    #getting the file_name without the extention
    file_name = os.path.splitext(file_name)[0]

    length = length_of_video(video_path)
    if length == 0:
        print('Video length is zero')
        return 0

    cap = cv2.VideoCapture(video_path)
    count = 0           #counting frames
    random_string = rand_string(5)              #for naming each frame

    #check if it is working by checking the very first frame of the video
    ret, frame = cap.read()
    test_file_path = os.path.join(
        save_path,
        file_name[:6] +\
        '{}_{}.jpg'.format(random_string, count))

    cv2.imwrite(test_file_path, frame)
    if os.path.isfile(test_file_path):
        print('Test frame saved,' + 'Continuing Extraction')

        #continuing the extraction 
        count = 1
        while ret:
            ret, frame = cap.read()
            #if frame is available and count is less than till, continue extraction
            if ret and count < till:
                file_path = os.path.join(
                    save_path,
                    file_name[:6]+
                    '{}_{}.jpg'.format(random_string, count))

                cv2.imwrite(file_path, frame)
                count +=1
                print(count)
            else:
                count +=1
    else:
        print('Problem with saving the file!')
        return 0
    cap.release()
    print('Extraction completed!')

#main function
if __name__ == '__main__':
    video_frame = ["../data/vtest.avi"]
    save_path = "../data/vtest_frames"
    for video in video_frame:
        print(video)
        extracting_frames(video, save_path, till=30)

    #length_of_video(video_frame)
导入cv2
导入操作系统
随机输入
导入字符串
#生成一个随机数字字符串,用作输出文件名的一部分
def rand_字符串(长度):
rand_str=''.join(random.choice(
string.ascii_小写
+string.ascii_大写
+字符串(数字)
对于范围(长度)内的i)
返回兰杜街
#计算视频/帧计数的长度
视频的定义长度(视频路径):
cap=cv2.视频捕获(视频路径)
长度=int(cap.get(cv2.cap_PROP_FRAME_COUNT))#帧计数
#打印(长度)
返回长度
#视频图像的提取
def提取帧(视频路径,保存路径,直到=50):
打印('正在提取帧')
#参数1:源的路径(视频)
#参数2:保存输出图像的位置
#参数3:提取应在何时停止
#拆分目录路径和文件名本身
_,file_name=os.path.split(视频路径)
#获取不带扩展名的文件名
file\u name=os.path.splitext(文件名)[0]
长度=视频的长度(视频路径)
如果长度==0:
打印('视频长度为零')
返回0
cap=cv2.视频捕获(视频路径)
计数=0#计数帧
随机字符串=随机字符串(5)#用于命名每个帧
#通过检查视频的第一帧来检查它是否正常工作
ret,frame=cap.read()
测试文件路径=os.path.join(
保存路径,
文件名[:6]+\
“{}{}.jpg.”格式(随机字符串,计数))
cv2.imwrite(测试文件路径,帧)
如果os.path.isfile(测试文件路径):
打印('已保存测试帧'+'继续提取')
#继续提取
计数=1
而ret:
ret,frame=cap.read()
#如果帧可用且计数小于till,则继续提取
如果ret和count<直到:
文件路径=os.path.join(
保存路径,
文件名[:6]+
“{}{}.jpg.”格式(随机字符串,计数))
cv2.imwrite(文件路径,帧)
计数+=1
打印(计数)
其他:
计数+=1
其他:
打印('保存文件时出现问题!')
返回0
第1章释放()
打印('提取完成!')
#主要功能
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
视频帧=[”。/data/vtest.avi“]
保存路径=“../data/vtest\u帧”
对于视频帧中的视频:
打印(视频)
提取帧(视频,保存路径,直到=30)
#视频的长度(视频帧)

记住RingBuffer中的一些帧这段代码对我来说非常好。请确保您已相应地更改了主功能中的“视频帧”和“保存路径”。