I tried using the following code and was able to successfully save frames from
an AVI file, but when I tried with an MPEG1 file (25 fps) avcodec_open (
pOCodecCtx, pOCodec ) fails, returning -1. Does anyone know why that would be?
2009/4/2 Clive Taylor <clive at taycon.co.uk>
> Stas,
>
> An update (quite a long one I'm afraid). I changed my code so that all
> of the JPEG writing stuff goes into its own function as below (once
> again, the e-mail system has ruined all of my beautiful code
> formatting):
>
> //
> // Write JPEG
> //
> // Converts and writes the video frame in pFrame to a JPEG file
> //
> // Parameters:
> // pCodexCtx Pointer to the video stream codec
> context
> // pFrame Pointer to Frame exracted from the video stream
> // FrameNo FrameNumber
> // Quality Image quality as %
> //
> // Returns:
> // Size of buffer written or 0 on error
> //
> int
> WriteJPEG ( AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo, int
> Quality )
> {
> AVCodecContext *pOCodecCtx;
> AVCodec *pOCodec;
> BYTE *Buffer;
> int BufSiz;
> int BufSizActual;
> int ImgFmt = PIX_FMT_YUVJ420P;
> FILE *JPEGFile;
> char JPEGFName[256];
>
> BufSiz = avpicture_get_size ( ImgFmt,
> pCodecCtx->width,
> pCodecCtx->height );
> //
> // Allocate local RGB frame buffer
> //
> Buffer = (BYTE *)malloc ( BufSiz );
> if ( Buffer == NULL )
> return ( 0 );
> memset ( Buffer, 0, BufSiz );
> //
> // Allocate the output codec context
> //
> pOCodecCtx = avcodec_alloc_context ( );
> if ( !pOCodecCtx ) {
> free ( Buffer );
> return ( 0 );
> }
> //
> // Initialise format parameters
> //
> pOCodecCtx->bit_rate = pCodecCtx->bit_rate;
> pOCodecCtx->width = pCodecCtx->width;
> pOCodecCtx->height = pCodecCtx->height;
> pOCodecCtx->pix_fmt = ImgFmt;
> pOCodecCtx->codec_id = CODEC_ID_MJPEG;
> pOCodecCtx->codec_type = CODEC_TYPE_VIDEO;
> pOCodecCtx->time_base.num = pCodecCtx->time_base.num;
> pOCodecCtx->time_base.den = pCodecCtx->time_base.den;
> //
> // Allocate the JPEG encoder
> //
> pOCodec = avcodec_find_encoder ( pOCodecCtx->codec_id );
> if ( !pOCodec ) {
> free ( Buffer );
> return ( 0 );
> }
> //
> // Open the JPEG encoder
> //
> if ( avcodec_open ( pOCodecCtx, pOCodec ) < 0 ) {
> free ( Buffer );
> return ( 0 );
> }
> //
> // Initialise the "VBR" settings
> //
> pOCodecCtx->qmin = pOCodecCtx->qmax = Quality;
> pOCodecCtx->mb_lmin = pOCodecCtx->lmin =
> pOCodecCtx->qmin *
> FF_QP2LAMBDA;
> pOCodecCtx->mb_lmax = pOCodecCtx->lmax =
> pOCodecCtx->qmax *
> FF_QP2LAMBDA;
> pOCodecCtx->flags = CODEC_FLAG_QSCALE;
> pOCodecCtx->global_quality = pOCodecCtx->qmin * FF_QP2LAMBDA;
> //
> // Set the timestamp and quality parameters
> //
> pFrame->pts = 1;
> pFrame->quality = pOCodecCtx->global_quality;
> BufSizActual = avcodec_encode_video ( pOCodecCtx,
> Buffer,
> BufSiz,
> pFrame );
> //
> // Write JPEG to file
> //
> sprintf ( JPEGFName, "f%06dq%02d.jpg", FrameNo, Quality );
> JPEGFile = fopen ( JPEGFName, "wb" );
> fwrite ( Buffer, 1, BufSizActual, JPEGFile );
> fclose ( JPEGFile );
> //
> // Clean up and exit
> //
> avcodec_close ( pOCodecCtx );
> free ( Buffer );
> return ( BufSizActual );
> }
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user