출처: https://lists.libav.org/pipermail/libav-user/2010-August/005159.html
Hoang Ong ngochoanghcm at gmail.com
Fri Aug 6 19:33:28 CEST 2010
- Previous message: [libav-user] Save AVFrame to jpg file
- Next message: [libav-user] Save AVFrame to jpg file
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Libavcodec can do it. This is the code from Anand, and his code only use ffmpeg libs. First I also think about libjpeg and found a jpeg library including Anroid.mk to port to Android NDK (I'm working in android platform), but I search in ffmpeg source code and see that it has the CODEC_ID_JPEG, so I think it does not need to use libjpeg. int WriteJPEG (AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo){ AVCodecContext *pOCodecCtx; AVCodec *pOCodec; uint8_t *Buffer; int BufSiz; int BufSizActual; int ImgFmt = PIX_FMT_YUVJ420P; //for the newer ffmpeg version, this int to pixelformat FILE *JPEGFile; char JPEGFName[256]; BufSiz = avpicture_get_size ( ImgFmt,pCodecCtx->width,pCodecCtx->height ); Buffer = (uint8_t *)malloc ( BufSiz ); if ( Buffer == NULL ) return ( 0 ); memset ( Buffer, 0, BufSiz ); pOCodecCtx = avcodec_alloc_context ( ); if ( !pOCodecCtx ) { free ( Buffer ); return ( 0 ); } 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; pOCodec = avcodec_find_encoder ( pOCodecCtx->codec_id ); if ( !pOCodec ) { free ( Buffer ); return ( 0 ); } if ( avcodec_open ( pOCodecCtx, pOCodec ) < 0 ) { free ( Buffer ); return ( 0 ); } 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; pFrame->pts = 1; pFrame->quality = pOCodecCtx->global_quality; BufSizActual = avcodec_encode_video( pOCodecCtx,Buffer,BufSiz,pFrame ); sprintf ( JPEGFName, "%06d.jpg", FrameNo ); JPEGFile = fopen ( JPEGFName, "wb" ); fwrite ( Buffer, 1, BufSizActual, JPEGFile ); fclose ( JPEGFile ); avcodec_close ( pOCodecCtx ); free ( Buffer ); return ( BufSizActual ); } I searched on google and saw that many people ask the same question but not found the answer yet. So this will help for developer in the first use ffmpeg libs Hoang Ong On 8/6/2010 12:20 AM, Michael Chisholm wrote: > Here we go again... :) Can't you use libavcodec's builtin mjpeg encoder? > > On 8/5/2010 11:38 AM, Glen Ruedinger wrote: >> You need libjpeg. >> >> On Thu, Aug 5, 2010 at 10:58 AM, Hoang Ong<ngochoanghcm at gmail.com> >> wrote: >> >>> Hi everybody, >>> >>> I can get the AVFrame and output it to a raw image file (.ppm) I try >>> to use >>> sws_scale to convert that image to variety of image type to save it >>> as a jpg >>> file but it not work. I think it not a way to do. >>> >>> I dont have much knowledge about image processing. So anybody know >>> how to >>> use ffmpeg's libs to convert AVFrame to jpg please help me. The code >>> I used >>> to save frame as a ppm file like this: >>> >>> void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) >>> { >>> FILE *pFile; >>> char szFilename[32]; >>> int y; >>> >>> // Open file >>> sprintf(szFilename, "frame.ppm", iFrame); >>> pFile=fopen(szFilename, "wb"); >>> if(pFile==NULL) >>> return; >>> >>> // Write header >>> fprintf(pFile, "P6\n%d %d\n255\n", width, height); >>> >>> // Write pixel data >>> for(y=0; y<height; y++) >>> fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, >>> pFile); >>> >>> // Close file >>> fclose(pFile); >>> } >>> >>> Regards >>> Hoang Ong >>> _______________________________________________ >>> libav-user mailing list >>> libav-user at mplayerhq.hu >>> https://lists.mplayerhq.hu/mailman/listinfo/libav-user >>> >> _______________________________________________ >> libav-user mailing list >> libav-user at mplayerhq.hu >> https://lists.mplayerhq.hu/mailman/listinfo/libav-user >> > > > _______________________________________________ > libav-user mailing list > libav-user at mplayerhq.hu > https://lists.mplayerhq.hu/mailman/listinfo/libav-user
- Previous message: [libav-user] Save AVFrame to jpg file
- Next message: [libav-user] Save AVFrame to jpg file
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
'Programming' 카테고리의 다른 글
The Windows Programming Model (0) | 2015.12.19 |
---|---|
VC++2010, 2013 차이- Windows Forms Application 유무 (0) | 2015.04.22 |
C#, 크로스 스레드 작업이 잘못되었습니다. (0) | 2015.03.19 |