您好,登录后才能下订单哦!
说明 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]);
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。