Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

FFmpeg YUV420 saves JPEG examples

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Describe the FFmpeg3.4 version

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. 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

}

Call example

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report