FFmpeg YUV420保存JPEG例子
admin
2023-02-19 22:40:05
0

说明 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]);


相关内容

热门资讯

警方介入女游客高空秋千坠亡,家... 近日,女游客体验高空秋千坠亡事件引发关注。5月5日,四川省广安市华蓥市“5·3”事故调查组发布情况通...
存储涨价之后,如何让AI走向数... 国家数据局数据显示,从2024年到2026年,中国日均Token调用量从1000亿飙升至140万亿(...
华为MatePad Pro M... IT之家 5 月 6 日消息,华为海外账号 Huawei Mobile 今日公布了一款华为 Mate...
万象 一体化防爆气象仪:做高危... WX-FBQ2万象 一体化防爆气象仪:做高危环境的“硬核守护者” 在石油化工、油气储备及煤矿矿井等高...
母亲节好礼推荐 三星Galax... 母亲节悄然临近,一份兼具心意与实用性的礼物,成为子女们表达感恩的最佳载体。在智能手机成为生活必需品的...
视频丨联合国举行AI主题会议 ... 日,中国、赞比亚常驻联合国代表团和中国科学技术协会在纽约联合国总部共同举办“人工智能能力建设国际合作...
首发2亿RYYB长焦+XMAG... 一、前言:2亿RYYB长焦出手 华为影像旗舰的新答案 为什么有人随手一拍就是氛围感大片,而多数人反复...
谷歌、英伟达押注,这家估值 4... 作者|桦林舞王 编辑|靖宇 1956 年,一批科学家聚在达特茅斯,第一次正式讨论「机器能否思考」。...
英伟达想革光模块的命 问AI· 英伟达CPO技术如何冲击光模块市场格局? 上个月中际旭创一季报公布,营收、净利润双双翻倍...
豆包为什么选择了 “最low”... 针对付费会员,豆包官方回应称,豆包始终提供免费服务,在免费服务的基础上,豆包也在探索推出更多增值服务...