python视频分类保存_python如何通过不同的名称保存视频?

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()

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值