Files
esun3d/app/code/Esun/Video/Model/Ffmpeg.php
T
2026-09-15 10:35:56 +08:00

43 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Video\Model;
use Magento\Framework\Exception\LocalizedException;
use Symfony\Component\Process\Process;
class Ffmpeg
{
public function __construct(private readonly Config $config)
{
}
public function createCover(string $videoFile, string $coverFile): void
{
$process = new Process([
$this->config->getFfmpegBinary(),
'-hide_banner',
'-loglevel', 'error',
'-ss', '1',
'-i', $videoFile,
'-frames:v', '1',
'-q:v', '2',
'-y',
$coverFile,
]);
$process->setTimeout(60);
try {
$process->run();
} catch (\Throwable $e) {
throw new LocalizedException(__('Unable to run ffmpeg: %1', $e->getMessage()));
}
if (!$process->isSuccessful() || !is_file($coverFile) || filesize($coverFile) === 0) {
throw new LocalizedException(
__('ffmpeg failed to create the cover image: %1', trim($process->getErrorOutput()) ?: __('unknown error'))
);
}
}
}