<div style="font:14px/1.5 'Lucida Grande', '微软雅黑';color:#333;"><p style="line-height: 1.5; margin: 0px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', sans-serif !important;">Dear all,</p><p style="line-height: 1.5; margin: 0px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', sans-serif !important;"><span class="Apple-tab-span" style="white-space: pre;"> </span>I am trying to decode an flv file to Frame and encode them after some modifies, then I will send it to a rtmp server. but I got segmentation fault when I called the `av_write_frame`. I have searched in google and other website, but it doesn’t help.</p><p style="line-height: 1.5; margin: 0px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', sans-serif !important;"><br></p><p style="line-height: 1.5; margin: 0px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', sans-serif !important;">compile command: gcc src/encode.c -o encode.out -g -v -I /usr/local/ffmpeg/include -L /usr/local/ffmpeg/lib -lavformat -lavcodec -lavutil</p><p style="line-height: 1.5; margin: 0px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', sans-serif !important;">run command: encode.out /tmp/out.flv rtmp://domain/live/livestream</p><p style="line-height: 1.5; margin: 0px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', sans-serif !important;">code is here:</p><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-size: 9pt; font-family: 'Lucida Grande', 'Lucida Sans Unicode', sans-serif !important;"><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <math.h><br></span><span style="color: rgb(106, 135, 89);"><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/opt.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavcodec/avcodec.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/channel_layout.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/common.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/imgutils.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/mathematics.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/samplefmt.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/timestamp.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavformat/avformat.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/mathematics.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/time.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libswscale/swscale.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/log.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <libavutil/mathematics.h><br></span><span style="color: rgb(187, 181, 41);">#include</span><span style="color: rgb(106, 135, 89);"> <stdio.h><br></span><span style="color: rgb(106, 135, 89);"><br></span><span style="color: rgb(106, 135, 89);"><br></span><span style="color: rgb(187, 181, 41);">#define </span>INBUF_SIZE <span style="color: rgb(104, 151, 187);">4096<br></span><span style="color: rgb(187, 181, 41);">#define </span>AUDIO_INBUF_SIZE <span style="color: rgb(104, 151, 187);">20480<br></span><span style="color: rgb(187, 181, 41);">#define </span>AUDIO_REFILL_THRESH <span style="color: rgb(104, 151, 187);">4096<br></span><span style="color: rgb(104, 151, 187);"><br></span><span style="color: rgb(128, 128, 128);">// compatibility with newer API<br></span><span style="color: rgb(187, 181, 41);">#if </span>LIBAVCODEC_VERSION_INT < AV_VERSION_INT(<span style="color: rgb(104, 151, 187);">55</span><span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">28</span><span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">1</span>)<br><span style="color: rgb(187, 181, 41);">#define </span>av_frame_alloc avcodec_alloc_frame<br><span style="color: rgb(187, 181, 41);">#define </span>av_frame_free avcodec_free_frame<br><span style="color: rgb(187, 181, 41);">#endif<br></span><span style="color: rgb(187, 181, 41);"><br></span><span style="color: rgb(187, 181, 41);">#define </span>STREAM_DURATION   <span style="color: rgb(104, 151, 187);">20<br></span><span style="color: rgb(187, 181, 41);">#define </span>STREAM_FRAME_RATE <span style="color: rgb(104, 151, 187);">25 </span><span style="color: rgb(128, 128, 128);">/* 25 images/s */<br></span><span style="color: rgb(187, 181, 41);">#define </span>STREAM_PIX_FMT   AV_PIX_FMT_YUV420P <span style="color: rgb(128, 128, 128);">/* default pix_fmt */ //AV_PIX_FMT_NV12;<br></span><span style="color: rgb(187, 181, 41);">#define </span>VIDEO_CODEC_ID CODEC_ID_H264<br><br><span style="color: rgb(128, 128, 128);">/* video output */<br></span><span style="color: rgb(204, 120, 50); font-weight: bold;">static </span>AVFrame *frame<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50); font-weight: bold;">static </span>AVPicture src_picture<span style="color: rgb(204, 120, 50);">, </span>dst_picture<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span>AVCodec *encoder<span style="color: rgb(204, 120, 50);">;<br></span>AVCodecContext *outputCodecCtx = NULL<span style="color: rgb(204, 120, 50);">;<br></span>AVFormatContext *outputFormatCtx<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span>AVFormatContext *inputFormatCtx = NULL<span style="color: rgb(204, 120, 50);">;<br></span>AVCodecContext *inputCodecCtx = NULL<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>video_index = -<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50); font-weight: bold;">static int </span>open_video(AVFormatContext *oc<span style="color: rgb(204, 120, 50);">, </span>AVCodec *codec<span style="color: rgb(204, 120, 50);">, </span>AVStream *st)<br>{<br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>ret<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>AVCodecContext *c = st->codec<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(128, 128, 128);">/* open the codec */<br></span><span style="color: rgb(128, 128, 128);">    </span>ret = avcodec_open2(c<span style="color: rgb(204, 120, 50);">, </span>codec<span style="color: rgb(204, 120, 50);">, </span>NULL)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>        av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Could not open video codec.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>avcodec_get_name(c->codec_id))<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">else </span>{<br><br>        <span style="color: rgb(128, 128, 128);">/* allocate and init a re-usable frame */<br></span><span style="color: rgb(128, 128, 128);">        </span>frame = av_frame_alloc()<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(!frame) {<br>            av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Could not allocate video frame.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>ret = -<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>}<br>        <span style="color: rgb(204, 120, 50); font-weight: bold;">else </span>{<br>            frame->format = c->pix_fmt<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>frame->width = c->width<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>frame->height = c->height<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(128, 128, 128);">/* Allocate the encoded raw picture. */<br></span><span style="color: rgb(128, 128, 128);">            </span>ret = avpicture_alloc(&dst_picture<span style="color: rgb(204, 120, 50);">, </span>c->pix_fmt<span style="color: rgb(204, 120, 50);">, </span>c->width<span style="color: rgb(204, 120, 50);">, </span>c->height)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>                av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Could not allocate picture.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>}<br>            <span style="color: rgb(204, 120, 50); font-weight: bold;">else </span>{<br>                <span style="color: rgb(128, 128, 128);">/* copy data and linesize picture pointers to frame */<br></span><span style="color: rgb(128, 128, 128);">                </span>*((AVPicture *)frame) = dst_picture<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>}<br>        }<br>    }<br><br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>ret<span style="color: rgb(204, 120, 50);">;<br></span>}<br><br><br><span style="color: rgb(204, 120, 50); font-weight: bold;">static void </span>fill_yuv_image(AVPicture *pict<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>frame_index<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>width<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>height)<br>{<br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>x<span style="color: rgb(204, 120, 50);">, </span>y<span style="color: rgb(204, 120, 50);">, </span>i<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span>i = frame_index<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(128, 128, 128);">/* Y */<br></span><span style="color: rgb(128, 128, 128);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">for </span>(y = <span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>y < height<span style="color: rgb(204, 120, 50);">; </span>y++)<br>        <span style="color: rgb(204, 120, 50); font-weight: bold;">for </span>(x = <span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>x < width<span style="color: rgb(204, 120, 50);">; </span>x++)<br>            pict->data[<span style="color: rgb(104, 151, 187);">0</span>][y * pict->linesize[<span style="color: rgb(104, 151, 187);">0</span>] + x] = x + y + i * <span style="color: rgb(104, 151, 187);">3</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(128, 128, 128);">/* Cb and Cr */<br></span><span style="color: rgb(128, 128, 128);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">for </span>(y = <span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>y < height / <span style="color: rgb(104, 151, 187);">2</span><span style="color: rgb(204, 120, 50);">; </span>y++) {<br>        <span style="color: rgb(204, 120, 50); font-weight: bold;">for </span>(x = <span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>x < width / <span style="color: rgb(104, 151, 187);">2</span><span style="color: rgb(204, 120, 50);">; </span>x++) {<br>            pict->data[<span style="color: rgb(104, 151, 187);">1</span>][y * pict->linesize[<span style="color: rgb(104, 151, 187);">1</span>] + x] = <span style="color: rgb(104, 151, 187);">128 </span>+ y + i * <span style="color: rgb(104, 151, 187);">2</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>pict->data[<span style="color: rgb(104, 151, 187);">2</span>][y * pict->linesize[<span style="color: rgb(104, 151, 187);">2</span>] + x] = <span style="color: rgb(104, 151, 187);">64 </span>+ x + i * <span style="color: rgb(104, 151, 187);">5</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>}<br>    }<br>}<br><br><span style="color: rgb(204, 120, 50); font-weight: bold;">static int </span>write_video_frame(AVFormatContext *oc<span style="color: rgb(204, 120, 50);">, </span>AVStream *st<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>frameCount<span style="color: rgb(204, 120, 50);">, </span>AVFrame *frame<span style="color: rgb(204, 120, 50);">, </span>int64_t start_time)<br>{<br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>ret = <span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>AVCodecContext *c = st->codec<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>printf(<span style="color: rgb(106, 135, 89);">"start_time %d</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>start_time)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span>fill_yuv_image(&dst_picture<span style="color: rgb(204, 120, 50);">, </span>frameCount<span style="color: rgb(204, 120, 50);">, </span>c->width<span style="color: rgb(204, 120, 50);">, </span>c->height)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span>AVPacket pkt = { <span style="color: rgb(104, 151, 187);">0 </span>}<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>got_packet<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>av_init_packet(&pkt)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(128, 128, 128);">/* encode the image */<br></span><span style="color: rgb(128, 128, 128);">    </span>frame->pts = frameCount<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>ret = avcodec_encode_video2(c<span style="color: rgb(204, 120, 50);">, </span>&pkt<span style="color: rgb(204, 120, 50);">, </span>frame<span style="color: rgb(204, 120, 50);">, </span>&got_packet)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>        av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Error encoding video frame.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">else </span>{<br>        <span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(got_packet) {<br>            pkt.stream_index = st->index<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>pkt.pts = av_rescale_q_rnd(pkt.pts<span style="color: rgb(204, 120, 50);">, </span>inputFormatCtx->streams[video_index]->time_base<span style="color: rgb(204, 120, 50);">,<br></span><span style="color: rgb(204, 120, 50);">                                       </span>outputFormatCtx->streams[st->index]->time_base<span style="color: rgb(204, 120, 50);">, </span>AV_ROUND_NEAR_INF)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>ret = av_write_frame(oc<span style="color: rgb(204, 120, 50);">, </span>&pkt)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">            </span>AVRational time_base = inputFormatCtx->streams[video_index]->time_base<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>AVRational time_base_q = {<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">, </span>AV_TIME_BASE}<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>int64_t pts_time = av_rescale_q(pkt.pts<span style="color: rgb(204, 120, 50);">, </span>time_base<span style="color: rgb(204, 120, 50);">, </span>time_base_q)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>int64_t now_time = av_gettime() - start_time<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>int64_t sleep_time = pts_time - now_time<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>printf(<span style="color: rgb(106, 135, 89);">"pts time %d, now_time %d, start_time %d</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>pts_time<span style="color: rgb(204, 120, 50);">, </span>now_time<span style="color: rgb(204, 120, 50);">, </span>start_time)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(pts_time > now_time){<br>                printf(<span style="color: rgb(106, 135, 89);">"sleep %d</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>sleep_time)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span>av_usleep(pts_time - now_time)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>}<br><br>            <span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>                av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Error while writing video frame.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>}<br>        }<br>    }<br><br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>ret<span style="color: rgb(204, 120, 50);">;<br></span>}<br><br><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>open_output_file(<span style="color: rgb(204, 120, 50); font-weight: bold;">const char</span>* filename) {<br>    avformat_alloc_output_context2(&outputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>NULL<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"flv"</span><span style="color: rgb(204, 120, 50);">, </span>filename)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(!outputFormatCtx) {<br>        av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_FATAL<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Could not allocate an output context for '%s'.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>filename)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br><br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(!outputFormatCtx->oformat) {<br>        av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_FATAL<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Could not create the output format for '%s'.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>filename)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br>}<br><br><span style="color: rgb(204, 120, 50); font-weight: bold;">static </span>AVStream *add_stream(AVFormatContext *oc<span style="color: rgb(204, 120, 50);">, </span>AVCodec **codec<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(204, 120, 50); font-weight: bold;">enum </span>AVCodecID codec_id)<br>{<br>    AVCodecContext *c<span style="color: rgb(204, 120, 50);">, </span>*input_c<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>AVStream *st<span style="color: rgb(204, 120, 50);">, </span>*input_stream<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span>input_stream = inputFormatCtx->streams[video_index]<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>input_c = input_stream->codec<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(128, 128, 128);">/* find the encoder */<br></span><span style="color: rgb(128, 128, 128);">    </span>*codec = avcodec_find_encoder(codec_id)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(!(*codec)) {<br>        av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Could not find encoder for '%s'.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>avcodec_get_name(codec_id))<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">else </span>{<br>        st = avformat_new_stream(oc<span style="color: rgb(204, 120, 50);">, </span>*codec)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(!st) {<br>            av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Could not allocate stream.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>}<br>        <span style="color: rgb(204, 120, 50); font-weight: bold;">else </span>{<br>            st->id = oc->nb_streams - <span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>st->time_base.den = st->pts.den = input_stream->time_base.den<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>st->time_base.num = st->pts.num = input_stream->time_base.num<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">            </span>c = st->codec<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>c->codec_id = codec_id<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>c->bit_rate = input_c->bit_rate<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>c->width = input_c->width<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>c->height = input_c->height<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>c->time_base.den = input_c->time_base.den<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>c->time_base.num = input_c->time_base.num<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>c->gop_size = input_c->gop_size<span style="color: rgb(204, 120, 50);">; </span><span style="color: rgb(128, 128, 128);">/* emit one intra frame every twelve frames at most */<br></span><span style="color: rgb(128, 128, 128);">            </span>c->pix_fmt = input_c->pix_fmt<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>}<br>    }<br><br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>st<span style="color: rgb(204, 120, 50);">;<br></span>}<br><br><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>open_output_file2(<span style="color: rgb(204, 120, 50); font-weight: bold;">const char </span>*filename) {<br>    AVStream *out_stream<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>AVStream *in_stream<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>AVCodecContext *dec_ctx<span style="color: rgb(204, 120, 50);">, </span>*enc_ctx<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>AVCodec *encoder<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>ret<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">unsigned int </span>i<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span>outputFormatCtx = NULL<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>avformat_alloc_output_context2(&outputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>NULL<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"flv"</span><span style="color: rgb(204, 120, 50);">, </span>filename)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(!outputFormatCtx) {<br>        av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Could not create output context</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>AVERROR_UNKNOWN<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br><br><br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">for </span>(i = <span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>i < inputFormatCtx->nb_streams<span style="color: rgb(204, 120, 50);">; </span>i++) {<br>        out_stream = avformat_new_stream(outputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>NULL)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(!out_stream) {<br>            av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Failed allocating output stream</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>AVERROR_UNKNOWN<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>}<br><br>        in_stream = inputFormatCtx->streams[i]<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>dec_ctx = in_stream->codec<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>enc_ctx = out_stream->codec<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO<br>            || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {<br>            <span style="color: rgb(128, 128, 128);">/* in this example, we choose transcoding to same codec */<br></span><span style="color: rgb(128, 128, 128);">            </span>encoder = avcodec_find_encoder(dec_ctx->codec_id)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(!encoder) {<br>                av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_FATAL<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Necessary encoder not found</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>AVERROR_INVALIDDATA<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>}<br><br>            <span style="color: rgb(128, 128, 128);">/* In this example, we transcode to same properties (picture size,<br></span><span style="color: rgb(128, 128, 128);">             * sample rate etc.). These properties can be changed for output<br></span><span style="color: rgb(128, 128, 128);">             * streams easily using filters */<br></span><span style="color: rgb(128, 128, 128);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {<br>                enc_ctx->height = dec_ctx->height<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span>enc_ctx->width = dec_ctx->width<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span>enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span><span style="color: rgb(128, 128, 128);">/* take first format from list of supported formats */<br></span><span style="color: rgb(128, 128, 128);">                </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(encoder->pix_fmts)<br>                    enc_ctx->pix_fmt = encoder->pix_fmts[<span style="color: rgb(104, 151, 187);">0</span>]<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span><span style="color: rgb(204, 120, 50); font-weight: bold;">else<br></span><span style="color: rgb(204, 120, 50); font-weight: bold;">                    </span>enc_ctx->pix_fmt = dec_ctx->pix_fmt<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span><span style="color: rgb(128, 128, 128);">/* video time_base can be set to whatever is handy and supported by encoder */<br></span><span style="color: rgb(128, 128, 128);">                </span>enc_ctx->time_base = dec_ctx->time_base<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>} <span style="color: rgb(204, 120, 50); font-weight: bold;">else </span>{<br>                enc_ctx->sample_rate = dec_ctx->sample_rate<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span>enc_ctx->channel_layout = dec_ctx->channel_layout<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span>enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span><span style="color: rgb(128, 128, 128);">/* take first format from list of supported formats */<br></span><span style="color: rgb(128, 128, 128);">                </span>enc_ctx->sample_fmt = encoder->sample_fmts[<span style="color: rgb(104, 151, 187);">0</span>]<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span>enc_ctx->time_base = (AVRational){<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">, </span>enc_ctx->sample_rate}<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>}<br><br>            <span style="color: rgb(128, 128, 128);">/* Third parameter can be used to pass settings to encoder */<br></span><span style="color: rgb(128, 128, 128);">            </span>ret = avcodec_open2(enc_ctx<span style="color: rgb(204, 120, 50);">, </span>encoder<span style="color: rgb(204, 120, 50);">, </span>NULL)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>                av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Cannot open video encoder for stream #%u</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>i)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>ret<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>}<br>        } <span style="color: rgb(204, 120, 50); font-weight: bold;">else if </span>(dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {<br>            av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_FATAL<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Elementary stream #%d is of unknown type, cannot proceed</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>i)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>AVERROR_INVALIDDATA<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>} <span style="color: rgb(204, 120, 50); font-weight: bold;">else </span>{<br>            <span style="color: rgb(128, 128, 128);">/* if this stream must be remuxed */<br></span><span style="color: rgb(128, 128, 128);">            </span>ret = avcodec_copy_context(outputFormatCtx->streams[i]->codec<span style="color: rgb(204, 120, 50);">,<br></span><span style="color: rgb(204, 120, 50);">                                       </span>inputFormatCtx->streams[i]->codec)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>                av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Copying stream context failed</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>ret<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>}<br>        }<br><br>        <span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(outputFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)<br>            enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span>}<br>    av_dump_format(outputFormatCtx<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">, </span>filename<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">1</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(!(outputFormatCtx->oformat->flags & AVFMT_NOFILE)) {<br>        ret = avio_open(&outputFormatCtx->pb<span style="color: rgb(204, 120, 50);">, </span>filename<span style="color: rgb(204, 120, 50);">, </span>AVIO_FLAG_WRITE)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>            av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Could not open output file '%s'"</span><span style="color: rgb(204, 120, 50);">, </span>filename)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>ret<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>}<br>    }<br><br>    <span style="color: rgb(128, 128, 128);">/* init muxer, write output file header */<br></span><span style="color: rgb(128, 128, 128);">    </span>ret = avformat_write_header(outputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>NULL)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>        av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Error occurred when opening output file</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>ret<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br><br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">return </span><span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">;<br></span>}<br><br><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>encode(AVFrame *pFrame<span style="color: rgb(204, 120, 50);">, </span>AVPacket *packet) {<br>    AVCodecContext * context = outputFormatCtx->streams[<span style="color: rgb(104, 151, 187);">0</span>]->codec<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>got_packet<span style="color: rgb(204, 120, 50);">, </span>ret<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>av_init_packet(packet)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>ret = avcodec_encode_video2(context<span style="color: rgb(204, 120, 50);">, </span>packet<span style="color: rgb(204, 120, 50);">, </span>pFrame<span style="color: rgb(204, 120, 50);">, </span>&got_packet)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if</span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>        av_log(context<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"encode failed</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br><br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">if</span>(got_packet) {<br>        <span style="color: rgb(204, 120, 50); font-weight: bold;">return </span><span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br><br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">return </span><span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span>}<br><br><span style="color: rgb(204, 120, 50); font-weight: bold;">static void </span>log_packet(<span style="color: rgb(204, 120, 50); font-weight: bold;">const </span>AVFormatContext *fmt_ctx<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(204, 120, 50); font-weight: bold;">const </span>AVPacket *pkt)<br>{<br>    AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span>printf(<span style="color: rgb(106, 135, 89);">"pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">,<br></span><span style="color: rgb(204, 120, 50);">           </span>av_ts2str(pkt->pts)<span style="color: rgb(204, 120, 50);">, </span>av_ts2timestr(pkt->pts<span style="color: rgb(204, 120, 50);">, </span>time_base)<span style="color: rgb(204, 120, 50);">,<br></span><span style="color: rgb(204, 120, 50);">           </span>av_ts2str(pkt->dts)<span style="color: rgb(204, 120, 50);">, </span>av_ts2timestr(pkt->dts<span style="color: rgb(204, 120, 50);">, </span>time_base)<span style="color: rgb(204, 120, 50);">,<br></span><span style="color: rgb(204, 120, 50);">           </span>av_ts2str(pkt->duration)<span style="color: rgb(204, 120, 50);">, </span>av_ts2timestr(pkt->duration<span style="color: rgb(204, 120, 50);">, </span>time_base)<span style="color: rgb(204, 120, 50);">,<br></span><span style="color: rgb(204, 120, 50);">           </span>pkt->stream_index)<span style="color: rgb(204, 120, 50);">;<br></span>}<br><br><span style="color: rgb(204, 120, 50); font-weight: bold;">static int </span>open_input_file(<span style="color: rgb(204, 120, 50); font-weight: bold;">char </span>*filename)<br>{<br>    AVCodecContext *inputCodecCtxOrig = NULL<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>AVCodec *pCodec = NULL<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>ret<span style="color: rgb(204, 120, 50);">, </span>i<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if</span>((ret = avformat_open_input(&inputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>filename<span style="color: rgb(204, 120, 50);">, </span>NULL<span style="color: rgb(204, 120, 50);">, </span>NULL)) != <span style="color: rgb(104, 151, 187);">0</span>) {<br>        av_log(inputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"could not open input file</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>ret<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br><br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">if</span>((ret = avformat_find_stream_info(inputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>NULL)) < <span style="color: rgb(104, 151, 187);">0</span>) {<br>        av_log(inputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"could not find stream info"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>ret<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br><br>    <span style="color: rgb(128, 128, 128);">// Dump information about file onto standard error<br></span><span style="color: rgb(128, 128, 128);">    </span>av_dump_format(inputFormatCtx<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">, </span>filename<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">0</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(128, 128, 128);">// Find the first video stream<br></span><span style="color: rgb(128, 128, 128);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">for </span>(i = <span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>i < inputFormatCtx->nb_streams<span style="color: rgb(204, 120, 50);">; </span>i++)<br>        <span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(inputFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {<br>            video_index = i<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">break</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>}<br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(video_index == -<span style="color: rgb(104, 151, 187);">1</span>)<br>        <span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">; </span><span style="color: rgb(128, 128, 128);">// Didn't find a video stream<br></span><span style="color: rgb(128, 128, 128);"><br></span><span style="color: rgb(128, 128, 128);"><br></span><span style="color: rgb(128, 128, 128);">    // Get a pointer to the codec context for the video stream<br></span><span style="color: rgb(128, 128, 128);">    </span>inputCodecCtxOrig = inputFormatCtx->streams[video_index]->codec<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(128, 128, 128);">// Find the decoder for the video stream<br></span><span style="color: rgb(128, 128, 128);">    </span>pCodec = avcodec_find_decoder(inputCodecCtxOrig->codec_id)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(pCodec == NULL) {<br>        av_log(inputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Unsupported codec!</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">; </span><span style="color: rgb(128, 128, 128);">// Codec not found<br></span><span style="color: rgb(128, 128, 128);">    </span>}<br>    <span style="color: rgb(128, 128, 128);">// Copy context<br></span><span style="color: rgb(128, 128, 128);">    </span>inputCodecCtx = avcodec_alloc_context3(pCodec)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(avcodec_copy_context(inputCodecCtx<span style="color: rgb(204, 120, 50);">, </span>inputCodecCtxOrig) != <span style="color: rgb(104, 151, 187);">0</span>) {<br>        av_log(inputCodecCtx<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Couldn't copy codec context"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">; </span><span style="color: rgb(128, 128, 128);">// Error copying codec context<br></span><span style="color: rgb(128, 128, 128);">    </span>}<br><br>    <span style="color: rgb(128, 128, 128);">// Open codec<br></span><span style="color: rgb(128, 128, 128);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(avcodec_open2(inputCodecCtx<span style="color: rgb(204, 120, 50);">, </span>pCodec<span style="color: rgb(204, 120, 50);">, </span>NULL) < <span style="color: rgb(104, 151, 187);">0</span>)<br>        <span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">; </span><span style="color: rgb(128, 128, 128);">// Could not open codec<br></span><span style="color: rgb(128, 128, 128);"><br></span><span style="color: rgb(128, 128, 128);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span><span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">;<br></span>}<br><br><span style="color: rgb(204, 120, 50); font-weight: bold;">static int </span>decode(AVFrame *pFrame)<br>{<br>    AVPacket packet<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>ret<span style="color: rgb(204, 120, 50);">, </span>frameFinished<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">while </span>(av_read_frame(inputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>&packet) >= <span style="color: rgb(104, 151, 187);">0</span>) {<br>        log_packet(inputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>&packet)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(128, 128, 128);">// Is this a packet from the video stream?<br></span><span style="color: rgb(128, 128, 128);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(packet.stream_index == video_index) {<br>            <span style="color: rgb(128, 128, 128);">// Decode video frame<br></span><span style="color: rgb(128, 128, 128);">            </span>ret = avcodec_decode_video2(inputCodecCtx<span style="color: rgb(204, 120, 50);">, </span>pFrame<span style="color: rgb(204, 120, 50);">, </span>&frameFinished<span style="color: rgb(204, 120, 50);">, </span>&packet)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if</span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>                av_log(inputCodecCtx<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"could not decode package</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">                </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>}<br>            <span style="color: rgb(128, 128, 128);">// Did we get a video frame?<br></span><span style="color: rgb(128, 128, 128);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(frameFinished) {<br>                <span style="color: rgb(204, 120, 50); font-weight: bold;">return </span><span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span>}<br>        }<br><br>        <span style="color: rgb(128, 128, 128);">// Free the packet that was allocated by av_read_frame<br></span><span style="color: rgb(128, 128, 128);">        </span>av_free_packet(&packet)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br><br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span>}<br><br><br><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>main(<span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>argc<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(204, 120, 50); font-weight: bold;">char</span>* argv[]) {<br>    av_register_all()<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>avformat_network_init()<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>av_log_set_level(AV_LOG_DEBUG)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>AVStream *video_st<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>AVFrame *pFrame<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>AVCodec *video_codec<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>ret<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span>open_input_file(argv[<span style="color: rgb(104, 151, 187);">1</span>])<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>open_output_file(argv[<span style="color: rgb(104, 151, 187);">2</span>])<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>video_st = add_stream(outputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>&video_codec<span style="color: rgb(204, 120, 50);">, </span>AV_CODEC_ID_H264)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(128, 128, 128);">/* Now that all the parameters are set, we can open the video codec and allocate the necessary encode buffers. */<br></span><span style="color: rgb(128, 128, 128);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(video_st) {<br>        av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_DEBUG<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Video stream codec %s.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);"> "</span><span style="color: rgb(204, 120, 50);">, </span>avcodec_get_name(video_st->codec->codec_id))<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">        </span>ret = open_video(outputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>video_codec<span style="color: rgb(204, 120, 50);">, </span>video_st)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(ret < <span style="color: rgb(104, 151, 187);">0</span>) {<br>            av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_FATAL<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Open video stream failed.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">            </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>}<br>    }<br>    <span style="color: rgb(204, 120, 50); font-weight: bold;">else </span>{<br>        av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_FATAL<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Add video stream for the codec '%s' failed.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>avcodec_get_name(AV_CODEC_ID_H264))<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br><br>    av_dump_format(outputFormatCtx<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">, </span>argv[<span style="color: rgb(104, 151, 187);">2</span>]<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">1</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>ret = avformat_write_header(outputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>NULL)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">if </span>(ret != <span style="color: rgb(104, 151, 187);">0</span>) {<br>        av_log(NULL<span style="color: rgb(204, 120, 50);">, </span>AV_LOG_ERROR<span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(106, 135, 89);">"Failed to connect to RTSP server for '%s'.</span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>argv[<span style="color: rgb(104, 151, 187);">2</span>])<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(204, 120, 50); font-weight: bold;">return </span>-<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>}<br><br>    AVFormatContext *out = outputFormatCtx<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(128, 128, 128);">// Allocate video frame<br></span><span style="color: rgb(128, 128, 128);">    </span>pFrame = av_frame_alloc()<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">int </span>frameCount = <span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>int64_t start_time = av_gettime()<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span>printf(<span style="color: rgb(106, 135, 89);">"start_time %d </span><span style="color: rgb(204, 120, 50);">\n</span><span style="color: rgb(106, 135, 89);">"</span><span style="color: rgb(204, 120, 50);">, </span>start_time)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">    </span><span style="color: rgb(204, 120, 50); font-weight: bold;">while</span>(decode(pFrame) == <span style="color: rgb(104, 151, 187);">0</span>) {<br>        frameCount++<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span><span style="color: rgb(128, 128, 128);">// do something with frame<br></span><span style="color: rgb(128, 128, 128);">        </span>AVPacket packet = {<span style="color: rgb(104, 151, 187);">0</span>}<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);">        </span>write_video_frame(outputFormatCtx<span style="color: rgb(204, 120, 50);">, </span>video_st<span style="color: rgb(204, 120, 50);">, </span>frameCount<span style="color: rgb(204, 120, 50);">, </span>pFrame<span style="color: rgb(204, 120, 50);">, </span>start_time)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"><br></span><span style="color: rgb(128, 128, 128);">//        ret = encode(pFrame, &packet);<br></span><span style="color: rgb(128, 128, 128);">//        printf("ret:%d\n", ret);<br></span><span style="color: rgb(128, 128, 128);">//        if(ret != 0) continue;<br></span><span style="color: rgb(128, 128, 128);">//        ret = av_write_frame(out, &packet);<br></span><span style="color: rgb(128, 128, 128);">//<br></span><span style="color: rgb(128, 128, 128);">//        printf("out format:\n");<br></span><span style="color: rgb(128, 128, 128);">        //log_packet(outputFormatCtx, &packet);<br></span><span style="color: rgb(128, 128, 128);">//        if(ret < 0) {<br></span><span style="color: rgb(128, 128, 128);">//            av_log(outputFormatCtx, AV_LOG_ERROR, "write frame failed\n");<br></span><span style="color: rgb(128, 128, 128);">//        }<br></span><span style="color: rgb(128, 128, 128);">//        ret = flush_encoder(outputFormatCtx, 0);<br></span><span style="color: rgb(128, 128, 128);">//        if (ret < 0) {<br></span><span style="color: rgb(128, 128, 128);">//            printf("Flushing encoder failed\n");<br></span><span style="color: rgb(128, 128, 128);">//            return -1;<br></span><span style="color: rgb(128, 128, 128);">//        }<br></span><span style="color: rgb(128, 128, 128);">    </span>}<br>}</pre></div>