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);