1. #include <stdio.h>
    2. #include <conio.h>
    3. #define __STDC_CONSTANT_MACROS
    4. #ifdef _WIN32
    5. extern "C"{
    6. #include "libavformat/avformat.h"
    7. };
    8. #else
    9. #ifdef __cplusplus
    10. extern "C"{
    11. #endif
    12. #include <libavformat/avformat.h>
    13. #ifdef __cplusplus
    14. };
    15. #endif
    16. #endif
    17. #define errReport(info, val) do{ \
    18. fprintf(stderr, "ERR:: %s %s(line=%d) code=%d\n", __FUNCTION__, info, __LINE__, val);\
    19. getch(); exit(0);\
    20. }while (0);
    21. AVOutputFormat *ofmt_a = NULL, *ofmt_v = NULL;
    22. AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx_a = NULL, *ofmt_ctx_v = NULL;
    23. AVPacket pkt;
    24. int ret, i;
    25. int videoindex = -1, audioindex = -1;
    26. int frame_index = 0;
    27. const char *in_filename = "in.ts";
    28. const char *out_filename_v = "out.h264";
    29. const char *out_filename_a = "out.aac";
    30. int init_demuxer() {
    31. av_register_all();
    32. if (avformat_open_input(&ifmt_ctx, in_filename, 0, 0) < 0)
    33. return -1;
    34. if (avformat_find_stream_info(ifmt_ctx, 0) < 0)
    35. return -2;
    36. avformat_alloc_output_context2(&ofmt_ctx_v, NULL, NULL, out_filename_v);
    37. if (!ofmt_ctx_v) return -3;
    38. ofmt_v = ofmt_ctx_v->oformat;
    39. avformat_alloc_output_context2(&ofmt_ctx_a, NULL, NULL, out_filename_a);
    40. if (!ofmt_ctx_a) return -4;
    41. ofmt_a = ofmt_ctx_a->oformat;
    42. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
    43. AVFormatContext *ofmt_ctx;
    44. AVStream *in_stream = ifmt_ctx->streams[i];
    45. AVStream *out_stream = NULL;
    46. if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
    47. videoindex = i;
    48. out_stream = avformat_new_stream(ofmt_ctx_v, in_stream->codec->codec);
    49. ofmt_ctx = ofmt_ctx_v;
    50. }
    51. else if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
    52. audioindex = i;
    53. out_stream = avformat_new_stream(ofmt_ctx_a, in_stream->codec->codec);
    54. ofmt_ctx = ofmt_ctx_a;
    55. }
    56. else {
    57. break;
    58. }
    59. if (!out_stream) return -5;
    60. if (avcodec_copy_context(out_stream->codec, in_stream->codec) < 0)
    61. return -6;
    62. out_stream->codec->codec_tag = 0;
    63. if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
    64. out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
    65. }
    66. printf("================Input===============\n");
    67. av_dump_format(ifmt_ctx, 0, in_filename, 0);
    68. printf("================Output==============\n");
    69. av_dump_format(ofmt_ctx_v, 0, out_filename_v, 1);
    70. printf("++++++++++++++++++++++++++++++++++++\n");
    71. av_dump_format(ofmt_ctx_a, 0, out_filename_a, 1);
    72. printf("====================================\n");
    73. if (!(ofmt_v->flags & AVFMT_NOFILE)) {
    74. if (avio_open(&ofmt_ctx_v->pb, out_filename_v, AVIO_FLAG_WRITE) < 0)
    75. return -7;
    76. }
    77. if (!(ofmt_a->flags & AVFMT_NOFILE)) {
    78. if (avio_open(&ofmt_ctx_a->pb, out_filename_a, AVIO_FLAG_WRITE) < 0)
    79. return -8;
    80. }
    81. if (avformat_write_header(ofmt_ctx_v, NULL) < 0)
    82. return -9;
    83. if (avformat_write_header(ofmt_ctx_a, NULL) < 0)
    84. return -10;
    85. return 0;
    86. }
    87. int main(int argc, char* argv[]){
    88. if ((ret = init_demuxer()) < 0)
    89. errReport("init_demuxer", ret);
    90. while (1) {
    91. AVFormatContext *ofmt_ctx;
    92. AVStream *in_stream, *out_stream;
    93. if (av_read_frame(ifmt_ctx, &pkt) < 0)
    94. break;
    95. in_stream = ifmt_ctx->streams[pkt.stream_index];
    96. if (pkt.stream_index == videoindex) {
    97. out_stream = ofmt_ctx_v->streams[0];
    98. ofmt_ctx = ofmt_ctx_v;
    99. printf("\nv#s:%d\tp:%lld", pkt.size, pkt.pts);
    100. }
    101. else if (pkt.stream_index == audioindex) {
    102. out_stream = ofmt_ctx_a->streams[0];
    103. ofmt_ctx = ofmt_ctx_a;
    104. printf("a#s:%d\tp:%lld", pkt.size, pkt.pts);
    105. }
    106. else {
    107. continue;
    108. }
    109. pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
    110. pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
    111. pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
    112. pkt.pos = -1;
    113. pkt.stream_index = 0;
    114. if (av_interleaved_write_frame(ofmt_ctx, &pkt) < 0) {
    115. printf("Error muxing packet\n");
    116. break;
    117. }
    118. //printf("Write %8d frames to output file\n",frame_index);
    119. av_free_packet(&pkt);
    120. frame_index++;
    121. }
    122. av_write_trailer(ofmt_ctx_a);
    123. av_write_trailer(ofmt_ctx_v);
    124. avformat_close_input(&ifmt_ctx);
    125. if (ofmt_ctx_a && !(ofmt_a->flags & AVFMT_NOFILE))
    126. avio_close(ofmt_ctx_a->pb);
    127. if (ofmt_ctx_v && !(ofmt_v->flags & AVFMT_NOFILE))
    128. avio_close(ofmt_ctx_v->pb);
    129. avformat_free_context(ofmt_ctx_a);
    130. avformat_free_context(ofmt_ctx_v);
    131. if (ret < 0 && ret != AVERROR_EOF) {
    132. printf("Error occurred.\n");
    133. return -1;
    134. }
    135. printf("successed.\n");
    136. getch();
    137. return 0;
    138. }

    qrcode_for_gh_e95b474fcf08_344.jpg