C libavformat:av_交错_写入_帧-无法处理非交错数据

C libavformat:av_交错_写入_帧-无法处理非交错数据,c,ffmpeg,libav,libavformat,C,Ffmpeg,Libav,Libavformat,我正在尝试从ffmpeg示例代码运行remuxing.c示例。我正在使用它将包含h264、aac的TS文件还原为FLV格式 正在馈送的TS数据包未正确交错。因此,我正在使用[av\u interleave\u write\u frame][1]函数进行muxing。即使这样,我还是得到了以下错误 [flv @ 0x7feab581d400] Application provided invalid, non monotonically increasing dts to muxer in str

我正在尝试从ffmpeg示例代码运行remuxing.c示例。我正在使用它将包含h264、aac的TS文件还原为FLV格式

正在馈送的TS数据包未正确交错。因此,我正在使用
[av\u interleave\u write\u frame][1]
函数进行muxing。即使这样,我还是得到了以下错误

[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
以下是我用于remuxing的代码:

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)
AVBitStreamFilterContext*bsfc=av_bitstream_filter_init(“aac_adtstoasc”);
如果(!bsfc){
printf(“创建adtstoasc筛选器时出错\n”);
转到终点;
}
而(1){
AVStream*输入流,*输出流;
ret=av_读取_帧(ifmt_ctx和pkt);
如果(ret<0)断裂;
in_stream=ifmt_ctx->streams[pkt.stream_index];
out_stream=ofmt_ctx->streams[pkt.stream_index];
日志数据包(ifmt\U ctx和pkt,“in”);
/*复制包*/
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);
包装期限=
av_rescale_q(打包持续时间、入流->时基、出流->时基);
pkt.pos=-1;
日志数据包(ofmt\U ctx和pkt,“退出”);
如果(输出流->编解码器->编解码器id==AV\U编解码器id\U AAC){
AVPacket newpacket={0};
int rc=av_比特流_过滤器_过滤器(
bsfc,out_stream->codec,NULL,&newpacket.data,&newpacket.size,
pkt.data、pkt.size、pkt.flags和AV_pkt_FLAG_KEY);
如果(rc>=0){
/*printf(“过滤aac成功\n”)*/
newpacket.pts=pkt.pts;
newpacket.dts=pkt.dts;
newpacket.duration=pkt.duration;
newpacket.pos=-1;
/*printf(“调用音频的av\u交错\u写入\u帧:%d\n”,
数据包(流索引)*/
ret=av交织写入帧(ofmt、ctx和新数据包);
/*printf(“av交织写入帧成功”)*/
av_免费_数据包(&newpacket);
}否则{
printf(“错误过滤aac数据包\n”);
打破
}
}否则{
ret=av交织写入帧(ofmt、ctx和pkt);
如果(ret<0){
fprintf(stderr,“错误多路复用数据包\n”);
打破
}
}
av_数据包_unref(&pkt);
}
av_写入_预告片(ofmt_ctx)