如何在 FFmpeg C/C++ 中查找
有谁知道如何在 FFmpeg 中实现按秒(或毫秒)查找.我目前有一个使用 av_read_frame() 遍历视频帧的循环,我想以秒为单位确定该帧应该在什么时间.如果它到达某个点,那么我想在视频中寻找稍后的点.顺便说一下,它不是视频播放器,只是处理帧.我听说我应该能够从数据包中获取 dts 或 pts,但它总是返回 0.
Does anyone know how to implement seeking by seconds (or milliseconds) in FFmpeg. I currently have a loop running through the frames of a video using av_read_frame() and I want to determine what time this frame should be at in seconds. If it gets to a certain point then I want to seek to a later point in the video. By the way it is not a video player, just processing the frames. Ive heard I should be able to get the dts or pts from the packet but its always returning 0.
推荐答案
注意:这是过时的,它应该仍然有效,但现在有 av_seek_frame()
正式完成.
NOTE: This is out of date, it should still work but there is now av_seek_frame()
to do it officially.
这不是我写的,但这里有一些来自我的样本的代码
I didn't write this but here is some code from a sample I have
bool seekMs(int tsms)
{
//printf("**** SEEK TO ms %d. LLT: %d. LT: %d. LLF: %d. LF: %d. LastFrameOk: %d
",tsms,LastLastFrameTime,LastFrameTime,LastLastFrameNumber,LastFrameNumber,(int)LastFrameOk);
// Convert time into frame number
DesiredFrameNumber = ffmpeg::av_rescale(tsms,pFormatCtx->streams[videoStream]->time_base.den,pFormatCtx->streams[videoStream]->time_base.num);
DesiredFrameNumber/=1000;
return seekFrame(DesiredFrameNumber);
}
bool seekFrame(ffmpeg::int64_t frame)
{
//printf("**** seekFrame to %d. LLT: %d. LT: %d. LLF: %d. LF: %d. LastFrameOk: %d
",(int)frame,LastLastFrameTime,LastFrameTime,LastLastFrameNumber,LastFrameNumber,(int)LastFrameOk);
// Seek if:
// - we don't know where we are (Ok=false)
// - we know where we are but:
// - the desired frame is after the last decoded frame (this could be optimized: if the distance is small, calling decodeSeekFrame may be faster than seeking from the last key frame)
// - the desired frame is smaller or equal than the previous to the last decoded frame. Equal because if frame==LastLastFrameNumber we don't want the LastFrame, but the one before->we need to seek there
if( (LastFrameOk==false) || ((LastFrameOk==true) && (frame<=LastLastFrameNumber || frame>LastFrameNumber) ) )
{
//printf(" avformat_seek_file
");
if(ffmpeg::avformat_seek_file(pFormatCtx,videoStream,0,frame,frame,AVSEEK_FLAG_FRAME)<0)
return false;
avcodec_flush_buffers(pCodecCtx);
DesiredFrameNumber = frame;
LastFrameOk=false;
}
//printf(" decodeSeekFrame
");
return decodeSeekFrame(frame);
return true;
}
相关文章