/** * 拼接视频 * 视频地址为 url * @param $url * @param string $object_name * @return array * @author: xiao * @Date: 2021/4/15 14:04 */ public function vIDeoConcatUrl($url,$object_name = '') { if (!is_array($url) || empty($url)){ $this->data['code'] = -1; $this->data['msg'] = 'url 非数组或为空'; return $this->data; } $str = ''; foreach ($url as $key => $val){ $str .= "file '".$val."' \r\n"; } //写入文件 $temp_txt_name = 'temp_vIDeo_file_'.rand(1000,9999).'.txt'; $fp = @fopen($this->vIDeo_path.$temp_txt_name,"w+"); @fputs($fp,$str); @fclose(); if (!file_exists($this->vIDeo_path.$temp_txt_name)){ $this->data['code'] = -1; $this->data['msg'] = 'url文件写入失败'; return $this->data; } //执行拼接命令 # -safe 0 -protocol_whiteList "file,http,https,rtp,udp,tcp,tls" $ffmpeg_exec = '/usr/local/ffmpeg/bin/ffmpeg -f concat -safe 0 -i '.$this->vIDeo_path.$temp_txt_name.' -c copy '.$this->vIDeo_path.$object_name.''; exec($ffmpeg_exec,$output,$code); if ($code != 0){ $this->data['code'] = -1; $this->data['msg'] = '视频拼接失败'; return $this->data; } //删除临时文件 if (file_exists($this->vIDeo_path.$temp_txt_name)){ @unlink($this->vIDeo_path.$temp_txt_name); } $this->data['data'] = [ 'vIDeo_url' => $this->vIDeo_path.$object_name ]; return $this->data; }
添加字幕(硬压字幕) //主要颜色 private $primaryColour = ['FFFFFF']; //边框颜色 private $OutlineColour = '000000'; //字体 private $Fontname = ["Muyao\-Softbrush","YRDZST"]; /** * 字幕添加 * @param $vIDeo_url * @param $vIDeo_srt_url * @param $object_name * @param string $ratio 分辨率 * @return array * @author: xiao * @Date: 2021/4/15 14:41 */ public function vIDeoSubTitles($vIDeo_url,$vIDeo_srt_url,$object_name,$ratio='720x1280') { if (empty($vIDeo_url) || empty($vIDeo_srt_url) || empty($object_name)){ $this->data['code'] = -1; $this->data['msg'] = '参数为空'; return $this->data; } //下载字幕文件到本地 $file = file_get_contents($vIDeo_srt_url); $srt_name = date('YmdHis').'_'.rand(1000,9999).'_'.'srt'.'.srt'; $srt_path = $this->vIDeo_path.$srt_name; file_put_contents($srt_path,$file); /** * 设置字幕样式 */ //字体 $Font_name = $this->Fontname[rand(0,(count($this->Fontname)-1))]; $out_line_color = $this->OutlineColour; //主要颜色 $primary_color = $this->primaryColour[rand(0,count($this->primaryColour)-1)]; $primary_color = '&H'.$primary_color; $out_line_color = '&H'.$out_line_color; $margin_v = rand(70,85); $Font_size = rand(12,13); //根据分辨率判断视频是否为横屏 if ($ratio == '1280x720' || $ratio == '1920x1080'){ \Log::error('come in ratio'); $Font_size = rand(18,21); $margin_v = rand(5,15); } $force_style='Fontname='.$Font_name.'\,Fontsize='.$Font_size.'\,marginV='.$margin_v.'\,PrimaryColour='.$primary_color.'\,OutlineColour='.$out_line_color.'\,Alignment=2'; $ffmpeg_exec = "/usr/local/ffmpeg/bin/ffmpeg -i ".$vIDeo_url." -threads 5 -preset ultrafast -vf subTitles=".$srt_path.":force_style='".$force_style."' -y ".$this->vIDeo_path.$object_name." "; exec($ffmpeg_exec,$output,$code); if ($code != 0){ $this->data['code'] = -1; $this->data['msg'] = '字幕插入失败'; return $this->data; } $this->data['data'] = [ 'vIDeo_url' => $this->vIDeo_path.$object_name ]; if (file_exists($srt_path)){ @unlink($srt_path); } return $this->data; }
音频插入 /** * 音频插入 * @param $vIDeo_url * @param $audio_url * @param $object_name * @return array * @author: xiao * @Date: 2021/4/15 16:11 */ public function audioInsert($vIDeo_url,$audio_url,$object_name) { if (empty($vIDeo_url) || empty($audio_url) || empty($object_name)){ $this->data['code'] = -1; $this->data['msg'] = '参数为空'; return $this->data; } //消除原有视频的音频 $temp_name = 'temp_'.rand(1000,9999).'.mp4'; //临时文件 $temp_path = public_path() . '/upload/' . $temp_name; $clear_audio_exec = '/usr/local/ffmpeg/bin/ffmpeg -i '.$vIDeo_url.' -vcodec copy -an '.$temp_path.''; exec($clear_audio_exec,$out,$audio_code); if ($audio_code != 0 ){ $this->data['code'] = -1; $this->data['msg'] = '消除音频失败'; return $this->data; } # -safe 0 -protocol_whiteList $merge_vIDeo_exec = '/usr/local/ffmpeg/bin/ffmpeg -i '.$temp_path.' -i '.$audio_url.' -vcodec copy -acodec copy '.$this->vIDeo_path.$object_name.' '; exec($merge_vIDeo_exec,$o,$merge_code); if ($merge_code != 0){ $this->data['code'] = -1; $this->data['msg'] = '音频插入失败'; return $this->data; } $this->data['data'] = [ 'vIDeo_url' => $this->vIDeo_path.$object_name ]; return $this->data; }
压缩视频 /** * 压缩视频 * @param $vIDeo_url * @param string $object_name * @param string $code_rate * @param string $ratio 码率 * @return array * @author: xiao * @Date: 2021/4/27 14:49 */ public function vIDeoCompress($vIDeo_url,$object_name = '',$code_rate = '500k',$ratio='1280x720' ) { if (empty($vIDeo_url)){ $this->data['code'] = -1; $this->data['msg'] = '参数为空'; return $this->data; }// $ffmpeg_exec = '/usr/local/ffmpeg/bin/ffmpeg -i '.$vIDeo_url.' -threads 2 -preset ultrafast -b:v '.$code_rate.' -s '.$wIDth.'x'.$height.' '.$this->vIDeo_path.$object_name.' '; $ffmpeg_exec = '/usr/local/ffmpeg/bin/ffmpeg -i '.$vIDeo_url.' -threads 2 -preset ultrafast -b:v '.$code_rate.' '.$this->vIDeo_path.$object_name.' '; exec($ffmpeg_exec,$o,$code); if ($code != 0){ $this->data['code'] = -1; $this->data['msg'] = '视频压缩失败'; return $this->data; } $this->data['data'] = [ 'vIDeo_url' => $this->vIDeo_path.$object_name ]; return $this->data; }
设置关键帧(主要用于视频拼接中间有卡顿现象) /** * 设置关键帧 * @param $vIDeo_url * @param $object_name * @return array * @author: xiao * @Date: 2021/4/27 17:05 */ public function vIDeoSetKey($vIDeo_url, $object_name) { $ffmpeg_exec = '/usr/local/ffmpeg/bin/ffmpeg -i '.$vIDeo_url.' -threads 2 -preset ultrafast -g 1 -keyint_min 2 '.$this->vIDeo_path.$object_name.' '; exec($ffmpeg_exec,$o,$code); if ($code != 0){ $this->data['code'] = -1; $this->data['msg'] = '关键帧设置失败'; return $this->data; } $this->data['data'] = [ 'vIDeo_url' => $this->vIDeo_path.$object_name ]; return $this->data; }
获取视频信息 /** * 获取视频信息 * @param $vIDeo_url * @return array|string[] * @author: xiao * @Date: 2021/6/10 15:54 */ public function getVIDeoInfo($vIDeo_url) { $arr = []; $ffmpeg_exec = '/usr/local/ffmpeg/bin/ffprobe -i '.$vIDeo_url.' -preset ultrafast 2>&1';// $vIDeo_info = shell_exec($ffmpeg_exec); exec($ffmpeg_exec,$info,$code); if (empty($info)){ $this->data['code'] = -1; $this->data['msg'] = '获取视频信息失败'; return $this->data; } /** * 获取视频信息开始 */ //分辨率 if (isset($info[18])){ $temp_ratio = explode(',',$info[18]); $arr['ratio'] = isset($temp_ratio[2])?trim($temp_ratio[2]):'1280x720'; } return $arr; }
总结 以上是内存溢出为你收集整理的ffmpeg公用方法封装-PHP全部内容,希望文章能够帮你解决ffmpeg公用方法封装-PHP所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)