remuxing.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2013 Stefano Sabatini
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file
  24. * libavformat/libavcodec demuxing and muxing API example.
  25. *
  26. * Remux streams from one container format to another.
  27. * @example remuxing.c
  28. */
  29. #include <libavutil/timestamp.h>
  30. #include <libavformat/avformat.h>
  31. static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt, const char *tag)
  32. {
  33. AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
  34. printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
  35. tag,
  36. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
  37. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
  38. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
  39. pkt->stream_index);
  40. }
  41. int main(int argc, char **argv)
  42. {
  43. AVOutputFormat *ofmt = NULL;
  44. AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
  45. AVPacket pkt;
  46. const char *in_filename, *out_filename;
  47. int ret, i;
  48. int stream_index = 0;
  49. int *stream_mapping = NULL;
  50. int stream_mapping_size = 0;
  51. if (argc < 3) {
  52. printf("usage: %s input output\n"
  53. "API example program to remux a media file with libavformat and libavcodec.\n"
  54. "The output format is guessed according to the file extension.\n"
  55. "\n", argv[0]);
  56. return 1;
  57. }
  58. in_filename = argv[1];
  59. out_filename = argv[2];
  60. av_register_all();
  61. if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
  62. fprintf(stderr, "Could not open input file '%s'", in_filename);
  63. goto end;
  64. }
  65. if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
  66. fprintf(stderr, "Failed to retrieve input stream information");
  67. goto end;
  68. }
  69. av_dump_format(ifmt_ctx, 0, in_filename, 0);
  70. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
  71. if (!ofmt_ctx) {
  72. fprintf(stderr, "Could not create output context\n");
  73. ret = AVERROR_UNKNOWN;
  74. goto end;
  75. }
  76. stream_mapping_size = ifmt_ctx->nb_streams;
  77. stream_mapping = av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
  78. if (!stream_mapping) {
  79. ret = AVERROR(ENOMEM);
  80. goto end;
  81. }
  82. ofmt = ofmt_ctx->oformat;
  83. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  84. AVStream *out_stream;
  85. AVStream *in_stream = ifmt_ctx->streams[i];
  86. AVCodecParameters *in_codecpar = in_stream->codecpar;
  87. if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
  88. in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
  89. in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  90. stream_mapping[i] = -1;
  91. continue;
  92. }
  93. stream_mapping[i] = stream_index++;
  94. out_stream = avformat_new_stream(ofmt_ctx, NULL);
  95. if (!out_stream) {
  96. fprintf(stderr, "Failed allocating output stream\n");
  97. ret = AVERROR_UNKNOWN;
  98. goto end;
  99. }
  100. ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
  101. if (ret < 0) {
  102. fprintf(stderr, "Failed to copy codec parameters\n");
  103. goto end;
  104. }
  105. out_stream->codecpar->codec_tag = 0;
  106. }
  107. av_dump_format(ofmt_ctx, 0, out_filename, 1);
  108. if (!(ofmt->flags & AVFMT_NOFILE)) {
  109. ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
  110. if (ret < 0) {
  111. fprintf(stderr, "Could not open output file '%s'", out_filename);
  112. goto end;
  113. }
  114. }
  115. ret = avformat_write_header(ofmt_ctx, NULL);
  116. if (ret < 0) {
  117. fprintf(stderr, "Error occurred when opening output file\n");
  118. goto end;
  119. }
  120. while (1) {
  121. AVStream *in_stream, *out_stream;
  122. ret = av_read_frame(ifmt_ctx, &pkt);
  123. if (ret < 0)
  124. break;
  125. in_stream = ifmt_ctx->streams[pkt.stream_index];
  126. if (pkt.stream_index >= stream_mapping_size ||
  127. stream_mapping[pkt.stream_index] < 0) {
  128. av_packet_unref(&pkt);
  129. continue;
  130. }
  131. pkt.stream_index = stream_mapping[pkt.stream_index];
  132. out_stream = ofmt_ctx->streams[pkt.stream_index];
  133. log_packet(ifmt_ctx, &pkt, "in");
  134. /* copy packet */
  135. pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  136. pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  137. pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
  138. pkt.pos = -1;
  139. log_packet(ofmt_ctx, &pkt, "out");
  140. ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
  141. if (ret < 0) {
  142. fprintf(stderr, "Error muxing packet\n");
  143. break;
  144. }
  145. av_packet_unref(&pkt);
  146. }
  147. av_write_trailer(ofmt_ctx);
  148. end:
  149. avformat_close_input(&ifmt_ctx);
  150. /* close output */
  151. if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
  152. avio_closep(&ofmt_ctx->pb);
  153. avformat_free_context(ofmt_ctx);
  154. av_freep(&stream_mapping);
  155. if (ret < 0 && ret != AVERROR_EOF) {
  156. fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
  157. return 1;
  158. }
  159. return 0;
  160. }