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

技术共进,成长同行——讯飞AI开发者社区

更多推荐