說明 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]);
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享題目:FFmpegYUV420保存JPEG例子-創新互聯
本文鏈接:http://www.yijiale78.com/article30/dichpo.html
成都網站建設公司_創新互聯,為您提供自適應網站、ChatGPT、企業建站、建站公司、用戶體驗、品牌網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