基于opencv 实现录像


import time
import cv2
import os
class VideoSave():
   def __init__(self) -> None:
       self.Number=0
       self.LastPath=""   
   def saveImg(self,img,i):
      """保存截图"""
      # 让图片按分钟保存
      timecc=time.localtime(time.time())
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year)+"/"+str(timecc.tm_mon)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon)+"/"+str(timecc.tm_mday)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon)+"/"+str(timecc.tm_mday)) 
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min)):
         if self.LastPath!="":
            self.MergVideo(self.LastPath,self.Number)
            self.Number=0
         self.LastPath="../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min)
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min))
      cv2.imwrite(self.LastPath+"/"+str(i)+".jpg",img)

   def Videos(self):
      """读取摄像头"""
      VideoObj=cv2.VideoCapture(0)
      while True:
         ret,img=VideoObj.read()
         self.saveImg(img,self.Number)
         self.Number+=1
   def MergVideo(self,ImgListPath,Number): 
      """将图片保存为短视频"""
      path=ImgListPath
      out=cv2.VideoWriter("../videos/videos/"+time.time()+".mp4",-1,20,(640,480))
      # for root,dirs,files in os.walk(path):
      #    for i in range(len(files)):
      #为了保证图片顺序一致
      for i in range(0,Number):
            #print(path+"/"+str(i)+".jpg")
            out.write(cv2.imread(path+"/"+str(i)+".jpg") )
      out.release()

if __name__ == '__main__':
   obj=VideoSave()
   obj.Videos()import time
import cv2
import os
class VideoSave():
   def __init__(self) -> None:
       self.Number=0
       self.LastPath=""   
   def saveImg(self,img,i):
      """保存截图"""
      timecc=time.localtime(time.time())
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year)+"/"+str(timecc.tm_mon)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon)+"/"+str(timecc.tm_mday)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon)+"/"+str(timecc.tm_mday)) 
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)):
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour))
      if not os.path.exists("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min)):
         if self.LastPath!="":
            self.MergVideo(self.LastPath,self.Number)
            self.Number=0
         self.LastPath="../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min)
         os.mkdir("../videos/imgCachs/"+str(timecc.tm_year) +"/"+str(timecc.tm_mon) +"/"+str(timecc.tm_mday)+"/"+str(timecc.tm_hour)+"/"+str(timecc.tm_min))
      cv2.imwrite(self.LastPath+"/"+str(i)+".jpg",img)

   def Videos(self):
      """读取摄像头"""
      VideoObj=cv2.VideoCapture(0)
      while True:
         ret,img=VideoObj.read()
         self.saveImg(img,self.Number)
         self.Number+=1
   def MergVideo(self,ImgListPath,Number): 
      """将图片保存为短视频"""
      path=ImgListPath
      out=cv2.VideoWriter("../videos/videos/"+time.time()+".mp4",-1,20,(640,480))
      # for root,dirs,files in os.walk(path):
      #    for i in range(len(files)):
      for i in range(0,Number):
            print(path+"/"+str(i)+".jpg")
            out.write(cv2.imread(path+"/"+str(i)+".jpg") )
      out.release()

if __name__ == '__main__':
   obj=VideoSave()
   obj.Videos()
``
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值