- int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt){
- AVStream *st= s->streams[ pkt->stream_index];
- int ret;
-
- //FIXME/XXX/HACK drop zero sized packets
- if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size==0)
- return 0;
-
- av_dlog(s, "av_interleaved_write_frame size:%d dts:%"PRId64" pts:%"PRId64"\n",
- pkt->size, pkt->dts, pkt->pts);
- if((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
- return ret;
-
- if(pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
- return AVERROR(EINVAL);
-
- for(;;){
- AVPacket opkt;
- int ret= interleave_packet(s, &opkt, pkt, 0);
- if(ret<=0) //FIXME cleanup needed for ret<0 ?
- return ret;
-
- ret= s->oformat->write_packet(s, &opkt);
- if (ret >= 0)
- s->streams[opkt.stream_index]->nb_frames++;
-
- av_free_packet(&opkt);
- pkt= NULL;
-
- if(ret<0)
- return ret;
- if(s->pb && s->pb->error)
- return s->pb->error;
- }
- }
我在编码的时候采用如下的代码:
- AVPacket pkt;
- av_init_packet(&pkt);
-
- if(c->coded_frame->pts != AV_NOPTS_VALUE)
- {
- pkt.pts = av_rescale_q(c->coded_frame->pts,c->time_base,en_info->video_st->time_base);
- }
- if(c->coded_frame->key_frame)
- pkt.flags |= AV_PKT_FLAG_KEY;
- pkt.stream_index = en_info->video_st->index;
- pkt.data = en_info->video_outbuf;
- pkt.size = en_info->out_size;
- ret = av_interleaved_write_frame( en_info->oc , &pkt );