溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

FFmpeg YUV420保存JPEG例子

發(fā)布時(shí)間:2020-08-02 11:00:08 來(lái)源:網(wǎng)絡(luò) 閱讀:731 作者:fengyuzaitu 欄目:軟件技術(shù)

說(shuō)明 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;
}


調(diào)用例子

SaveYUV420ToJPEG("fengyuzaitu.jpg", pFrame->width, pFrame->height, pFrame->linesize[0], pFrame->data[0], pFrame->data[1], pFrame->data[2]);


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI