您好,登錄后才能下訂單哦!
說明 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。