zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

【Android FFMPEG 开发】FFMPEG 视频播放进度控制 ( 显示播放进度 | 拖动进度条播放 )

Android控制开发 视频 显示 播放 FFMPEG 拖动
2023-06-13 09:17:42 时间

文章目录

I . FFMPEG 播放进度控制


FFMPEG 播放进度控制 : 为 FFMPEG 播放视频添加拖动进度条功能 , 主要包含以下两个功能 ;

第一 , 进度更新 , 视频播放过程中 , 播放的同时更新当前的播放进度 , 界面中的进度条实时显示当前的播放进度 ;

第二 , 进度控制 , 拖动进度条 , 控制视频播放进度跳转 ;

进度控制前提 : 上述功能主要用于 视频播放 , 只有完整的视频才能添加进度控制功能 , 直播视频流是无法添加进度功能的 ;

II . FFMPEG 播放视频 ( 效果展示 )


GitHub 项目地址 : han1202012 / 011_FFMPEG

直播功能 : 之前使用 FFMPEG 开发直播流播放功能 , 播放的是网络上的 RTPM 直播流 , 当时使用的是 avformat_open_input 方法 , 将下面的视频流地址传递到该方法中 , 即可播放网络视频流 ;

播放湖南卫视直播流 : rtmp://58.200.131.2:1935/livetv/hunantv

本次在直播功能的基础上 , 添加了本地文件播放功能 , 进度控制主要在本地视频文件播放功能上进行 ;

视频文件播放功能 : 将本地 SD 卡中的视频地址传入到上述 avformat_open_input 方法中 , 即可播放手机本地的视频文件 ;

播放手机本地文件 : /sdcard/game.mp4 , 本文件放在了 GitHub 源码的 Assets 目录中 , 将其拷贝到 SD 卡根目录即可在本程序中播放 ;

III . FFMPEG 获取视频时长


1 . 视频时长信息 : FFMPEG 的音频时长封装在 AVFormatContext 结构体中 , 只要 AVFormatContext 初始化成功 , 就可以获取该结构体中的视频时长 ;

2 . AVFormatContext 结构体 : 该结构体中封装了 音频 视频相关信息 , 包括音频的采样率 , 采样位数等属性 , 视频的宽高 , 编解码信息 , 音视频时长 等信息 ;

3 . FFMPEG 获取视频时长流程 :

① 打开视频文件 : 使用 avformat_open_input 方法 , 打开视频文件 , 将视频文件地址传入该方法中 ;

// 打开音视频地址 ( 播放文件前 , 需要先将文件打开 )  
// 地址类型 : ① 文件类型 , ② 音视频流
int open_result = avformat_open_input(&formatContext, dataSource, 0, 0);

② 查找媒体流 : 调用 avformat_find_stream_info 方法 , 查找打开文件的媒体流信息 ;

//2 . 查找媒体 地址 对应的音视频流 ( 给 AVFormatContext* 成员赋值 )
//      方法原型 : int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);
//      调用该方法后 , AVFormatContext 结构体的 nb_streams 元素就有值了 ,
//      该值代表了音视频流 AVStream 个数
int find_result = avformat_find_stream_info(formatContext, 0);

③ 获取视频时长 : 视频时长就封装在 AVFormatContext *formatContext 编解码上下文环境结构体的 duration 结构体成员中 ;

//获取视频时长, 单位是微秒 , 除以 1000000 是秒时间
duration = formatContext->duration / 1000000;

④ duration 视频时长原型 : 下面是封装在 AVFormatContext 结构体中的 duration 原型 ; 这是音视频流的时长 , 其单位是 微秒 , 一般不需要手动设置该值 , 该值是从音视频文件中解析出来的 ;

typedef struct AVFormatContext {
		...
		/**
		 * Duration of the stream, in AV_TIME_BASE fractional
		 * seconds. Only set this value if you know none of the individual stream
		 * durations and also do not set any of them. This is deduced from the
		 * AVStream values if not set.
		 *
		 * Demuxing only, set by libavformat.
		 */
		int64_t duration;
		...
}

IV . FFMPEG 视频播放进度获取


