1. 萤石云的视频列表没有封面
默认获取到的视频列表是没有封面的,好在ys7提供了一个抓取当前画面的接口:
image.png
所以,我在用户访问视频列表页的时候,将每个视频都去抓拍一张图片,作为poster使用,如下图:
20230808145614.jpg
2. 但是,问题来了
目前的视频列表只有2条数据的时候,页面加载完成都需要5~8秒的时间,这用户体验能行的?
循环抓拍图片作为封面这个方案,直接就被pass掉了!要是有10多个视频,那不得抓拍半天啊!!!
3. 解决
只能想办法在后台先提前设置好封面,前端访问列表的时候,直接用了!
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validateFailException()->validate($validate);
}
//抓取摄像头的图片作为封面
$competition = CompetitionModel::get($params['competition_id']);
if (!$competition->organizer->appkey || !$competition->organizer->appsecret) {
throw new \Exception('主办方的萤石云配置错误');
}
$auth = new YS7Auth($competition->organizer->appkey, $competition->organizer->appsecret);
$client = new YS7Client($auth);
$res = $client->device->capture($params['deviceSerial'], $params['channelNo']);
$params['picUrl'] = $res['picUrl'];
//判断是修改还是添加
$map['deviceId'] = $params['deviceId'];
$map['competition_id'] = $params['competition_id'];
$info = $this->model->where($map)->find();
$path = '/uploads/poster/';
if ($info) {
$path .= $info['id'].'.jpg';
$result = $this->model->allowField(true)->where($map)->update($params);
} else {
$result = $this->model->allowField(true)->save($params);
$path .= $this->model->id.'.jpg';
}
if (file_put_contents($path, file_get_contents($res['picUrl']))) {
$this->model->allowField(true)->save(['picUrl' => $path]);
}
Db::commit();
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
网友评论