FFmpegYUV420保存JPEG例子
说明 FFmpeg3.4版本
宕昌网站建设公司创新互联公司,宕昌网站设计制作,有大型网站制作公司丰富经验。已为宕昌成百上千家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的宕昌做网站的公司定做!
bool SaveYUV420ToJPEG(const std::string &strFileName, int nWidth, int nHeight, int linesize, uint8_t * Y, uint8_t * U, uint8_t * V)
{
AVFormatContext* pFormatCtx;
AVCodecContext* pCodecCtx;
AVCodec* pCodec;
AVFrame* pFrame;
AVPacket pkt;
int nRet = 0;
avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, strFileName.c_str());
AVStream* pVideoAVStream = avformat_new_stream(pFormatCtx, 0);
if (NULL == pVideoAVStream) return false;
pCodecCtx = avcodec_alloc_context3(NULL);
if (NULL == pCodecCtx) return false;
avcodec_parameters_to_context(pCodecCtx, pVideoAVStream->codecpar);
pCodecCtx->codec_id = pFormatCtx->oformat->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P;
pCodecCtx->width = nWidth;
pCodecCtx->height = nHeight;
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 25;
pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (NULL == pCodec) return false;
if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0)
{
return false;
}
pFrame = av_frame_alloc();
int nJpegQuality = 80;
pCodecCtx->qcompress = (float)nJpegQuality/100.f; // 0~1.0, default is 0.5
pCodecCtx->qmin = 2;
pCodecCtx->qmax = 31;
pCodecCtx->max_qdiff = 3;
avformat_write_header(pFormatCtx,NULL);
av_new_packet(&pkt, pCodecCtx->width * pCodecCtx->height * 3);
pFrame->data[0] = Y;
pFrame->data[1] = U;
pFrame->data[2] = V;
pFrame->linesize[0] = linesize;
pFrame->linesize[1] = linesize/2;
pFrame->linesize[2] = linesize/2;
nRet = avcodec_send_frame(pCodecCtx, pFrame);
if (avcodec_receive_packet(pCodecCtx, &pkt) != 0) return false;
pkt.stream_index = 0;
nRet = av_write_frame(pFormatCtx, &pkt);
av_packet_unref(&pkt);
av_write_trailer(pFormatCtx);
avcodec_close(pCodecCtx);
avcodec_free_context(&pCodecCtx);
av_frame_free(&pFrame);
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);
return true;
}
调用例子
SaveYUV420ToJPEG("fengyuzaitu.jpg", pFrame->width, pFrame->height, pFrame->linesize[0], pFrame->data[0], pFrame->data[1], pFrame->data[2]);
网页名称:FFmpegYUV420保存JPEG例子
本文网址:http://scyanting.com/article/iichje.html