ffmplay---seek实现播放进度控制

本文详细介绍了如何使用FFmpeg库实现播放进度控制,包括停止线程、清除队列、恢复线程、精准seek操作等步骤,以及涉及的关键函数av_seek_file和av_seek_frame。针对不同类型的媒体文件,如ts和mp4,seek操作存在差异,需考虑时间单位、时间大小和操作基准等因素。最后展示了按视频和音频流进行seek的具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0、实现播放进度控制—即实现随机访问流媒体文件

seek流程:
1、停下demux线程、音视频解码线程
2、清空pkt队列
3、恢复三个线程(此时还不能进入正常播放状态)
4、扔掉小于seek的目标pts的帧
5、直到拿到帧的pts≥seek目标pts,才恢复音视频渲染
6、恢复正常播放

ffplay的seek操作:对于ts和mp4的逻辑差异
mp4 可以seek到指定的时间戳
ts 是 seek到文件的某个position,而不能直接seek到指定的时间点

1、seek操作要点

1、操作基准:即seek操作当前时间点,因此在进行播放时需要实时记录音视频流的DTS或者PTS
2、时间单位:进行seek操作时,根据流索引,确定其时间单位time_base,
3、时间大小:即向前或者向后seek的时间大小,根据此时间大小和当前时间点,可以计算出向前或者向后进行seek操作时的目标时间
4、操作载体:即是按照音频还是视频进行seek操作,只需要按照其中一个流进行seek即可,不需要分别对音视频流进行seek操作
5、操作刷新:在进行seek操作之后,正常播放之前需要将编解码的内部缓冲区进行刷新,同时也要将自行控制的缓冲区内缓存的音视频清零

2、seek相关函数

avformat_seek_file(会调用av_seek_frame)

/**
 * Seek to timestamp ts.
 * Seeking will be done so that the point from which all active streams
 * can be presented successfully will be closest to ts and within min/max_ts.
 * Active streams are all streams that have AVStream.discard < AVDISCARD_ALL.
 *
 * If flags contain AVSEEK_FLAG_BYTE, then all timestamps are in bytes and
 * are the file position (this may not be supported by all demuxers).
 * If flags contain AVSEEK_FLAG_FRAME, then all timestamps are in frames
 * in the stream with stream_index (this may not be supported by all demuxers).
 * Otherwise all timestamps are in units of the stream selected by stream_index
 * or if stream_index is -1, in AV_TIME_BASE units.
 * If flags contain AVSEEK_FLAG_ANY, then non-keyframes are treated as
 * keyframes (this may not be supported by all demuxers).
 * If flags contain AVSEEK_FLAG_BACKWARD, it is ignored.
 *
 * @param s media file handle
 * @param stream_index index of the stream which is used as time base reference
 * @param min_ts smallest acceptable timestamp
 * @param ts target timestamp
 * @param max_ts largest acceptable timestamp
 * @param flags flags
 * @return >=0 on success, error code otherwise
 *
 * @note This is part of the new seek API which is still under construction.
 *       Thus do not use this yet. It may change at any time, do not
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值