libavformat: av_interleaved_write_frame - не может обрабатывать данные без чередования
Я пытаюсь запустить пример remuxing.c из кода примера ffmpeg. Я использую его для повторного смешивания файла TS, содержащего h264, aac в формат FLV.
Пакеты TS, которые подаются, неправильно перемежаются. Так что я использую [av_interleaved_write_frame][1]
функция для мультиплексирования. Даже тогда я получаю следующую ошибку.
[flv @ 0x7feab581d400] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 2464 >= 2379
Я использую API неправильно? Любая помощь будет принята с благодарностью.
Вот фрагмент журнала:
in: pts:209934 pts_time:2.3326 dts:209934 dts_time:2.3326 duration:2089 duration_time:0.0232111 stream_index:1
out: pts:2333 pts_time:2.333 dts:2333 dts_time:2.333 duration:23 duration_time:0.023 stream_index:1
in: pts:212023 pts_time:2.35581 dts:212023 dts_time:2.35581 duration:2089 duration_time:0.0232111 stream_index:1
out: pts:2356 pts_time:2.356 dts:2356 dts_time:2.356 duration:23 duration_time:0.023 stream_index:1
in: pts:218790 pts_time:2.431 dts:215787 dts_time:2.39763 duration:3003 duration_time:0.0333667 stream_index:0
out: pts:2431 pts_time:2.431 dts:2398 dts_time:2.398 duration:33 duration_time:0.033 stream_index:0
in: pts:221793 pts_time:2.46437 dts:218790 dts_time:2.431 duration:3003 duration_time:0.0333667 stream_index:0
out: pts:2464 pts_time:2.464 dts:2431 dts_time:2.431 duration:33 duration_time:0.033 stream_index:0
in: pts:224796 pts_time:2.49773 dts:221793 dts_time:2.46437 duration:3003 duration_time:0.0333667 stream_index:0
out: pts:2498 pts_time:2.498 dts:2464 dts_time:2.464 duration:33 duration_time:0.033 stream_index:0
in: pts:214108 pts_time:2.37898 dts:214108 dts_time:2.37898 duration:2089 duration_time:0.0232111 stream_index:1
out: pts:2379 pts_time:2.379 dts:2379 dts_time:2.379 duration:23 duration_time:0.023 stream_index:1
[flv @ 0x7fb9de014800] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 2464 >= 2379
in: pts:216198 pts_time:2.4022 dts:216198 dts_time:2.4022 duration:2089 duration_time:0.0232111 stream_index:1
out: pts:2402 pts_time:2.402 dts:2402 dts_time:2.402 duration:23 duration_time:0.023 stream_index:1
[flv @ 0x7fb9de014800] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 2464 >= 2402
in: pts:218288 pts_time:2.42542 dts:218288 dts_time:2.42542 duration:2089 duration_time:0.0232111 stream_index:1
out: pts:2425 pts_time:2.425 dts:2425 dts_time:2.425 duration:23 duration_time:0.023 stream_index:1
[flv @ 0x7fb9de014800] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 2464 >= 2425
in: pts:220378 pts_time:2.44864 dts:220378 dts_time:2.44864 duration:2089 duration_time:0.0232111 stream_index:1
out: pts:2449 pts_time:2.449 dts:2449 dts_time:2.449 duration:23 duration_time:0.023 stream_index:1
[flv @ 0x7fb9de014800] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 2464 >= 2449
in: pts:222467 pts_time:2.47186 dts:222467 dts_time:2.47186 duration:2089 duration_time:0.0232111 stream_index:1
out: pts:2472 pts_time:2.472 dts:2472 dts_time:2.472 duration:23 duration_time:0.023 stream_index:1
in: pts:224557 pts_time:2.49508 dts:224557 dts_time:2.49508 duration:2089 duration_time:0.0232111 stream_index:1
out: pts:2495 pts_time:2.495 dts:2495 dts_time:2.495 duration:23 duration_time:0.023 stream_index:1
in: pts:226647 pts_time:2.5183 dts:226647 dts_time:2.5183 duration:2089 duration_time:0.0232111 stream_index:1
out: pts:2518 pts_time:2.518 dts:2518 dts_time:2.518 duration:23 duration_time:0.023 stream_index:1
in: pts:227799 pts_time:2.5311 dts:224796 dts_time:2.49773 duration:3003 duration_time:0.0333667 stream_index:0
out: pts:2531 pts_time:2.531 dts:2498 dts_time:2.498 duration:33 duration_time:0.033 stream_index:0
[flv @ 0x7fb9de014800] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 2518 >= 2498
Error muxing packet
Error occurred: Invalid argument
Ниже приведен код, который я использую для ремуксинга:
AVBitStreamFilterContext *bsfc = av_bitstream_filter_init("aac_adtstoasc");
if (!bsfc) {
printf("Error creating adtstoasc filter\n");
goto end;
}
while (1) {
AVStream *in_stream, *out_stream;
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0) break;
in_stream = ifmt_ctx->streams[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
log_packet(ifmt_ctx, &pkt, "in");
/* copy packet */
pkt.pts =
av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base,
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
pkt.dts =
av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base,
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
pkt.duration =
av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
log_packet(ofmt_ctx, &pkt, "out");
if (out_stream->codec->codec_id == AV_CODEC_ID_AAC) {
AVPacket newpacket = {0};
int rc = av_bitstream_filter_filter(
bsfc, out_stream->codec, NULL, &newpacket.data, &newpacket.size,
pkt.data, pkt.size, pkt.flags & AV_PKT_FLAG_KEY);
if (rc >= 0) {
/*printf("Filter aac success\n");*/
newpacket.pts = pkt.pts;
newpacket.dts = pkt.dts;
newpacket.duration = pkt.duration;
newpacket.pos = -1;
/*printf("calling av_interleaved_write_frame for audio:%d\n",
pkt.stream_index);*/
ret = av_interleaved_write_frame(ofmt_ctx, &newpacket);
/*printf("av_interleaved_write_frame success\n");*/
av_free_packet(&newpacket);
} else {
printf("Error filtering aac packet\n");
break;
}
} else {
ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
if (ret < 0) {
fprintf(stderr, "Error muxing packet\n");
break;
}
}
av_packet_unref(&pkt);
}
av_write_trailer(ofmt_ctx)