1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- from os.path import splitext, join, exists
- import cv2
- from pycs.projects.ImageFile import ImageFile
- from pycs.projects.MediaFile import MediaFile
- class VideoFile(MediaFile):
- def __init__(self, obj, project_id):
- # add type to object
- obj['type'] = 'video'
- # call parent constructor
- super().__init__(obj, project_id)
- # generate thumbnail
- self.__thumbnail = self.__read_video()
- def __read_video(self):
- # determine some properties
- path, name = self.get_file()
- file_name, file_ext = splitext(name)
- video_path = join(path, name)
- image_path = join(path, f'{file_name}.jpg')
- # open video file
- video = cv2.VideoCapture(video_path)
- # read fps value
- self['fps'] = video.get(cv2.CAP_PROP_FPS)
- self['frameCount'] = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
- # create thumbnail
- if not exists(image_path):
- _, image = video.read()
- cv2.imwrite(image_path, image)
- # close video file
- video.release()
- # return ImageFile from thumbnail
- copy = self.copy()
- copy['extension'] = '.jpg'
- return ImageFile(copy, self.project_id)
- def resize(self, maximum_width):
- return self.__thumbnail.resize(maximum_width)
|