#include
#include
#define __STDC_CONSTANT_MACROS
#ifdef _WIN32 //Windows
extern "C" {
#include "libavformat/avformat.h"
};
#else //Linux...
#ifdef __cplusplus
extern "C" {
#endif
#include
#ifdef __cplusplus
};
#endif
#endif
#define errReport(info, val) do{ \
fprintf(stderr, "ERR:: %s %s(line=%d) code=%d\n", __FUNCTION__, info, __LINE__, val);\
getch(); exit(0);\
}while (0);
typedef struct {
AVFormatContext *ifmt_ctx;
int stream_index = 0;
AVStream *in_stream, *out_stream;
}data_t;
AVOutputFormat *ofmt = NULL;
AVFormatContext *ifmt_ctx_v = NULL, *ifmt_ctx_a = NULL, *ofmt_ctx = NULL;
AVPacket pkt;
int videoindex_v = -1, videoindex_out = -1;
int audioindex_a = -1, audioindex_out = -1;
int frame_index = 0;
int64_t cur_pts_v = 0, cur_pts_a = 0;
#if 0
const char *in_filename_v = "input.h264";
const char *in_filename_a = "input.aac";
#if 1
const char *out_filename = "output.mkv";
#else
const char *out_filename = "output.ts";
#endif
#else
const char *in_filename_v = "input.h264";
const char *in_filename_a = "input.mp3";
#if 0
const char *out_filename = "output.mp4";
#elif 1
const char *out_filename = "output.flv";
#else
const char *out_filename = "output.avi";
#endif
#endif
int create_out_stream(AVFormatContext **fmt_ctx, AVMediaType type, int *idx, int *idx_o) {
for (int i = 0; i < (*fmt_ctx)->nb_streams; i++) {
if ((*fmt_ctx)->streams[i]->codec->codec_type == type) {
AVStream *in_stream = (*fmt_ctx)->streams[i];
AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
*idx = i;
if (!out_stream) return -1;
*idx_o = out_stream->index;
if (avcodec_copy_context(out_stream->codec, in_stream->codec) < 0)
return -2;
out_stream->codec->codec_tag = 0;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
break;
}
}
return 0;
}
int init_muxer() {
int i;
av_register_all();
if (avformat_open_input(&ifmt_ctx_v, in_filename_v, 0, 0) < 0)
return -1;
if (avformat_find_stream_info(ifmt_ctx_v, 0) < 0)
return -2;
if (avformat_open_input(&ifmt_ctx_a, in_filename_a, 0, 0) < 0)
return -3;
if (avformat_find_stream_info(ifmt_ctx_a, 0) < 0)
return -4;
printf("======================Input Information=====================\n");
av_dump_format(ifmt_ctx_v, 0, in_filename_v, 0);
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
av_dump_format(ifmt_ctx_a, 0, in_filename_a, 0);
printf("============================================================\n");
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
if (!ofmt_ctx) return -5;
ofmt = ofmt_ctx->oformat;
if (create_out_stream(&ifmt_ctx_v, AVMEDIA_TYPE_VIDEO, &videoindex_v, &videoindex_out) < 0)
return -6;
if (create_out_stream(&ifmt_ctx_a, AVMEDIA_TYPE_AUDIO, &audioindex_a, &audioindex_out)<0)
return -7;
printf("=====================Output Information=====================\n");
av_dump_format(ofmt_ctx, 0, out_filename, 1);
printf("============================================================\n");
if (!(ofmt->flags & AVFMT_NOFILE))
if (avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE) < 0)
return -8;
if (avformat_write_header(ofmt_ctx, NULL) < 0)
return -9;
return 0;
}
int headle_data(data_t *d, AVFormatContext *fmt_ctx, int idx, int idx_o, int64_t *pts) {
d->ifmt_ctx = fmt_ctx;
d->stream_index = idx_o;
if (av_read_frame(d->ifmt_ctx, &pkt) >= 0) {
do {
d->in_stream = d->ifmt_ctx->streams[pkt.stream_index];
d->out_stream = ofmt_ctx->streams[d->stream_index];
if (pkt.stream_index == idx) {
//FIX:No PTS (Example: Raw H.264)
//Simple Write PTS
if (pkt.pts == AV_NOPTS_VALUE) {
//Write PTS
AVRational time_base1 = d->in_stream->time_base;
//Duration between 2 frames (us)
int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(d->in_stream->r_frame_rate);
//Parameters
pkt.pts = (double)(frame_index*calc_duration) / (double)(av_q2d(time_base1)*AV_TIME_BASE);
pkt.dts = pkt.pts;
pkt.duration = (double)calc_duration / (double)(av_q2d(time_base1)*AV_TIME_BASE);
frame_index++;
}
*pts = pkt.pts;
break;
}
} while (av_read_frame(d->ifmt_ctx, &pkt) >= 0);
}
else {
return 1;
}
return 0;
}
int main(int argc, char* argv[]) {
int ret, i;
if ((ret = init_muxer()) < 0)
errReport("init_muxer", ret);
while (1) {
data_t dat;
//Get an AVPacket
if (av_compare_ts(cur_pts_v, ifmt_ctx_v->streams[videoindex_v]->time_base,
cur_pts_a, ifmt_ctx_a->streams[audioindex_a]->time_base) <= 0) {
if (headle_data(&dat, ifmt_ctx_v, videoindex_v, videoindex_out, &cur_pts_v) > 0)
break;
}
else {
if (headle_data(&dat, ifmt_ctx_a, audioindex_a, audioindex_out, &cur_pts_a) > 0)
break;
}
//Convert PTS/DTS
pkt.pts = av_rescale_q_rnd(pkt.pts, dat.in_stream->time_base, dat.out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, dat.in_stream->time_base, dat.out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q(pkt.duration, dat.in_stream->time_base, dat.out_stream->time_base);
pkt.pos = -1;
pkt.stream_index = dat.stream_index;
printf("#:%5d:%lld\t", pkt.size, pkt.pts);
if (av_interleaved_write_frame(ofmt_ctx, &pkt) < 0) {
printf("Error muxing packet\n");
break;
}
av_free_packet(&pkt);
}
av_write_trailer(ofmt_ctx);
avformat_close_input(&ifmt_ctx_v);
avformat_close_input(&ifmt_ctx_a);
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
avio_close(ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
if (ret < 0 && ret != AVERROR_EOF)
printf("Error occurred.\n");
else
printf("successed.\n");
getch();
return 0;
}
结果:
[aac @ 03aa7960] Estimating duration from bitrate, this may be inaccurate
======================Input Information=====================
Input #0, h264, from 'input.h264':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: h264 (High), yuv420p, 176x144, 25 fps, 25 tbr, 1200k tbn
, 50 tbc
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Input #0, aac, from 'input.aac':
Duration: 00:00:09.11, bitrate: 73 kb/s
Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 73 kb/s
============================================================
=====================Output Information=====================
Output #0, matroska, to 'output.mkv':
Stream #0:0: Video: h264 (High), yuv420p, 176x144, q=2-31, 50 tbc
Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp, 73 kb/s
============================================================
[matroska @ 031ca6e0] Using AVStream.codec.time_base as a timebase hint to the m
uxer is deprecated. Set AVStream.time_base instead.
[matroska @ 031ca6e0] Using AVStream.codec.time_base as a timebase hint to the m
uxer is deprecated. Set AVStream.time_base instead.
Ignoring attempt to set invalid timebase 1/0 for st:1
#: 5829:0 #: 2522:40 #: 140:0 #: 225:23 #: 198:46
#: 1038:80 #: 198:70 #: 180:93 #: 2824:120 #: 171:116
#: 182:139 #: 826:160 #: 185:163 #: 1088:200 #: 191:186
#: 195:209 #: 478:240 #: 203:232 #: 208:255 #: 1740:280
#: 190:279 #: 189:302 #: 1997:320 #: 199:325 #: 396:360
#: 193:348 #: 186:372 #: 2164:400 #: 209:395 #: 193:418
#: 406:440 #: 193:441 #: 5821:480 #: 181:464 #: 181:488
#: 1777:520 #: 207:511 #: 202:534 #: 505:560 #: 205:557
#: 198:580 #: 2253:600 #: 191:604 #: 742:640 #: 175:627
#: 200:650 #: 2752:680 #: 187:673 #: 215:697 #: 877:720
#: 201:720 #: 198:743 #: 2485:760 #: 182:766 #: 624:800
#: 202:789 #: 170:813 #: 2202:840 #: 191:836 #: 203:859
#: 576:880 #: 196:882 #: 867:920 #: 188:906 #: 189:929
#: 6511:960 #: 207:952 #: 194:975 #: 2339:1000 #: 180:998
#: 179:1022 #: 729:1040 #: 200:1045 #: 2720:1080 #: 202:1068
#: 186:1091 #: 775:1120 #: 193:1115 #: 188:1138 #: 2605:1160
#: 177:1161 #: 905:1200 #: 210:1184 #: 213:1207 #: 2614:1240
#: 209:1231 #: 195:1254 #: 986:1280 #: 171:1277 #: 187:1300
#: 2844:1320 #: 188:1324 #: 756:1360 #: 195:1347 #: 203:1370
#: 929:1400 #: 207:1393 #: 198:1416 #: 6083:1440 #: 198:1440
#: 194:1463 #: 2049:1480 #: 194:1486 #: 438:1520 #: 171:1509
#: 198:1533 #: 1985:1560 #: 192:1556 #: 180:1579 #: 384:1600
#: 220:1602 #: 1840:1640 #: 203:1625 #: 187:1649 #: 566:1680
#: 184:1672 #: 190:1695 #: 2677:1720 #: 174:1718 #: 201:1741
#: 897:1760 #: 193:1765 #: 3019:1800 #: 194:1788 #: 201:1811
#: 918:1840 #: 190:1834 #: 203:1858 #: 1270:1880 #: 204:1881
#: 5481:1920 #: 179:1904 #: 196:1927 #: 1703:1960 #: 197:1950
#: 198:1974 #: 2174:2000 #: 175:1997 #: 195:2020 #: 1918:2040
#: 198:2043 #: 2599:2080 #: 195:2067 #: 200:2090 #: 713:2120
#: 191:2113 #: 196:2136 #: 2497:2160 #: 191:2159 #: 190:2183
#: 682:2200 #: 203:2206 #: 1937:2240 #: 187:2229 #: 178:2252
#: 615:2280 #: 205:2276 #: 191:2299 #: 2158:2320 #: 174:2322
#: 857:2360 #: 206:2345 #: 222:2368 #: 5786:2400 #: 189:2392
#: 196:2415 #: 2610:2440 #: 186:2438 #: 192:2461 #: 683:2480
#: 182:2485 #: 2522:2520 #: 212:2508 #: 196:2531 #: 716:2560
#: 178:2554 #: 209:2577 #: 2342:2600 #: 192:2601 #: 616:2640
#: 178:2624 #: 201:2647 #: 2308:2680 #: 196:2670 #: 194:2694
#: 440:2720 #: 191:2717 #: 188:2740 #: 2330:2760 #: 192:2763
#: 505:2800 #: 200:2786 #: 196:2810 #: 879:2840 #: 188:2833
#: 192:2856 #: 6597:2880 #: 193:2879 #: 203:2902 #: 2145:2920
#: 192:2926 #: 608:2960 #: 199:2949 #: 188:2972 #: 2863:3000
#: 196:2995 #: 192:3019 #: 1012:3040 #: 187:3042 #: 2520:3080
#: 190:3065 #: 197:3088 #: 2894:3120 #: 196:3111 #: 184:3135
#: 678:3160 #: 196:3158 #: 193:3181 #: 2667:3200 #: 201:3204
#: 552:3240 #: 188:3228 #: 212:3251 #: 2580:3280 #: 185:3274
#: 176:3297 #: 525:3320 #: 194:3320 #: 7660:3360 #: 205:3344
#: 196:3367 #: 2045:3400 #: 176:3390 #: 198:3413 #: 562:3440
#: 203:3437 #: 186:3460 #: 2481:3480 #: 187:3483 #: 709:3520
#: 199:3506 #: 187:3529 #: 2538:3560 #: 192:3553 #: 199:3576
#: 592:3600 #: 201:3599 #: 191:3622 #: 2396:3640 #: 193:3646
#: 547:3680 #: 195:3669 #: 205:3692 #: 2329:3720 #: 200:3715
#: 189:3738 #: 534:3760 #: 189:3762 #: 958:3800 #: 197:3785
#: 199:3808 #: 6396:3840 #: 197:3831 #: 184:3855 #: 2378:3880
#: 193:3878 #: 192:3901 #: 619:3920 #: 182:3924 #: 2096:3960
#: 202:3947 #: 182:3971 #: 727:4000 #: 198:3994 #: 203:4017
#: 2046:4040 #: 199:4040 #: 2221:4080 #: 191:4063 #: 172:4087
#: 356:4120 #: 199:4110 #: 189:4133 #: 2083:4160 #: 205:4156
#: 190:4180 #: 454:4200 #: 204:4203 #: 2268:4240 #: 196:4226
#: 186:4249 #: 413:4280 #: 175:4272 #: 207:4296 #: 6571:4320
#: 200:4319 #: 196:4342 #: 1777:4360 #: 197:4365 #: 397:4400
#: 179:4389 #: 196:4412 #: 1930:4440 #: 195:4435 #: 195:4458
#: 2143:4480 #: 194:4481 #: 2689:4520 #: 179:4505 #: 197:4528
#: 2378:4560 #: 198:4551 #: 203:4574 #: 491:4600 #: 194:4598
#: 195:4621 #: 2496:4640 #: 180:4644 #: 493:4680 #: 203:4667
#: 182:4690 #: 2378:4720 #: 199:4714 #: 195:4737 #: 551:4760
#: 179:4760 #: 6641:4800 #: 193:4783 #: 197:4807 #: 1853:4840
#: 191:4830 #: 190:4853 #: 2540:4880 #: 201:4876 #: 196:4899
#: 622:4920 #: 201:4923 #: 2098:4960 #: 198:4946 #: 192:4969
#: 509:5000 #: 199:4992 #: 207:5016 #: 2136:5040 #: 191:5039
#: 182:5062 #: 487:5080 #: 194:5085 #: 1740:5120 #: 192:5108
#: 172:5132 #: 1744:5160 #: 194:5155 #: 199:5178 #: 2137:5200
#: 195:5201 #: 739:5240 #: 199:5224 #: 184:5248 #: 6329:5280
#: 195:5271 #: 201:5294 #: 2083:5320 #: 200:5317 #: 198:5341
#: 672:5360 #: 192:5364 #: 2529:5400 #: 174:5387 #: 197:5410
#: 737:5440 #: 195:5433 #: 202:5457 #: 2429:5480 #: 190:5480
#: 195:5503 #: 649:5520 #: 190:5526 #: 1690:5560 #: 196:5550
#: 192:5573 #: 1784:5600 #: 191:5596 #: 195:5619 #: 1704:5640
#: 193:5642 #: 1996:5680 #: 196:5666 #: 191:5689 #: 802:5720
#: 198:5712 #: 192:5735 #: 5532:5760 #: 191:5759 #: 187:5782
#: 1481:5800 #: 191:5805 #: 2505:5840 #: 190:5828 #: 196:5851
#: 920:5880 #: 176:5875 #: 212:5898 #: 2146:5920 #: 199:5921
#: 2557:5960 #: 211:5944 #: 189:5968 #: 1120:6000 #: 184:5991
#: 212:6014 #: 2580:6040 #: 192:6037 #: 190:6060 #: 2046:6080
#: 192:6084 #: 2364:6120 #: 196:6107 #: 199:6130 #: 2439:6160
#: 189:6153 #: 192:6177 #: 1956:6200 #: 193:6200 #: 183:6223
#: 6207:6240 #: 197:6246 #: 2161:6280 #: 186:6269 #: 207:6293
#: 805:6320 #: 196:6316 #: 193:6339 #: 2164:6360 #: 187:6362
#: 2546:6400 #: 203:6385 #: 192:6409 #: 3750:6440 #: 184:6432
#: 193:6455 #: 1051:6480 #: 198:6478 #: 198:6502 #: 3174:6520
#: 173:6525 #: 889:6560 #: 208:6548 #: 193:6571 #: 2989:6600
#: 190:6594 #: 198:6618 #: 981:6640 #: 192:6641 #: 1372:6680
#: 192:6664 #: 185:6687 #: 6644:6720 #: 198:6711 #: 198:6734
#: 2401:6760 #: 185:6757 #: 190:6780 #: 3512:6800 #: 193:6803
#: 1304:6840 #: 198:6827 #: 197:6850 #: 2518:6880 #: 200:6873
#: 199:6896 #: 2783:6920 #: 188:6920 #: 190:6943 #: 908:6960
#: 189:6966 #: 3130:7000 #: 189:6989 #: 189:7012 #: 1090:7040
#: 198:7036 #: 197:7059 #: 3256:7080 #: 190:7082 #: 1139:7120
#: 196:7105 #: 213:7129 #: 1315:7160 #: 166:7152 #: 204:7175
#: 6379:7200 #: 197:7198 #: 200:7221 #: 2744:7240 #: 186:7245
#: 1050:7280 #: 196:7268 #: 192:7291 #: 2528:7320 #: 197:7314
#: 188:7338 #: 848:7360 #: 191:7361 #: 2621:7400 #: 172:7384
#: 210:7407 #: 783:7440 #: 199:7430 #: 207:7454 #: 2294:7480
#: 191:7477 #: 191:7500 #: 2304:7520 #: 185:7523 #: 2225:7560
#: 190:7546 #: 198:7570 #: 2240:7600 #: 195:7593 #: 195:7616
#: 2274:7640 #: 196:7639 #: 192:7663 #: 6405:7680 #: 196:7686
#: 1987:7720 #: 190:7709 #: 192:7732 #: 2628:7760 #: 202:7755
#: 196:7779 #: 577:7800 #: 192:7802 #: 2392:7840 #: 197:7825
#: 188:7848 #: 412:7880 #: 184:7872 #: 186:7895 #: 1904:7920
#: 197:7918 #: 195:7941 #: 455:7960 #: 198:7964 #: 2116:8000
#: 192:7988 #: 197:8011 #: 2556:8040 #: 189:8034 #: 196:8057
#: 731:8080 #: 197:8081 #: 1184:8120 #: 196:8104 #: 177:8127
#: 7245:8160 #: 196:8150 #: 200:8173 #: 2296:8200 #: 197:8197
#: 195:8220 #: 868:8240 #: 182:8243 #: 2533:8280 #: 175:8266
#: 199:8290 #: 985:8320 #: 202:8313 #: 197:8336 #: 2534:8360
#: 196:8359 #: 169:8382 #: 942:8400 #: 203:8406 #: 2508:8440
#: 197:8429 #: 202:8452 #: 835:8480 #: 198:8475 #: 177:8499
#: 2499:8520 #: 194:8522 #: 820:8560 #: 201:8545 #: 197:8568
#: 1073:8600 #: 196:8591 #: 184:8615 #: 6810:8640 #: 176:8638
#: 199:8661 #: 2340:8680 #: 205:8684 #: 763:8720 #: 195:8707
#: 189:8731 #: 2138:8760 #: 204:8754 #: 191:8777 #: 736:8800
#: 195:8800 #: 2243:8840 #: 203:8824 #: 185:8847 #: 676:8880
#: 196:8870 #: 197:8893 #: 2148:8920 #: 189:8916 #: 200:8940
#: 680:8960 #: 196:8963 #: 1757:9000 #: 199:8986 #: 192:9009
#: 2077:9040 #: 183:9033 #: 197:9056 #: 2174:9080 #: 187:9079
#: 189:9102 #: 6016:9120 #: 195:9125 #: 1203:9160 #: 201:9149
#: 198:9172 #: 1463:9200 #: 185:9195 #: 188:9218 #: 2474:9240
#: 195:9242 #: 790:9280 #: 192:9265 #: 196:9288 #: 2549:9320
#: 194:9311 #: 191:9334 #: 627:9360 #: 192:9358 #: 195:9381
#: 2141:9400 #: 192:9404 #: 505:9440 #: 196:9427 #: 195:9451
#: 2197:9480 #: 194:9474 #: 197:9497 #: 489:9520 #: 193:9520
#: 1003:9560 #: 185:9543 #: 163:9567 #: 5942:9600 #: 203:9590
#: 208:9613 #: 1444:9640 #: 196:9636 #: 198:9660 #: 1569:9680
#: 199:9683 #: 2115:9720 #: 198:9706 #: 193:9729 #: 1003:9760
#: 188:9752 #: 194:9776 #: 2163:9800 #: 193:9799 #: 199:9822
#: 826:9840 #: 203:9845 #: 1886:9880 #: 198:9868 #: 195:9892
#: 907:9920 #: 190:9915 #: 197:9938 #: 1402:9960 #: 191:9961
successed.