remux.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 libavformat/libavcodec demuxing and muxing API usage example
  24. * @example remux.c
  25. *
  26. * Remux streams from one container format to another. Data is copied from the
  27. * input to the output without transcoding.
  28. */
  29. #include <libavutil/mem.h>
  30. #include <libavutil/timestamp.h>
  31. #include <libavformat/avformat.h>
  32. static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt, const char *tag)
  33. {
  34. AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
  35. printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
  36. tag,
  37. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
  38. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
  39. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
  40. pkt->stream_index);
  41. }
  42. int main(int argc, char **argv)
  43. {
  44. const AVOutputFormat *ofmt = NULL;
  45. AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
  46. AVPacket *pkt = NULL;
  47. const char *in_filename, *out_filename;
  48. int ret, i;
  49. int stream_index = 0;
  50. int *stream_mapping = NULL;
  51. int stream_mapping_size = 0;
  52. if (argc < 3) {
  53. printf("usage: %s input output\n"
  54. "API example program to remux a media file with libavformat and libavcodec.\n"
  55. "The output format is guessed according to the file extension.\n"
  56. "\n", argv[0]);
  57. return 1;
  58. }
  59. in_filename = argv[1];
  60. out_filename = argv[2];
  61. pkt = av_packet_alloc();
  62. if (!pkt) {
  63. fprintf(stderr, "Could not allocate AVPacket\n");
  64. return 1;
  65. }
  66. if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
  67. fprintf(stderr, "Could not open input file '%s'", in_filename);
  68. goto end;
  69. }
  70. if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
  71. fprintf(stderr, "Failed to retrieve input stream information");
  72. goto end;
  73. }
  74. av_dump_format(ifmt_ctx, 0, in_filename, 0);
  75. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
  76. if (!ofmt_ctx) {
  77. fprintf(stderr, "Could not create output context\n");
  78. ret = AVERROR_UNKNOWN;
  79. goto end;
  80. }
  81. stream_mapping_size = ifmt_ctx->nb_streams;
  82. stream_mapping = av_calloc(stream_mapping_size, sizeof(*stream_mapping));
  83. if (!stream_mapping) {
  84. ret = AVERROR(ENOMEM);
  85. goto end;
  86. }
  87. ofmt = ofmt_ctx->oformat;
  88. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  89. AVStream *out_stream;
  90. AVStream *in_stream = ifmt_ctx->streams[i];
  91. AVCodecParameters *in_codecpar = in_stream->codecpar;
  92. if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
  93. in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
  94. in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  95. stream_mapping[i] = -1;
  96. continue;
  97. }
  98. stream_mapping[i] = stream_index++;
  99. out_stream = avformat_new_stream(ofmt_ctx, NULL);
  100. if (!out_stream) {
  101. fprintf(stderr, "Failed allocating output stream\n");
  102. ret = AVERROR_UNKNOWN;
  103. goto end;
  104. }
  105. ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
  106. if (ret < 0) {
  107. fprintf(stderr, "Failed to copy codec parameters\n");
  108. goto end;
  109. }
  110. out_stream->codecpar->codec_tag = 0;
  111. }
  112. av_dump_format(ofmt_ctx, 0, out_filename, 1);
  113. if (!(ofmt->flags & AVFMT_NOFILE)) {
  114. ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
  115. if (ret < 0) {
  116. fprintf(stderr, "Could not open output file '%s'", out_filename);
  117. goto end;
  118. }
  119. }
  120. ret = avformat_write_header(ofmt_ctx, NULL);
  121. if (ret < 0) {
  122. fprintf(stderr, "Error occurred when opening output file\n");
  123. goto end;
  124. }
  125. while (1) {
  126. AVStream *in_stream, *out_stream;
  127. ret = av_read_frame(ifmt_ctx, pkt);
  128. if (ret < 0)
  129. break;
  130. in_stream = ifmt_ctx->streams[pkt->stream_index];
  131. if (pkt->stream_index >= stream_mapping_size ||
  132. stream_mapping[pkt->stream_index] < 0) {
  133. av_packet_unref(pkt);
  134. continue;
  135. }
  136. pkt->stream_index = stream_mapping[pkt->stream_index];
  137. out_stream = ofmt_ctx->streams[pkt->stream_index];
  138. log_packet(ifmt_ctx, pkt, "in");
  139. /* copy packet */
  140. av_packet_rescale_ts(pkt, in_stream->time_base, out_stream->time_base);
  141. pkt->pos = -1;
  142. log_packet(ofmt_ctx, pkt, "out");
  143. ret = av_interleaved_write_frame(ofmt_ctx, pkt);
  144. /* pkt is now blank (av_interleaved_write_frame() takes ownership of
  145. * its contents and resets pkt), so that no unreferencing is necessary.
  146. * This would be different if one used av_write_frame(). */
  147. if (ret < 0) {
  148. fprintf(stderr, "Error muxing packet\n");
  149. break;
  150. }
  151. }
  152. av_write_trailer(ofmt_ctx);
  153. end:
  154. av_packet_free(&pkt);
  155. avformat_close_input(&ifmt_ctx);
  156. /* close output */
  157. if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
  158. avio_closep(&ofmt_ctx->pb);
  159. avformat_free_context(ofmt_ctx);
  160. av_freep(&stream_mapping);
  161. if (ret < 0 && ret != AVERROR_EOF) {
  162. fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
  163. return 1;
  164. }
  165. return 0;
  166. }