本文介绍了python如何通过不同的名称保存视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是记录流并将该流保存到文件夹中。问题是,我必须每隔5秒将流保存到不同的文件夹中。我的意思是说,对于30秒长的流,应该有6个文件夹。我的代码可以正常工作,但是我无法正确测量秒数,因此将帧(a)分为fps。但是它没有给出正确的结果。另外,我无法使用不同的名称将视频保存到其他文件夹中。我必须使用不同的名称,但是我不知道该怎么做。

My aim is recording stream and saving that stream into folders. The problem is, I have to save every 5 seconds long of stream into different folders. I mean for a 30 seconds long stream, there should be 6 folders. My code is working but I can't measure the seconds correctly, I divided the frames (a) into fps. But it did not give the correct result. Also I cannot save videos into different folders by using different names. I have to give different names but I don't know how to do it.

import numpy as np
import cv2, time
import os

cap = cv2.VideoCapture(0)
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

out = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))
a=0
n=0
while(cap.isOpened()):
    a=a+1
    fps = cap.get(cv2.CAP_PROP_FPS)
    sec = a / fps
    ret, frame = cap.read()
    n=n+1

    if ret==True:
        if sec%5==0:
            out = cv2.VideoWriter('output.avi2', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10,
                                  (frame_width, frame_height))
        else:
            out.write(frame)

        cv2.imshow('frame',frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    else:
        break

print(a)
print('fps= '+str(fps))
print('second= '+str(sec))
cap.release()
out.release()
cv2.destroyAllWindows()


推荐答案

由于脚本无法正确测量秒数由于python是一种相对较慢的编程语言,因此执行需要花费时间,因此,如果您要处理库,则执行代码所需的时间足以引起严重的延迟。尝试导入 datetime 模块并用其测量时间

You can't measure seconds correctly because your script takes time to execute and since python is a relatively slow programming language, the time needed to execute your code is enough to cause a significant delay if you are dealing with libraries. Try importing datetime module and measuring time with it

import datetime


time_to_wait = datetime.timedelta(seconds=5)

while(cap.isOpened()):
    last = datetime.datetime.now()
    # do your stuff
    if ret==True:
       if datetime.datetime.now() - last >= time_to_wait:
           last = datetime.datetime.now()
           # do your stuff

关于您的命名问题,我没有确定的解决方案,但是您可以尝试使用类和列表,但我不确定

regarding your naming issue I have no sure solution, but you could try using classes and lists, but I'm not sure

这篇关于python如何通过不同的名称保存视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:41
查看更多