有朋友問到ffmpeg播放文件如何定位問題,我想到應該還有一些新手朋友對這一塊比較陌生。ffmpeg定位問題用到seek方法,代碼
如下:
void SeekFrame(AVFormatContext* context, int64_t seekFrame)//跳轉到指定位置 { int defaultStreamIndex = av_find_default_stream_index(context); if(ist->index == defaultStreamIndex) { auto time_base = context->streams[defaultStreamIndex]->time_base; auto seekTime = context->streams[defaultStreamIndex]->start_time + av_rescale(seekFrame, time_base.den, time_base.num); int ret ; if(seekTime > ist->cur_dts) { ret = av_seek_frame(context, defaultStreamIndex, seekTime, AVSEEK_FLAG_ANY); } else { ret = av_seek_frame(context, defaultStreamIndex, seekTime, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD); } } }
需要注意的是,調用seek方法以后,packet的時間戳一定要重新計算,ffmpeg要求時間戳遞增,可以用一個成員變量記住最后一個時間戳信息,依次累加。
如需交流 請加入流媒體/Ffmpeg/音視頻 127903734
視頻下載地址:http://www.chungen90.com/?news_3/
Demo下載地址: http://www.chungen90.com/?news_2
