pkt.size= avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
- pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
+ if (c->coded_frame->pts != AV_NOPTS_VALUE)
+ pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
pkt.flags |= PKT_FLAG_KEY;
pkt.stream_index= st->index;
pkt.data= audio_outbuf;
/* write the compressed frame in the media file */
- if (av_write_frame(oc, &pkt) != 0) {
+ if (av_interleaved_write_frame(oc, &pkt) != 0) {
fprintf(stderr, "Error while writing audio frame\n");
exit(1);
}
c->mb_decision=2;
}
// some formats want stream headers to be separate
- if(!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))
+ if(oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
pkt.data= (uint8_t *)picture;
pkt.size= sizeof(AVPicture);
- ret = av_write_frame(oc, &pkt);
+ ret = av_interleaved_write_frame(oc, &pkt);
} else {
/* encode the image */
out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
AVPacket pkt;
av_init_packet(&pkt);
- pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
+ if (c->coded_frame->pts != AV_NOPTS_VALUE)
+ pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
if(c->coded_frame->key_frame)
pkt.flags |= PKT_FLAG_KEY;
pkt.stream_index= st->index;
pkt.size= out_size;
/* write the compressed frame in the media file */
- ret = av_write_frame(oc, &pkt);
+ ret = av_interleaved_write_frame(oc, &pkt);
} else {
ret = 0;
}
}
/* allocate the output media context */
- oc = av_alloc_format_context();
+ oc = avformat_alloc_context();
if (!oc) {
fprintf(stderr, "Memory error\n");
exit(1);
}
}
+ /* write the trailer, if any. the trailer must be written
+ * before you close the CodecContexts open when you wrote the
+ * header; otherwise write_trailer may try to use memory that
+ * was freed on av_codec_close() */
+ av_write_trailer(oc);
+
/* close each codec */
if (video_st)
close_video(oc, video_st);
if (audio_st)
close_audio(oc, audio_st);
- /* write the trailer, if any */
- av_write_trailer(oc);
-
/* free the streams */
for(i = 0; i < oc->nb_streams; i++) {
av_freep(&oc->streams[i]->codec);