예제 #1
0
파일: mediafile.py 프로젝트: puzzlet/cgkit
    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
예제 #2
0
파일: mediafile.py 프로젝트: puzzlet/cgkit
 def _initPilProcessing(self):
     """Initialize decoding into a PIL image.
     
     Initializes an intermediate buffer (AVPicture) that will receive
     the converted image and a swscale context to do the conversion. 
     """
     width,height = self.size
     
     dstPixFmt = decls.PIX_FMT_RGB24
     
     # In case, this is called several times, make sure we don't leak memory.
     self._free()
     
     # Allocate a buffer that can hold the converted image
     self._picture = decls.AVPicture()
     avcodec.avpicture_alloc(self._picture, dstPixFmt, width, height)
     # Get the size of the destination image buffer
     self._pictureSize = avcodec.avpicture_get_size(dstPixFmt, width, height)
     
     # Allocate the swscale context
     self._pilSwsCtx = swscale.sws_getContext(width, height, self._srcPixFmt, 
                                              width, height, dstPixFmt, 1)