Example #1
0
    def __init__(self, formatCtx, size, frameRate, pixelAspect):
        """Constructor.
        
        formatCtx is a AVFormatContext object.
        size is a tuple (width, height) containing the resolution of the
        video images.
        frameRate is the target framerate. It can be a single int or float
        or a tuple (num,den).
        pixelAspect is a float containing the pixel aspect ratio.
        """
        self._formatCtx = formatCtx
        self._size = size
        self._pixelAspect = pixelAspect
        
        try:
            # Check if frameRate is a 2-tuple
            num,den = frameRate
        except:
            # Turn the framerate into a rational...
            r = avutil.av_d2q(frameRate, 255)
            frameRate = (r.num, r.den)
        
        # The framerate as a tuple (num, den).
        self._frameRate = frameRate

        # Create an AVStream
        self._stream = self._createStream()
        
        # Get the codec context of the stream
        self._codecCtx = self._stream.codec.contents
        
        width,height = self._size
        # Set up the buffer that can store the encoded frame
        self._bufSize = 6*width*height+200
        self._buffer = ctypes.create_string_buffer(self._bufSize)

        self._frame = avcodec.avcodec_alloc_frame()
        if self._frame is None:
            raise MemoryError("Failed to allocate AVFrame object")
        avcodec.avpicture_alloc(self._frame, self._codecCtx.pix_fmt, width, height)

        # Allocate a picture for the converted image...
        self._picture = decls.AVPicture()
        avcodec.avpicture_alloc(self._picture, self._codecCtx.pix_fmt, width, height)
        # Get the size of the destination image buffer
        self._pictureSize = avcodec.avpicture_get_size(self._codecCtx.pix_fmt, width, height)
        
        self._pkt = decls.AVPacket()
        avformat.av_init_packet(self._pkt)
        #pkt.stream_index = outputstream.index
        
        self._currentPts = 0
Example #2
0
    def decodeBegin(self):
        
        # Allocate a video frame that will store the decoded frames and
        # create the VideoData object that will be passed to the callers
        # (the actual conversion to RGB (or whatever) is done by the
        # VideoData object)
        try:
            self._frame = avcodec.avcodec_alloc_frame()
            if self._frame is None:
                raise MemoryError("Failed to allocate AVFrame object")
            
            width = self._codecCtx.width
            height = self._codecCtx.height
            self._videoData = VideoData(self, size=(width, height), frame=self._frame, srcPixFmt=self._codecCtx.pix_fmt)
            
            tb = self._stream.time_base
            self._timebase = float(tb.num)/tb.den
        except:
            self.decodeEnd()
            raise

        return True