短视频导入功能示例
说明
短视频去水印最终方案选取了GitHub上的一个开源代码,本文讲一下通过短视频链接将短视频导入到平台的相关代码,本次讲解是存到本地为例,线上已经做了CDN+OSS方案的处理。
说明文档
GitHub:https://github.com/smalls0098/video-tools 短视频平台去水印
http://packagist.p2hp.com/packages/james-heinrich/getid3 获取视频信息
开发过程
1.通过video-tools扩展获取短视频的去水印链接
2.通过curl将短视频的无水印视频和封面下载到本地
3.通过getid3扩展将视频的宽高时长返回给前端
相关代码
获取短视频平台类型
protected static $typeArray=[
'bili' => ['b23.tv', 'www.bilibili.com'],
'douyin' => ['douyin.com', 'iesdouyin.com'],
'huoshan' => ['huoshan.com'],
'kuaishou' => ['ziyang.m.kspkg.com', 'kuaishou.com', 'gifshow.com', 'chenzhongtech.com', 'v.kuaishouapp.com'],
'livideo' => ['www.pearvideo.com'],
'meipai' => ['www.meipai.com'],
'momo' => ['immomo.com'],
'pipigaoxiao' => ['ippzone.com'],
'pipixia' => ['pipix.com'],
'quanminggaoxiao' => ['longxia.music.xiaomi.com'],
'shuabao' => ['h5.shua8cn.com', 'm.shua8cn.com'],
'toutiao' => ['toutiaoimg.com', 'toutiaoimg.cn'],
'weishi' => ['weishi.qq.com'],
'xiaokaxiu' => ['mobile.xiaokaxiu.com'],
'xigua' => ['xigua.com'],
'zuiyou' => ['izuiyou.com'],
'weibo' => ['weibo.com', 'weibo.cn'],
'newweibo' => ['video.weibo.com/show'],
'miaopai' => ['miaopai.com'],
'qqvideo' => ['m.v.qq.com'],
'taobao' => ['h5.m.taobao.com', 'm.tb.cn'],
];
/**
* [getShopVideoType 获取短视频平台类型]
* @param [type] $url [传入视频链接]
* @return [type] [返回视频类型 douyin kuaishou]
*/
public static function getShopVideoType($url){
$type="";
foreach (self::$typeArray as $key => $v) {
$result=false;
foreach ($v as $value){
if (strpos($url, $value)) {
$result=true;
break;
}
}
if($result){
$type=$key;
break;
}
}
return $type;
}
获取短链接
/**
* [getShortVideoUrl 获取短视频短链接]
* @param [type] $url [视频复制信息]
* @return [type] [视频地址]
*/
public static function getShortVideoUrl($url){
preg_match('/https?:\/{2}[-A-Za-z0-9+&@#\/\%?=~_|!:,.;]+[-A-Za-z0-9+&@#\/\%=~_|]/i', $url, $matches);
return $matches[0];
}
获取视频无水印地址
$videoInfo = VideoManager::DouYin()->start($shortUrl); //抖音示例 video-tools插件实现
curl下载到本地 图片为例
public static function getRemoteImg($imgUrl){
$imgExtArray=["jpg",'jpeg','png','grf'];//图片后缀设置
// const voice = 4; // 音频
$ch = curl_init($imgUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$img = curl_exec ($ch);
curl_close ($ch);
// 保存图片到本地
$filename=UploadService::newFileName(); //随机生成图片名称
$dst = UploadService::getLocalFilePath(1); //获取图片保存地址
$ext = strtolower(pathinfo($imgUrl, PATHINFO_EXTENSION));//获取链接后缀
//得到图片后缀
foreach ($imgExtArray as $value) {
if(strstr($ext,$value)){
$ext = $value;
}
}
$fileDst = $dst['fileDst'].'/'.$filename.'.'.$ext; //真实保存地址
$resDst = $dst['resDst'].'/'.$filename.'.'.$ext; //真实保存地址
$fp = fopen($fileDst, a);
$writeRes = fwrite($fp, $img);
fclose($fp);
if( $writeRes ){
return [
'fileDst' => $fileDst,'resDst' => $resDst];
}else{
return false;
}
}
getid3 获取相关信息
/**
* [getVideoInfo 获取视频信息]
* @param [type] $path [传入本地地址]
* @return [type] [返宽、高、时长等信息]
*/
public static function getVideoInfo($path){
$getID3 = new getID3(); //实例化类
// $path = '/data/website/cardApp/htdocs/storage/voice/20201020/ba03e4ad03f79add.mp4';
$file = $getID3->analyze($path);
if(!$file){
return false;
}
return [
'playtime_string' => $file['playtime_string'],
'playtime_seconds' => $file['playtime_seconds'],
'resolution_x'=> $file['video']['resolution_x'],
'resolution_y' =>$file['video']['resolution_y']
];
}
结尾
心如花木,向阳而生。