<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
use App\Jobs\BilibiliEncode;
|
|
use App\Services\FfmpegService;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
class QueuedFfmpegService {
|
|
|
|
private $ffmpegService;
|
|
|
|
private $fileService;
|
|
|
|
private $destinationDirectory;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->ffmpegService = new FfmpegService();
|
|
$this->ffmpegService->setNeedRemoveAfterEncode(true);
|
|
$this->ffmpegService->setNeedRemoveExistFiles(false);
|
|
$this->fileService = new FileService();
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getDestinationDirectory()
|
|
{
|
|
return $this->destinationDirectory;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $destinationDirectory
|
|
*/
|
|
public function setDestinationDirectory($destinationDirectory): void
|
|
{
|
|
$this->destinationDirectory = $destinationDirectory;
|
|
}
|
|
|
|
|
|
|
|
public function processDir($path)
|
|
{
|
|
$files = scandir($path);
|
|
foreach ($files as $file) {
|
|
if ($file == "." || $file == ".." || $file == ".DS_Store" || $file == "1762") {
|
|
continue;
|
|
}
|
|
$subDir = implode(DIRECTORY_SEPARATOR, [$path, $file]);
|
|
$isDir = is_dir($subDir);
|
|
if ($isDir) {
|
|
$this->processDir($subDir);
|
|
} else {
|
|
$this->addToQueue($subDir);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function addToQueue($file)
|
|
{
|
|
// BilibiliEncode::dispatch($file);
|
|
Log::info("add $file to ffmpeg process queue");
|
|
Redis::connection()->lpush("file_to_encode", $file);
|
|
// $result = Redis::publish("file_to_process", $file);
|
|
// echo $result;
|
|
}
|
|
|
|
public function processQueue()
|
|
{
|
|
while (true) {
|
|
Log::info("start or restart process files in redis queue");
|
|
$empty = true;
|
|
$item = Redis::connection()->rpop("file_to_encode");
|
|
while ($item != null) {
|
|
echo $item . "\n";
|
|
$empty = false;
|
|
$this->ffmpegService->processVideo($item);
|
|
$this->fileService->mvFile($item, $this->getDestinationDirectory());
|
|
$item = Redis::connection()->rpop("file_to_encode");
|
|
|
|
}
|
|
if ($empty) {
|
|
Log::info("process end sleep 300 seconds");
|
|
} else {
|
|
Log::info("process queue empty sleep 300 seconds");
|
|
}
|
|
|
|
sleep(5 *60);
|
|
}
|
|
|
|
exit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// redis 订阅逻辑
|
|
// Redis::subscribe(["file_to_process"], function ($message) use ($ffmpeg) {
|
|
// echo $message;
|
|
// sleep(10);
|
|
// $ffmpeg->processVideo($message);
|
|
// });
|
|
}
|
|
}
|