VideoFile.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from os.path import splitext, join, 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_id):
  7. # add type to object
  8. obj['type'] = 'video'
  9. # call parent constructor
  10. super().__init__(obj, project_id)
  11. # generate thumbnail
  12. self.__thumbnail = self.__read_video()
  13. def __read_video(self):
  14. # determine some properties
  15. path, name = self.get_file()
  16. file_name, file_ext = splitext(name)
  17. video_path = join(path, name)
  18. image_path = join(path, f'{file_name}.jpg')
  19. # open video file
  20. video = cv2.VideoCapture(video_path)
  21. # read fps value
  22. self['fps'] = video.get(cv2.CAP_PROP_FPS)
  23. self['frameCount'] = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
  24. # create thumbnail
  25. if not exists(image_path):
  26. _, image = video.read()
  27. cv2.imwrite(image_path, image)
  28. # close video file
  29. video.release()
  30. # return ImageFile from thumbnail
  31. copy = self.copy()
  32. copy['extension'] = '.jpg'
  33. return ImageFile(copy, self.project_id)
  34. def resize(self, maximum_width):
  35. return self.__thumbnail.resize(maximum_width)