remuxing.c 6.3 KB

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