AVFormatContext *oc; AVPicture spicture, dpicture; enum AVPixelFormat sfmt = PIX_FMT_RGB32; int iwidth = 1280; int iheight = 360; avformat_alloc_output_context2(&oc, NULL, "flv", fname); avio_open(&oc->pb, URLBytes, AVIO_FLAG_WRITE); AVCodec cvideo = avcodec_find_encoder(AV_CODEC_ID_H264); AVStream svideo = avformat_new_stream(oc, cvideo); svideo->codec->pix_fmt = PIX_FMT_YUV420P; svideo->codec->bit_rate = 921000; svideo->codec->width = iwidth / 2; svideo->codec->height = iheight; svideo->codec->time_base.num = 1; svideo->codec->time_base.den = FLV_TIMEBASE; svideo->codec->codec_tag = FLV_TAG_TYPE_VIDEO; svideo->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; avcodec_open2(svideo->codec, cvideo, NULL); avpicture_alloc(&spicture, sfmt, iwidth, iheight); avpicture_alloc(&dpicture, svideo->codec->pix_fmt, iwidth, iheight * 2); AVFrame *frame = avcodec_alloc_frame(); *((AVPicture *) frame) = dpicture; // ... // fill and scale dpicture int w, h; unsigned char *buffer = extractPixelBuffer(sampleBuffer, &w, &h); avpicture_fill(&spicture, buffer, sfmt, w, h); SwsContext *swsCtx = sws_getContext(w, h, sfmt, iwidth, iheight * 2, svideo->codec->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL); sws_scale(swsCtx, (const uint8_t * const *) spicture.data, spicture.linesize, 0, iheight, dpicture.data, dpicture.linesize); sws_freeContext(swsCtx); // ... // encode frame and write it to video int succ, ret; AVPacket pkt = {0}; av_init_packet(&pkt); avcodec_encode_video2(svideo->codec, &pkt, frame, &succ); av_interleaved_write_frame(oc, &pkt);