示例#1
0
    def _createStream(self):
        """Create the AVStream and open the codec.
        """
        
        # Allocate a new stream
        stream = avformat.av_new_stream(self._formatCtx, self._formatCtx.nb_streams)
        # Initialize it as a video stream
        avcodec.avcodec_get_context_defaults2(stream.codec.contents, decls.CODEC_TYPE_VIDEO)

        outputFormat = self._formatCtx.oformat.contents
        codecCtx = stream.codec.contents
        
        if outputFormat.flags & decls.AVFMT_GLOBALHEADER:
            stream.codec.contents.flags |= decls.CODEC_FLAG_GLOBAL_HEADER
        
        # Prepare the codec...
        codecId = avformat.av_guess_codec(outputFormat, decls.CODEC_TYPE_VIDEO, fileName=self._formatCtx.filename)
        if codecId is None:
            raise ValueError("Could not determine video codec to use for encoding the video.")
        codec = avcodec.avcodec_find_encoder(codecId)
        if codec is None:
            raise ValueError("Could not find encoder.")

        if codec.supported_framerates:
            print "mediafile: supported framerates available"
        else:
            print "mediafile: supported framerates not available"

        codecCtx.codec_id = codecId
        num,den = self._frameRate
        codecCtx.time_base.den = num
        codecCtx.time_base.num = den
        width,height = self._size
        codecCtx.width = width
        codecCtx.height = height
        codecCtx.sample_aspect_ratio = avutil.av_d2q(self._pixelAspect, 255)
        codecCtx.pix_fmt = decls.PIX_FMT_YUV420P
        stream.sample_aspect_ratio = codecCtx.sample_aspect_ratio
        
        # Check if the codec supports the pixel format (if not, switch the format)
        if codec.pix_fmts:
            i = 0
            while codec.pix_fmts[i]!=-1:
                if codecCtx.pix_fmt==codec.pix_fmts[i]:
                    break
                i += 1
            else:
                codecCtx.pix_fmt = codec.pix_fmts[0]
        
        # Open the codec
        avcodec.avcodec_open(codecCtx, codec)
        
        return stream
示例#2
0
    def __init__(self, stream):
        """Constructor.
        
        stream is an AVStream object.
        """
        object.__init__(self)

        # Data callback
        self._dataCallback = None
        
        # AVStream object
        self._stream = stream

        # Get the codec context of the stream (AVCodecContext)
        self._codecCtx = stream.codec.contents

        # Obtain an appropriate decoder (AVCodec)...
        self._codec = avcodec.avcodec_find_decoder(self._codecCtx.codec_id)

        if self._codec is not None:
            # Open the codec
            avcodec.avcodec_open(self._codecCtx, self._codec)