remuxing.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. if (argc < 3) {
  49. printf("usage: %s input output\n"
  50. "API example program to remux a media file with libavformat and libavcodec.\n"
  51. "The output format is guessed according to the file extension.\n"
  52. "\n", argv[0]);
  53. return 1;
  54. }
  55. in_filename = argv[1];
  56. out_filename = argv[2];
  57. av_register_all();
  58. if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
  59. fprintf(stderr, "Could not open input file '%s'", in_filename);
  60. goto end;
  61. }
  62. if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
  63. fprintf(stderr, "Failed to retrieve input stream information");
  64. goto end;
  65. }
  66. av_dump_format(ifmt_ctx, 0, in_filename, 0);
  67. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
  68. if (!ofmt_ctx) {
  69. fprintf(stderr, "Could not create output context\n");
  70. ret = AVERROR_UNKNOWN;
  71. goto end;
  72. }
  73. ofmt = ofmt_ctx->oformat;
  74. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  75. AVStream *in_stream = ifmt_ctx->streams[i];
  76. AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
  77. if (!out_stream) {
  78. fprintf(stderr, "Failed allocating output stream\n");
  79. ret = AVERROR_UNKNOWN;
  80. goto end;
  81. }
  82. ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
  83. if (ret < 0) {
  84. fprintf(stderr, "Failed to copy context from input to output stream codec context\n");
  85. goto end;
  86. }
  87. out_stream->codec->codec_tag = 0;
  88. if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  89. out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  90. }
  91. av_dump_format(ofmt_ctx, 0, out_filename, 1);
  92. if (!(ofmt->flags & AVFMT_NOFILE)) {
  93. ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
  94. if (ret < 0) {
  95. fprintf(stderr, "Could not open output file '%s'", out_filename);
  96. goto end;
  97. }
  98. }
  99. ret = avformat_write_header(ofmt_ctx, NULL);
  100. if (ret < 0) {
  101. fprintf(stderr, "Error occurred when opening output file\n");
  102. goto end;
  103. }
  104. while (1) {
  105. AVStream *in_stream, *out_stream;
  106. ret = av_read_frame(ifmt_ctx, &pkt);
  107. if (ret < 0)
  108. break;
  109. in_stream = ifmt_ctx->streams[pkt.stream_index];
  110. out_stream = ofmt_ctx->streams[pkt.stream_index];
  111. log_packet(ifmt_ctx, &pkt, "in");
  112. /* copy packet */
  113. pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  114. pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  115. pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
  116. pkt.pos = -1;
  117. log_packet(ofmt_ctx, &pkt, "out");
  118. ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
  119. if (ret < 0) {
  120. fprintf(stderr, "Error muxing packet\n");
  121. break;
  122. }
  123. av_packet_unref(&pkt);
  124. }
  125. av_write_trailer(ofmt_ctx);
  126. end:
  127. avformat_close_input(&ifmt_ctx);
  128. /* close output */
  129. if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
  130. avio_closep(&ofmt_ctx->pb);
  131. avformat_free_context(ofmt_ctx);
  132. if (ret < 0 && ret != AVERROR_EOF) {
  133. fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
  134. return 1;
  135. }
  136. return 0;
  137. }