def test_frames_getting(): from ffvideo import VideoStream vs = VideoStream(v0) f1 = vs.current() # first frame f2 = vs.next() assert f2.timestamp > f1.timestamp f = vs[0] # first frame assert f.frameno == 0 f = vs.get_frame_no(100) # f = vs[100] # assert f.frameno == 100 f = vs.get_frame_no(100) f = vs.get_frame_at_sec(1) assert f.timestamp - 1 < 0.1 f = vs.get_frame_at_pts(133000) assert f.width == vs.frame_width assert f.height == vs.frame_height assert f.mode == vs.frame_mode
class Movie(QtCore.QObject): """This class represent the video data stream""" frameChanged = QtCore.pyqtSignal() endOfVideo = QtCore.pyqtSignal() def __init__(self, fileName=None): """Initialize the video stream and open the video file if a fileName is provided. :param filename: a string containing the name of the file to be opened. """ self.rawBuffer=None self.source=None super(Movie, self).__init__() if(fileName is not None): self.setMovie(fileName) self.timer = None self.frame = None self.isPlaying = False self.frameRate = 0 self.frameNumber = 0 def reset(self): """Reset the movie object. Remove the source.""" self.rawBuffer=None self.source=None self.timer = None self.frame = None self.isPlaying = False self.frameRate = 0 self.frameNumber = 0 def setMovie(self, fileName): """Open a video file. :param filename: a string containing the name of the file to be opened. :raise: TypeError: The fileName is not a string. """ if(isinstance(fileName, basestring)): self.fileName = u''.join(fileName).encode('utf-8') self.source = VideoStream(self.fileName) else: raise TypeError('fileName must be a string') self.frameRate = self.source.framerate self.frameNumber = self.source.duration*1000/self.source.framerate self.timer = QtCore.QTimer() self.timer.setInterval(1000.0/self.frameRate) self.timer.setSingleShot(False) self.timer.timeout.connect(self.frameMustChange) def resetMovie(self): """Reset the video stream.""" self.source = VideoStream(self.fileName) def play(self): """Start to read the video stream.""" self.isPlaying = True self.timer.start() def pause(self): """Pause the reading of the video stream.""" self.isPlaying = False self.timer.stop() def frameMustChange(self): """Slot called when it is time to load the next frame. :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed. """ self.readNextFrame() def toggleState(self): """Toggle between playing and pausing the video.""" if(self.isPlaying==True): self.pause() else: self.play() def jumpToFrame(self, position): """Modify the position in the video. :param position: a value between 0 and 1 corresponding to the position in the video. 0 is the beginning and 1 is the end. """ if(position>1.0): position = 1.0 elif(position<0.001): position = 0.001 frame = self.source.get_frame_at_sec(position*self.source.duration).ndarray return frame def readNextFrame(self): """Load the next frame. :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed. """ try: self.rawBuffer = self.source.next().ndarray() except ffvideo.NoMoreData: self.isPlaying = False self.pause() self.rawBuffer = None self.endOfVideo.emit() self.frameChanged.emit() def readNCFrame(self, number): """Load the next frame. :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed. """ position = self.source.current().frameno try: self.rawBuffer = self.source.get_frame_no(position+number).ndarray() except ffvideo.NoMoreData: self.isPlaying = False self.pause() self.endOfVideo.emit() if(self.source.current().frameno>=self.source.duration*self.source.framerate): self.isPlaying = False self.pause() self.endOfVideo.emit() self.frameChanged.emit() def currentPositionRatio(self): """Returns the position in the video. :return: a value between 0 and 1 representing the position in the video. 0 is the beginning and 1 is the end. """ if(self.source is not None and self.source.current() is not None): result = self.source.current().timestamp/self.source.duration if(result>1.0): return 1.0 elif(result<0.0): return 0.0 else: return result else: return 1.0 def getFrameNumber(self): """Returns the number of frame in the video. :return: An integer representing the number of frame in the video. """ return int(self.source.duration*self.source.framerate) def getFrameSize(self): return (self.source.frame_width, self.source.frame_height) def getEllapsedTime(self): """Returns the number ellapsed time in the video. :return: An integer representing the number of frame in the video. """ return self.source.current().timestamp
class Movie(QtCore.QObject): """This class represent the video data stream""" frameChanged = QtCore.pyqtSignal() endOfVideo = QtCore.pyqtSignal() def __init__(self, fileName=None): """Initialize the video stream and open the video file if a fileName is provided. :param filename: a string containing the name of the file to be opened. """ self.rawBuffer = None self.source = None super(Movie, self).__init__() if (fileName is not None): self.setMovie(fileName) self.timer = None self.frame = None self.isPlaying = False self.frameRate = 0 self.frameNumber = 0 def reset(self): """Reset the movie object. Remove the source.""" self.rawBuffer = None self.source = None self.timer = None self.frame = None self.isPlaying = False self.frameRate = 0 self.frameNumber = 0 def setMovie(self, fileName): """Open a video file. :param filename: a string containing the name of the file to be opened. :raise: TypeError: The fileName is not a string. """ if (isinstance(fileName, basestring)): self.fileName = u''.join(fileName).encode('utf-8') self.source = VideoStream(self.fileName) else: raise TypeError('fileName must be a string') self.frameRate = self.source.framerate self.frameNumber = self.source.duration * 1000 / self.source.framerate self.timer = QtCore.QTimer() self.timer.setInterval(1000.0 / self.frameRate) self.timer.setSingleShot(False) self.timer.timeout.connect(self.frameMustChange) def resetMovie(self): """Reset the video stream.""" self.source = VideoStream(self.fileName) def play(self): """Start to read the video stream.""" self.isPlaying = True self.timer.start() def pause(self): """Pause the reading of the video stream.""" self.isPlaying = False self.timer.stop() def frameMustChange(self): """Slot called when it is time to load the next frame. :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed. """ self.readNextFrame() def toggleState(self): """Toggle between playing and pausing the video.""" if (self.isPlaying == True): self.pause() else: self.play() def jumpToFrame(self, position): """Modify the position in the video. :param position: a value between 0 and 1 corresponding to the position in the video. 0 is the beginning and 1 is the end. """ if (position > 1.0): position = 1.0 elif (position < 0.001): position = 0.001 frame = self.source.get_frame_at_sec(position * self.source.duration).ndarray return frame def readNextFrame(self): """Load the next frame. :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed. """ try: self.rawBuffer = self.source.next().ndarray() except ffvideo.NoMoreData: self.isPlaying = False self.pause() self.rawBuffer = None self.endOfVideo.emit() self.frameChanged.emit() def readNCFrame(self, number): """Load the next frame. :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed. """ position = self.source.current().frameno try: self.rawBuffer = self.source.get_frame_no(position + number).ndarray() except ffvideo.NoMoreData: self.isPlaying = False self.pause() self.endOfVideo.emit() if (self.source.current().frameno >= self.source.duration * self.source.framerate): self.isPlaying = False self.pause() self.endOfVideo.emit() self.frameChanged.emit() def currentPositionRatio(self): """Returns the position in the video. :return: a value between 0 and 1 representing the position in the video. 0 is the beginning and 1 is the end. """ if (self.source is not None and self.source.current() is not None): result = self.source.current().timestamp / self.source.duration if (result > 1.0): return 1.0 elif (result < 0.0): return 0.0 else: return result else: return 1.0 def getFrameNumber(self): """Returns the number of frame in the video. :return: An integer representing the number of frame in the video. """ return int(self.source.duration * self.source.framerate) def getFrameSize(self): return (self.source.frame_width, self.source.frame_height) def getEllapsedTime(self): """Returns the number ellapsed time in the video. :return: An integer representing the number of frame in the video. """ return self.source.current().timestamp