VideoFile.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from os.path import exists
  2. import cv2
  3. from pycs.projects.ImageFile import ImageFile
  4. from pycs.projects.MediaFile import MediaFile
  5. class VideoFile(MediaFile):
  6. def __init__(self, obj, project, type='data'):
  7. # add type to object
  8. obj['type'] = 'video'
  9. # call parent constructor
  10. super().__init__(obj, project, type)
  11. # generate thumbnail
  12. self.__thumbnail = self.__read_video()
  13. def __read_video(self):
  14. # create image file from properties
  15. copy = self.copy()
  16. copy['extension'] = '.jpg'
  17. resized = ImageFile(copy, self.project, 'temp')
  18. # open video file
  19. video = cv2.VideoCapture(self.path)
  20. # read fps value
  21. self['fps'] = video.get(cv2.CAP_PROP_FPS)
  22. self['frameCount'] = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
  23. # create thumbnail
  24. if not exists(resized.path):
  25. _, image = video.read()
  26. cv2.imwrite(resized.path, image)
  27. # close video file
  28. video.release()
  29. # return
  30. return resized
  31. def resize(self, maximum_width):
  32. return self.__thumbnail.resize(maximum_width)