1 . 视频播放进度 : 之前已经获取了视频的时长 , 即 AVFormatContext 中提取的 duration 元素值 , 是视频的总时长微秒数 , 这里获取到当前的播放时间 , 就可以得到当前时刻的播放进度百分比 ;

2 . 主要问题 : 那么问题就集中在了 如何获取当前的播放时间 , 当前的播放时间可以从 AVFrame 音视频帧中获取 ;

3 . 获取当前播放时间流程 :

① 获取 AVFrame 结构体 : 这是解码后的音视频数据帧 , 从音视频流中读取出来的是 AVPacket 数据包 , 使用编解码器将 AVPacket 压缩数据包 解码成 AVFrame 实际的数据帧 , 其中的 音频 / 视频 是解码后的 采样 或 图像 数据 , 可以用于直接播放 ;

② 从 AVFrame 中获取当前的相对播放时间 : AVFrame 结构体中封装的 best_effort_timestamp 元素值 , 就是当前 画面 或 采样 的相对播放时间 , 注意其单位是 AVRational ;

③ 时间单位转换 : best_effort_timestamp 的时间单位是 AVRational , 这里需要将其 转为 秒 , av_q2d 方法可以将 AVRational 时间单位转为秒单位 ;

//获取当前画面的相对播放时间 , 相对 : 即从播放开始到现在的时间
//  该值大多数情况下 , 与 pts 值是相同的
//  该值比 pts 更加精准 , 参考了更多的信息
//  转换成秒 : 这里要注意 pts 需要转成 秒 , 需要乘以 time_base 时间单位
//  其中 av_q2d 是将 AVRational 转为 double 类型
double vedio_best_effort_timestamp_second = avFrame->best_effort_timestamp * av_q2d(time_base);

④ AVFrame 结构体中的 best_effort_timestamp 元素原型 : 这是结合各种因素估算出来的当前帧应该播放的时间 , 其单位是 time base , 即 AVRational 类型 ;

/**
 * frame timestamp estimated using various heuristics, in stream time base
 * - encoding: unused
 * - decoding: set by libavcodec, read by user.
 */
int64_t best_effort_timestamp;

V . FFMPEG 设置播放进度


1 . FFMPEG 设置播放进度 : 传入一个播放进度后 , 首先将播放的进度转成微秒值 , 然后调用 av_seek_frame 方法 , 传入一系列参数 , 即可完成 FFMPEG 播放本地视频文件的进度跳转 ;

//将秒单位 转为 微秒单位
int64_t seek = progress * 1000 * 1000;

// 跳转核心方法 , 跳转到距离时间戳最近的关键帧位置
av_seek_frame(formatContext, -1, seek, AVSEEK_FLAG_BACKWARD);

2 . av_seek_frame ( ) 函数原型 : 查找第 stream_index 个媒体流的 timestamp 微秒附近的关键帧 , 并跳转到该帧开始播放 ;

① AVFormatContext **ps 参数 : 封装了文件格式相关信息的结构体 , 如视频宽高 , 音频采样率等信息 ; 该参数是 二级指针 , 意味着在方法中会修改该指针的指向 , 该参数的实际作用是当做返回值用的 ;

② int stream_index 参数 : 音视频流索引 , 如果设置 -1 , 说明是所有的媒体流同时跳转 ;

③ int64_t timestamp 参数 : 要跳转的目的时间戳 , 之后要在该时间附近查找关键帧 ;

④ int flags 参数 : 设置跳转模式 ;

⑤ int 返回值 : 返回值大于等于 0 , 代表打开成功 , 否则失败 ;

/**
 * Seek to the keyframe at timestamp.
 * 'timestamp' in 'stream_index'.
 *
 * @param s media file handle
 * @param stream_index If stream_index is (-1), a default
 * stream is selected, and timestamp is automatically converted
 * from AV_TIME_BASE units to the stream specific time_base.
 * @param timestamp Timestamp in AVStream.time_base units
 *        or, if no stream is specified, in AV_TIME_BASE units.
 * @param flags flags which select direction and seeking mode
 * @return >= 0 on success
 */
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp,
                  int flags);