<?php
/**
* Created by PhpStorm.
* User: Si Jiang Long
* Date: 2019/11/7
* Time: 9:22
*/
namespace app\common\lib;
/**
* ffmpeg 類
* Class Ffmpeg
* @package app\common\lib
*/
class Ffmpeg {
private static $instance;
private static $ffmpeg = 'G:\ffmpeg\bin\ffmpeg';
private static $ffprobe = 'G:\ffmpeg\bin\ffprobe';
static function getInstance()
{
//instanceof 確定一個變量是否屬於某一類的實例
if(!self::$instance instanceof self){
static::$instance = new self();
}
return static::$instance;
}
private function __construct()
{
}
//防止克隆
private function __clone()
{
// TODO: Implement __clone() method.
}
//防止序列化
private function __wakeup()
{
// TODO: Implement __wakeup() method.
}
/**
* 獲取視頻及音頻信息 寬、高、總時長、幀率、碼率等等。。。
* @param $file 視頻路徑
* @param $formant 返回的數據格式 支持:json xml array
* @return false|string
*/
public function getVideoInfo($file,$formant){
$cmd = static::$ffprobe . ' -v quiet -print_format '.$formant.' -show_format -show_streams "%s" 2>&1';
ob_start();
passthru(sprintf($cmd, $file));
$video_info = ob_get_contents();
ob_end_clean();
return $video_info;
}
/**
* 截圖(可截取多張圖)
* @param $file 視頻路徑
* @param $s 多少秒截一次圖 $s = round(視頻總時長 / 4, 2); 截四張圖
* @param $output 圖片輸出路徑 $output = /image/example_%02d.jpg';
* @param $width 原視頻width
* @param $height 原視頻height
* @param int $outoptWidth 截圖的寬
* @param int $outoptHeight 截圖的高
* @return bool
*/
public function screenshot($file,$s,$output,$width,$height,$outputWidth = 0,$outputHeight = 0){
if($outputWidth > 0 && $outputHeight > 0){
$width = $outputWidth;
$height = $outputHeight;
}else{
//如果有圖寬,自動算截圖高
if($outputWidth > 0){
$float = round($height / $width,2);
$height = floor($outputWidth * $float);
if($height%2 != 0){
$height = $height + 1;
}
$width = $outputWidth;
}
}
$cmd = static::$ffmpeg . ' -y -i "'.$file.'" -vf "fps=1/'.$s.'" -s '.$width.'x'.$height.' -an ' . $output;
system($cmd,$out);
if($out == 0){
return $height;
}else{
return false;
}
}
/**
* 生成雪碧圖 四張截圖拼一張大圖
* @param $file
* @param $s
* @param $output
* @return bool
*/
public function spriteChart($file,$s,$output){
$cmd = static::$ffmpeg . ' -y -i "'.$file.'" -vf "fps=1/'.$s.',scale=iw/4:-1,tile=2x2" -an ' . $output;
system($cmd,$out);
if($out == 0){
return true;
}else{
return false;
}
}
/**
* 視頻轉碼加水印
* @param $file 原視頻路徑
* @param $output 輸出路徑
* @param $width 原視頻width
* @param $height 原視頻height
* @param $outputWidth 輸出width
* @param $fileId
* @param $logoPath 水印logo圖片路徑
* @return bool
*/
public function transcoding($file,$output,$logoPath,$width,$height,$outputWidth = 0, $outputHeight = 0){
$x = $width/2-100;
$y = $height/2-33;
if($outputWidth > 0 && $outputHeight > 0){
$width = $outputWidth;
$height = $outputHeight;
}else{
if($outputWidth > 0){
$float = round($height / $width,2);
$height = floor($outputWidth * $float);
if($height%2 != 0){
$height = $height + 1;
}
$width = $outputWidth;
}
}
$cmd = static::$ffmpeg . ' -y -i "'.$file.'" -i '.$logoPath.' -r 18 -b 800k -s '.$width.'*'.$height.' -filter_complex overlay='.$x.':'.$y.' '.$output;
system($cmd,$out);
if($out == 0){
return true;
}else{
return false;
}
}
}