如何寻求FFmpeg的C / C ++ [英] How to seek in 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
我没有写这一点,但这里是从样品中我有一些code
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\n",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\n",(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("\t avformat_seek_file\n");
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("\t decodeSeekFrame\n");
return decodeSeekFrame(frame);
return true;
}
这篇关于如何寻求FFmpeg的C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!