1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| AVFormatContext *ifmtCtx = NULL; AVStream *inStream, *outStream; int streamIndex = 0;
if (av_compare_ts(curPstVideo, ifmtCtxVideo->streams[inVideoIndex]->time_base, curPstAudio, ifmtCtxAudio->streams[inAudioIndex]->time_base) < 0) { ifmtCtx = ifmtCtxVideo; streamIndex = outVideoIndex;
if (av_read_frame(ifmtCtx, &packet) >= 0) { do { inStream = ifmtCtx->streams[packet.stream_index]; outStream = ofmtCtx->streams[streamIndex];
if (packet.stream_index == inVideoIndex) { if (packet.pts == AV_NOPTS_VALUE) { AVRational timeBase1 = inStream->time_base; int64_t calcDuration = (double)AV_TIME_BASE/av_q2d(inStream->r_frame_rate); packet.pts = (double)(frameIndex*calcDuration)/(double)(av_q2d(timeBase1)*AV_TIME_BASE); packet.dts = packet.pts; packet.duration = (double)calcDuration/(double)(av_q2d(timeBase1)*AV_TIME_BASE); frameIndex++; }
curPstVideo = packet.pts; break; } }while(av_read_frame(ifmtCtx, &packet) >= 0); } else { break; } }else{ ifmtCtx = ifmtCtxAudio; streamIndex = outAudioIndex;
if (av_read_frame(ifmtCtx, &packet) >= 0) { do { inStream = ifmtCtx->streams[packet.stream_index]; outStream = ofmtCtx->streams[streamIndex];
if (packet.stream_index == inAudioIndex) { if (packet.pts == AV_NOPTS_VALUE) { AVRational timeBase1 = inStream->time_base; int64_t calcDuration = (double)AV_TIME_BASE/av_q2d(inStream->r_frame_rate); packet.pts = (double)(frameIndex*calcDuration)/(double)(av_q2d(timeBase1)*AV_TIME_BASE); packet.dts = packet.pts; packet.duration = (double)calcDuration/(double)(av_q2d(timeBase1)*AV_TIME_BASE); frameIndex++; }
curPstAudio = packet.pts; break; } }while(av_read_frame(ifmtCtx, &packet) >= 0); } else { break; } }
#if USE_H264BSF av_bitstream_filter_filter(h264bsfc, inStream->codec, NULL, &packet.data, &packet.size, packet.data, packet.size, 0); #endif #if USE_AACBSF av_bitstream_filter_filter(aacbsfc, outStream->codec, NULL, &packet.data, &packet.size, packet.data, packet.size, 0); #endif
packet.pts = av_rescale_q_rnd(packet.pts, inStream->time_base, outStream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX)); packet.dts = av_rescale_q_rnd(packet.dts, inStream->time_base, outStream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX)); packet.duration = av_rescale_q(packet.duration, inStream->time_base, outStream->time_base); packet.pos = -1; packet.stream_index = streamIndex;
if (av_interleaved_write_frame(ofmtCtx, &packet) < 0) { printf("error muxing packet"); break; }
av_packet_unref(&packet);
|