李帅

1.优化ffmpeg命令,适配各种类型视频

......@@ -5,7 +5,6 @@ namespace App\Jobs;
use App\Models\Immerse;
use App\Models\VideoTemp;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
......@@ -25,6 +24,8 @@ class AdminMakeImmerse implements ShouldQueue
protected $ffprobe;
protected $media_info;
protected $output_width;
protected $output_height;
......@@ -56,9 +57,8 @@ class AdminMakeImmerse implements ShouldQueue
$template = $adminMakeVideo->temp->where('state',1)->first();
// 素材准备
$drawtext = $this->getTextContentString();
$watermark = $this->getAbsolutePath('images/LOGO_eng.png');
$is_bgm = $template->bg_music == 1;
$is_bgm = $template->bg_music == 1; //是否手动上传背景音
$bgm = $this->getAbsolutePath($template->bgm_url);
// 区分类型
......@@ -66,6 +66,8 @@ class AdminMakeImmerse implements ShouldQueue
$file = $this->getAbsolutePath($adminMakeVideo->video_url);
// 分析视频
$media_info = $this->mediainfo($file);
// 素材准备
$drawtext = $this->getTextContentString();
if ($media_info['format']['nb_streams'] >= 2) {
/** 音频视频轨都有 */
......@@ -262,6 +264,8 @@ class AdminMakeImmerse implements ShouldQueue
public function mediainfo($file)
{
if ($this->media_info) return $this->media_info;
$cmd = $this->ffprobe . ' -v quiet -print_format json -show_format -show_streams ' . escapeshellarg($file);
$output = $this->execmd($cmd);
$data = json_decode($output, true);
......@@ -269,6 +273,7 @@ class AdminMakeImmerse implements ShouldQueue
$output = mb_convert_encoding($output, "UTF-8");
$data = json_decode($output, true);
}
$this->media_info = $data;
return $data;
}
......@@ -372,6 +377,8 @@ class AdminMakeImmerse implements ShouldQueue
case 'every_poem':
case 'one_poem':
$content = $this->adminMakeVideo->poem->content;
$contents = explode("\n",$content); //计算诗词行数
if ($this->media_info['format']['duration'] < 2 * count($contents)) {
$text_file = $this->getAbsolutePath($this->getTempPath('.txt','text'));
file_put_contents($text_file, $content);
$drawtext .= 'drawtext="'.
......@@ -385,6 +392,29 @@ class AdminMakeImmerse implements ShouldQueue
'box=1:boxborderw='. $text_bg_box . ':' .
'boxcolor=' . $text_bg_color . '@' . $opacity . '", ';
}else{
$FID = $FOD = 1.0;
if ($this->media_info['format']['duration'] > 3 * count($contents)) $FID = $FOD = 1.5;
$round = round($this->media_info['format']['duration'] / count($contents),1);
foreach ($contents as $key => $content){
$DS = $key * $round;
$DE = $DS + $round;
$text_file = $this->getAbsolutePath($this->getTempPath('.txt','text'));
file_put_contents($text_file, $content);
$drawtext .= 'drawtext="'.
'fontfile=' . escapeshellarg($font_file) . ':' .
'textfile=' . escapeshellarg($text_file) . ':' .
'fontsize=' . $this->calcFontSize($component->font_size) . ':' .
// 'fontcolor=' . $text_color . '@' . $opacity . ':' .
'fontcolor_expr=' . $text_color . '%{eif\\: clip(255*(1*between(t\\, ' . $DE . ' + ' . $FID . '\\, ' . $DE . ' - ' . $FOD . ') + ((t - ' . $DS . ')/' . $FID . ')*between(t\\, ' . $DS . '\\, ' . $DS . ' + ' . $FID . ') + (-(t - ' . $DE . ')/' . $FOD . ')*between(t\\, ' . $DE . ' - ' . $FOD . '\\, ' . $DE . ') )\\, 0\\, 255) \\: x\\: 2 }:' .
'x=' . escapeshellarg(VideoTemp::POSITION_FFMPEG[$component->position][0]) . ':' .
'y=' . escapeshellarg(VideoTemp::POSITION_FFMPEG[$component->position][1]) . ':' .
'fix_bounds='. $fix_bounds . ':' .
'box=1:boxborderw='. $text_bg_box . ':' .
'boxcolor=' . $text_bg_color . '@' . $opacity . '", ';
}
}
break;
case 'weather':
$content = $this->adminMakeVideo->weather;
......