• avformat_alloc_output_context2()/avformat_free_context() //输出上下文
  • avformat_new_stream()
  • avcodec_parameters_copy()
  • avformat_write_header() //生成多媒体文件头
  • av_write_frame()/av_interleaved_write_frame() //写数据,av_interleaved_write_frame用的多一些
  • av_write_trailer() //写文件尾,没有的话内部会执行空函数 ```cpp

    include

    include

    include

    include

    include

static void log_packet(const AVFormatContext fmt_ctx, const AVPacket pkt, const char tag) { AVRational time_base = &fmt_ctx->streams[pkt->stream_index]->time_base; printf(“%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n”, tag, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base), av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base), av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base), pkt->stream_index); }

int main(int argc, char argv[]) { AVOutputFormat ofmt = NULL; AVFormatContext ifmt_ctx = NULL, ofmt_ctx = NULL; AVPacket pkt;

  1. const char *in_filename, *out_filename;
  2. int ret, i;
  3. int stream_index = 0;
  4. int *stream_mapping = NULL;
  5. int stream_mapping_size = 0;
  6. if (argc < 3) {
  7. printf("usage: %s input output \n"
  8. "API example program to remux a media file with libavformat and libavcodec.\n"
  9. "The output format is guessed according to the file extension.\n"
  10. "\n", argv[0]);
  11. return 1;
  12. }
  13. in_filename = argv[1];
  14. out_filename = argv[2];
  15. av_register_all();
  16. if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
  17. fprintf(stderr, "Could not open input file '%s'", in_filename);
  18. goto end;
  19. }
  20. if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
  21. fprintf(stderr, "Failed to retrieve input stream information");
  22. goto end;
  23. }
  24. av_dump_format(ifmt_ctx, 0, in_filename, 0);
  25. /*创建输出上下文*/
  26. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
  27. if (!ofmt_ctx) {
  28. fprintf(stderr, "Could not create output context\n");
  29. ret = AVERROR_UNKNOWN;
  30. goto end;
  31. }
  32. stream_mapping_size = ifmt_ctx->nb_streams;
  33. stream_mapping = av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
  34. if (!stream_mapping) {
  35. ret = AVERROR(ENOMEM);
  36. goto end;
  37. }
  38. ofmt = ofmt_ctx->oformat;
  39. for (i=0; i<ifmt_ctx->nb_streams; i++) {
  40. AVStream *out_stream;
  41. AVStream *in_stream = ifmt_ctx->streams[i];
  42. AVCodecParameters *in_codecpar = in_stream->codecpar;
  43. if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
  44. in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
  45. in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  46. stream_mapping[i] = -1;
  47. continue;
  48. }
  49. stream_mapping[i] = stream_index++;
  50. out_stream = avformat_new_stream(ofmt_ctx, NULL);
  51. if (!out_stream) {
  52. fprintf(stderr, "Failed allocating output stream\n");
  53. ret = AVERROR_UNKNOWN;
  54. goto end;
  55. }
  56. ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
  57. if (ret < 0) {
  58. fprintf(stderr, "Failed to copy codec parameters\n");
  59. goto end;
  60. }
  61. out_stream->codecpar->codec_tag = 0;
  62. }
  63. av_dump_format(ofmt_ctx, 0, out_filename, 1);
  64. if (!(ofmt->flags & AVFMT_NOFILE)) {
  65. ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
  66. if (ret < 0) {
  67. fprintf(stderr, "Could not open output file '%s'", out_filename);
  68. goto end;
  69. }
  70. }
  71. ret = avformat_write_header(ofmt_ctx, NULL);
  72. if (ret < 0) {
  73. fprintf(stderr, "Error occurred when opening output file\n");
  74. goto end;
  75. }
  76. while (1) {
  77. AVStream *in_stream, *out_stream;
  78. ret = av_read_frame(ifmt_ctx, &pkt);
  79. if (ret < 0) {
  80. break;
  81. }
  82. in_stream = ifmt_ctx->streams[pkt.stream_index];
  83. if (pkt.stream_index >= stream_mapping_size ||
  84. stream_mapping[pkt.stream_index] < 0) {
  85. av_packet_unref(&pkt);
  86. continue;
  87. }
  88. pkt.stream_index = stream_mapping[pkt.stream_index];
  89. out_stream = ofmt_ctx->streams[pkt.stream_index];
  90. log_packet(ifmt_ctx, &pkt, "in");
  91. /*copy packet*/
  92. pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  93. pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  94. pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
  95. pkt.pos = -1;
  96. log_packet(ofmt_ctx, &pkt, "out");
  97. ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
  98. if (ret < 0) {
  99. fprintf(stderr, "Error muxing packet\n");
  100. break;
  101. }
  102. av_packet_unref(&pkt);
  103. }
  104. av_write_trailer(ofmt_ctx);

end:
avformat_close_input(&ifmt_ctx);

  1. if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
  2. avio_closep(&ofmt_ctx->pb);
  3. avformat_free_context(ofmt_ctx);
  4. av_freep(&stream_mapping);
  5. if (ret < 0 && ret != AVERROR_EOF) {
  6. fprintf(stderr, "Error occurred:%s \n", av_err2str(ret));
  7. return 1;
  8. }
  9. return 0;

}

编译: gcc mp4_flv.c -lavformat -lavutil -lavcodec -o mp4_flv ```