14

该应用程序将相机输出保存到 mov 中。文件,然后将其转换为由 AVPacket 发送到 rtmp 服务器的 flv 格式。它每次在两个文件之间切换,一个由相机输出写入,另一个被发送。我的问题是一段时间后音频/视频不同步。

发送的第一个缓冲区始终是 100% 同步的,但一段时间后它会变得混乱。我相信它是一个 DTS-PTS 问题..

 if(isVideo)
{
    packet->stream_index = VIDEO_STREAM;
   packet->dts = packet->pts = videoPosition;
    videoPosition += packet->duration = FLV_TIMEBASE * packet->duration * videoCodec->ticks_per_frame * videoCodec->time_base.num / videoCodec->time_base.den;

}
else
{
    packet->stream_index = AUDIO_STREAM;
    packet->dts = packet->pts = audioPosition;
    audioPosition += packet->duration = FLV_TIMEBASE * packet->duration / audioRate;

   //NSLog(@"audio position = %lld", audioPosition);
}

packet->pos = -1;
packet->convergence_duration = AV_NOPTS_VALUE;

// This sometimes fails without being a critical error, so no exception is raised
if((code = av_interleaved_write_frame(file, packet)))
{
    NSLog(@"Streamer::Couldn't write frame");
}
av_free_packet(packet);
4

2 回答 2

0

您可以研究此示例:http ://unick-soft.ru/art/files/ffmpegEncoder-vs2008.zip

但此示例适用于 Windows。

在此示例中,我仅将 pts 用于音频流:

  if (pVideoCodec->coded_frame->pts != AV_NOPTS_VALUE)
  {
    pkt.pts = av_rescale_q(pVideoCodec->coded_frame->pts, 
      pVideoCodec->time_base, pVideoStream->time_base);
  }
于 2013-01-10T22:55:03.527 回答
0

我在切换 AVAssetWriters 时遇到了类似的问题,并注意到如果我在获得视频样本时才开始使用新的 AVAssetWriter

https://medium.com/@brandon.kobel/ios-seamless-video-chunks-4383a5a3a874

于 2016-07-28T19:58:54.887 回答