ffmpeg + glfwGetTime()

ffmpeg + glfwGetTime()

本文关键字:glfwGetTime ffmpeg      更新时间:2023-10-16

我尝试使用ffmpeg+opengl来控制视频的播放速度。但我有问题。

我的视频以每秒25帧的速度编码,播放速度非常快。我在代码中添加了这个。

tiempo = glfwGetTime();
duracion = 1.0/25.0; // 1 second / 25 fps
while(1){
...        
  if(glfwGetTime() > tiempo + duracion){
    if(av_read_frame(pFormatCtx,&packet) >= 0){
      if(packet.stream_index == 0){
        avcodec_decode_video2(pCodecCtx,pFrame,&frameFin,&packet);
        if(frameFin)sws_scale(img_convert_ctx,pFrame->data,pFrame->linesize,0,pCodecCtx->height,pFrameRGB->data,pFrameRGB->linesize);
      }
      av_free_packet(&packet);
    }
    tiempo = glfwGetTime();
  }
...
}

问题是现在视频播放速度比它应该播放的要慢。问题出在哪里?

您将解码图像所需的时间添加到了显示下一个图像所需等待的40ms。此错误是因为您在循环结束时再次测量时间。

而不是:

    }
    tiempo = glfwGetTime();
  }

写入:

   }
   tiempo+=duraction;
}

我认为这是错误的:if(glfwGetTime()>tiempo+duracion){//。。。tiempo=glfwGetTime();

因为对glfwGetTime的第一个调用和第二个调用不相同->您应该将其缓存在一个变量中并与之进行比较。