remuxing.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
  61. fprintf(stderr, "Could not open input file '%s'", in_filename);
  62. goto end;
  63. }
  64. if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
  65. fprintf(stderr, "Failed to retrieve input stream information");
  66. goto end;
  67. }
  68. av_dump_format(ifmt_ctx, 0, in_filename, 0);
  69. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
  70. if (!ofmt_ctx) {
  71. fprintf(stderr, "Could not create output context\n");
  72. ret = AVERROR_UNKNOWN;
  73. goto end;
  74. }
  75. stream_mapping_size = ifmt_ctx->nb_streams;
  76. stream_mapping = av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
  77. if (!stream_mapping) {
  78. ret = AVERROR(ENOMEM);
  79. goto end;
  80. }
  81. ofmt = ofmt_ctx->oformat;
  82. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  83. AVStream *out_stream;
  84. AVStream *in_stream = ifmt_ctx->streams[i];
  85. AVCodecParameters *in_codecpar = in_stream->codecpar;
  86. if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
  87. in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
  88. in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  89. stream_mapping[i] = -1;
  90. continue;
  91. }
  92. stream_mapping[i] = stream_index++;
  93. out_stream = avformat_new_stream(ofmt_ctx, NULL);
  94. if (!out_stream) {
  95. fprintf(stderr, "Failed allocating output stream\n");
  96. ret = AVERROR_UNKNOWN;
  97. goto end;
  98. }
  99. ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
  100. if (ret < 0) {
  101. fprintf(stderr, "Failed to copy codec parameters\n");
  102. goto end;
  103. }
  104. out_stream->codecpar->codec_tag = 0;
  105. }
  106. av_dump_format(ofmt_ctx, 0, out_filename, 1);
  107. if (!(ofmt->flags & AVFMT_NOFILE)) {
  108. ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
  109. if (ret < 0) {
  110. fprintf(stderr, "Could not open output file '%s'", out_filename);
  111. goto end;
  112. }
  113. }
  114. ret = avformat_write_header(ofmt_ctx, NULL);
  115. if (ret < 0) {
  116. fprintf(stderr, "Error occurred when opening output file\n");
  117. goto end;
  118. }
  119. while (1) {
  120. AVStream *in_stream, *out_stream;
  121. ret = av_read_frame(ifmt_ctx, &pkt);
  122. if (ret < 0)
  123. break;
  124. in_stream = ifmt_ctx->streams[pkt.stream_index];
  125. if (pkt.stream_index >= stream_mapping_size ||
  126. stream_mapping[pkt.stream_index] < 0) {
  127. av_packet_unref(&pkt);
  128. continue;
  129. }
  130. pkt.stream_index = stream_mapping[pkt.stream_index];
  131. out_stream = ofmt_ctx->streams[pkt.stream_index];
  132. log_packet(ifmt_ctx, &pkt, "in");
  133. /* copy packet */
  134. pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  135. pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  136. pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
  137. pkt.pos = -1;
  138. log_packet(ofmt_ctx, &pkt, "out");
  139. ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
  140. if (ret < 0) {
  141. fprintf(stderr, "Error muxing packet\n");
  142. break;
  143. }
  144. av_packet_unref(&pkt);
  145. }
  146. av_write_trailer(ofmt_ctx);
  147. end:
  148. avformat_close_input(&ifmt_ctx);
  149. /* close output */
  150. if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
  151. avio_closep(&ofmt_ctx->pb);
  152. avformat_free_context(ofmt_ctx);
  153. av_freep(&stream_mapping);
  154. if (ret < 0 && ret != AVERROR_EOF) {
  155. fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
  156. return 1;
  157. }
  158. return 0;
  159. }