You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
4.8 KiB

<?php
namespace App\Services;
use ImageOptimizer\OptimizerFactory;
use Mhor\MediaInfo\MediaInfo;
use Illuminate\Support\Facades\Redis;
class ReplaceCompressedMediaService {
private $factory;
private $optimizer;
public function __construct()
{
$this->factory = new OptimizerFactory([
"output_filepath_pattern" => "%basename%/%filename%-new%ext%",
"ignore_errors" => false,
"jpegoptim_options" => array('-m85', '-S20%', '--strip-all', '--all-progressive'),
// "jpegtran_options" => array('-m85', '-S30', '-optimize', '-progressive')
]);
$this->optimizer = $this->factory->get("jpegoptim");
}
public function processDir($baseDir = "/Volumes/WD/tmp/bilibili/aoa")
{
$files = scandir($baseDir);
foreach ($files as $file) {
if ($file == "." || $file == ".." || $file == ".DS_Store") {
continue;
}
$subDir = implode("/", [$baseDir, $file]);
$isDir = is_dir($subDir);
if ($isDir) {
$this->processDir($subDir);
} else {
$this->processFiles($subDir);
}
}
}
public function processFiles($pathFile)
{
$mime = mime_content_type($pathFile);
// dump("file type", [$mime, $pathFile]);exit;
// continue;
if (strstr($mime, "video/")) {
$this->processVideo($pathFile);
} elseif (strstr($mime, "image/")) {
$this->processImage($pathFile);
}
}
public function processVideo($pathFile) {
if (is_file($pathFile)) {
$mediaInfo = new MediaInfo();
$mediaInfo->setConfig('use_oldxml_mediainfo_output_format', true);
$mediaContainer = $mediaInfo->getInfo($pathFile);
foreach ($mediaContainer->getVideos() as $video) {
$height = $video->get('height')->getAbsoluteValue();
$width = $video->get('width')->getAbsoluteValue();
if ($height > $width && $width <= 720) {
echo "$pathFile 分辨率小于 720p 跳过\n";
return;
}
if ($height <= $width && $height <= 720) {
echo "$pathFile 分辨率小于 720p 跳过\n";
return;
}
}
$fileInfo = pathinfo($pathFile);
dump("fileInfo", $fileInfo);
if (ends_with($fileInfo["filename"], "-XXXXX#compressed")) {
return;
}
if (starts_with($fileInfo["filename"], "171221 M!CountDown 光州特辑女团现场合集") || starts_with($fileInfo["filename"], "Rainbow - A 现场合集 P14 Rainbow - A + Mach")) {
return;
}
if (Redis::sismember("unneed", $fileInfo["filename"])) {
return;
}
$targetFile = $fileInfo["dirname"] . '/' .$fileInfo["filename"] . '-XXXXX#compressed'. '.' . $fileInfo["extension"];
if (is_file($targetFile)) {
unlink($pathFile);
rename($targetFile, $pathFile);
return;
}
dump("targetFile", [$targetFile]);
// $result = shell_exec("handBrakeCli -Z 'Very Fast 720p30' -i '". $subPathFile ."' -o '". $targetFile . " && echo 'success'");
$result = shell_exec("handBrakeCli --json -Z 'Very Fast 720p30' -i '". $pathFile ."' -o '". $targetFile . "'");
// echo $result;
preg_match_all("#\"State\"\: \"WORKDONE\"#", $result, $match);
if (count($match) > 0) {
echo "compress work done remove the file \n";
$oldFileSize = filesize($pathFile);
$newFileSize = filesize($targetFile);
if ($newFileSize >= $oldFileSize) {
Redis::sadd("unneed", $fileInfo["filename"]);
echo "old file size is smaller than new one, old is " . file_size($oldFileSize) . " and new is " . file_size($newFileSize) . ", now remove new one";
unlink($targetFile);
} else {
echo "new file size is smaller than old one, new is " . file_size($newFileSize) . " and old is " . file_size($oldFileSize) . ", now remove old one";
unlink($pathFile);
rename($targetFile, $pathFile);
}
}
}
}
public function processImage($pathFile) {
$this->optimizer->optimize($pathFile);
}
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
}