2023-04-10 21:01:29 +00:00
|
|
|
<?php
|
|
|
|
|
2023-04-14 23:03:22 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-04-14 23:02:13 +00:00
|
|
|
namespace OCA\Memories\Service;
|
|
|
|
|
2023-10-22 19:58:33 +00:00
|
|
|
use OCA\Memories\Settings\SystemConfig;
|
2023-04-10 21:01:29 +00:00
|
|
|
|
|
|
|
class BinExt
|
|
|
|
{
|
2023-05-05 10:49:39 +00:00
|
|
|
public const EXIFTOOL_VER = '12.60';
|
2023-10-25 00:19:43 +00:00
|
|
|
public const GOVOD_VER = '0.1.21';
|
2023-10-04 19:47:19 +00:00
|
|
|
public const NX_VER_MIN = '1.1';
|
2023-04-10 21:01:29 +00:00
|
|
|
|
2023-10-03 18:01:36 +00:00
|
|
|
/** Get the path to the temp directory */
|
2023-10-03 18:03:15 +00:00
|
|
|
public static function getTmpPath(): string
|
|
|
|
{
|
2023-10-22 19:58:33 +00:00
|
|
|
return SystemConfig::get('memories.exiftool.tmp') ?: sys_get_temp_dir();
|
2023-10-03 18:01:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 00:51:16 +00:00
|
|
|
/** Copy a binary to temp dir for execution */
|
|
|
|
public static function getTempBin(string $path, string $name, bool $copy = true): string
|
|
|
|
{
|
2023-04-21 08:13:15 +00:00
|
|
|
// Bust cache if the path changes
|
|
|
|
$suffix = hash('crc32', $path);
|
2023-04-11 02:45:42 +00:00
|
|
|
|
|
|
|
// Check target temp file
|
2023-10-03 18:01:36 +00:00
|
|
|
$target = self::getTmpPath().'/'.$name.'-'.$suffix;
|
2023-04-11 00:51:16 +00:00
|
|
|
if (file_exists($target)) {
|
|
|
|
if (!is_writable($target)) {
|
|
|
|
throw new \Exception("{$name} temp binary path is not writable: {$target}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_executable($target) && !chmod($target, 0755)) {
|
|
|
|
throw new \Exception("failed to make {$name} temp binary executable: {$target}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $target;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($copy) {
|
2023-04-24 19:42:54 +00:00
|
|
|
if (empty($path)) {
|
2023-04-24 19:48:18 +00:00
|
|
|
throw new \Exception('binary path is empty (run occ maintenance:repair or use system perl)');
|
2023-04-24 19:42:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 00:51:16 +00:00
|
|
|
if (!copy($path, $target)) {
|
|
|
|
throw new \Exception("failed to copy {$name} binary from {$path} to {$target}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::getTempBin($path, $name, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Exception("failed to find exiftool temp binary {$target}");
|
|
|
|
}
|
|
|
|
|
2023-04-11 18:52:12 +00:00
|
|
|
/** Get the name for a binary */
|
|
|
|
public static function getName(string $name, string $version = ''): string
|
|
|
|
{
|
2023-10-22 19:58:33 +00:00
|
|
|
$id = SystemConfig::get('instanceid');
|
2023-04-11 18:52:12 +00:00
|
|
|
|
|
|
|
return empty($version) ? "{$name}-{$id}" : "{$name}-{$id}-{$version}";
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:01:29 +00:00
|
|
|
/** Test configured exiftool binary */
|
2023-04-18 21:02:34 +00:00
|
|
|
public static function testExiftool(): string
|
2023-04-10 21:01:29 +00:00
|
|
|
{
|
|
|
|
$cmd = implode(' ', array_merge(self::getExiftool(), ['-ver']));
|
2023-10-14 23:06:25 +00:00
|
|
|
|
|
|
|
/** @psalm-suppress ForbiddenCode */
|
2023-04-10 21:01:29 +00:00
|
|
|
$out = shell_exec($cmd);
|
|
|
|
if (!$out) {
|
2023-04-11 02:38:07 +00:00
|
|
|
throw new \Exception("failed to run exiftool: {$cmd}");
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
// Check version
|
2023-04-10 21:01:29 +00:00
|
|
|
$version = trim($out);
|
|
|
|
$target = self::EXIFTOOL_VER;
|
|
|
|
if (!version_compare($version, $target, '=')) {
|
|
|
|
throw new \Exception("version does not match {$version} <==> {$target}");
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
// Test with actual file
|
2023-04-14 23:02:13 +00:00
|
|
|
$file = realpath(__DIR__.'/../../exiftest.jpg');
|
2023-04-14 02:43:13 +00:00
|
|
|
if (!$file) {
|
|
|
|
throw new \Exception('Could not find EXIF test file');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$exif = \OCA\Memories\Exif::getExifFromLocalPath($file);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception("Couldn't read Exif data from test file: ".$e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$exif) {
|
|
|
|
throw new \Exception('Got no Exif data from test file');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($exp = '2004:08:31 19:52:58') !== ($got = $exif['DateTimeOriginal'])) {
|
|
|
|
throw new \Exception("Got wrong Exif data from test file {$exp} <==> {$got}");
|
|
|
|
}
|
|
|
|
|
2023-04-18 21:02:34 +00:00
|
|
|
return $version;
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 00:51:16 +00:00
|
|
|
/** Get path to exiftool binary */
|
|
|
|
public static function getExiftoolPBin(): string
|
|
|
|
{
|
2023-10-22 19:58:33 +00:00
|
|
|
$path = SystemConfig::get('memories.exiftool');
|
2023-04-11 00:51:16 +00:00
|
|
|
|
2023-04-11 18:52:12 +00:00
|
|
|
return self::getTempBin($path, self::getName('exiftool', self::EXIFTOOL_VER));
|
2023-04-11 00:51:16 +00:00
|
|
|
}
|
|
|
|
|
2023-10-14 09:07:18 +00:00
|
|
|
/**
|
|
|
|
* Get path to exiftool binary for proc_open.
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2023-04-10 21:01:29 +00:00
|
|
|
public static function getExiftool(): array
|
|
|
|
{
|
2023-10-22 19:58:33 +00:00
|
|
|
if (SystemConfig::get('memories.exiftool_no_local')) {
|
2023-10-20 19:35:35 +00:00
|
|
|
return ['perl', realpath(__DIR__.'/../../bin-ext/exiftool/exiftool')];
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 00:51:16 +00:00
|
|
|
return [self::getExiftoolPBin()];
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-14 09:07:18 +00:00
|
|
|
/**
|
|
|
|
* Detect the exiftool binary to use.
|
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public static function detectExiftool(): false|string
|
2023-04-10 21:01:29 +00:00
|
|
|
{
|
2023-10-22 19:58:33 +00:00
|
|
|
if (!empty($path = SystemConfig::get('memories.exiftool')) && file_exists($path)) {
|
2023-04-10 21:32:35 +00:00
|
|
|
return $path;
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 19:58:33 +00:00
|
|
|
if (SystemConfig::get('memories.exiftool_no_local')) {
|
2023-04-10 21:32:35 +00:00
|
|
|
return implode(' ', self::getExiftool());
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect architecture
|
|
|
|
$arch = \OCA\Memories\Util::getArch();
|
|
|
|
$libc = \OCA\Memories\Util::getLibc();
|
|
|
|
|
|
|
|
// Get static binary if available
|
|
|
|
if ($arch && $libc) {
|
|
|
|
// get target file path
|
2023-10-20 19:35:35 +00:00
|
|
|
$path = realpath(__DIR__."/../../bin-ext/exiftool-{$arch}-{$libc}");
|
2023-04-10 21:01:29 +00:00
|
|
|
|
2023-04-11 00:51:16 +00:00
|
|
|
// make sure it exists
|
2023-04-25 05:54:53 +00:00
|
|
|
if ($path && file_exists($path)) {
|
2023-10-22 19:58:33 +00:00
|
|
|
SystemConfig::set('memories.exiftool', $path);
|
2023-04-25 05:54:53 +00:00
|
|
|
|
2023-04-10 21:32:35 +00:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-22 19:58:33 +00:00
|
|
|
SystemConfig::set('memories.exiftool_no_local', true);
|
2023-04-10 21:32:35 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-10 23:45:04 +00:00
|
|
|
/**
|
|
|
|
* Get the upstream URL for a video.
|
|
|
|
*/
|
|
|
|
public static function getGoVodUrl(string $client, string $path, string $profile): string
|
|
|
|
{
|
|
|
|
$path = rawurlencode($path);
|
|
|
|
|
2023-10-22 19:58:33 +00:00
|
|
|
$bind = SystemConfig::get('memories.vod.bind');
|
|
|
|
$connect = SystemConfig::get('memories.vod.connect', $bind);
|
2023-04-10 23:45:04 +00:00
|
|
|
|
|
|
|
return "http://{$connect}/{$client}{$path}/{$profile}";
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
public static function getGoVodConfig(bool $local = false): array
|
2023-04-10 23:45:04 +00:00
|
|
|
{
|
|
|
|
// Get config from system values
|
|
|
|
$env = [
|
2023-10-25 00:19:43 +00:00
|
|
|
'qf' => SystemConfig::get('memories.vod.qf'),
|
|
|
|
|
2023-10-22 19:58:33 +00:00
|
|
|
'vaapi' => SystemConfig::get('memories.vod.vaapi'),
|
|
|
|
'vaapiLowPower' => SystemConfig::get('memories.vod.vaapi.low_power'),
|
2023-04-10 23:45:04 +00:00
|
|
|
|
2023-10-22 19:58:33 +00:00
|
|
|
'nvenc' => SystemConfig::get('memories.vod.nvenc'),
|
|
|
|
'nvencTemporalAQ' => SystemConfig::get('memories.vod.nvenc.temporal_aq'),
|
|
|
|
'nvencScale' => SystemConfig::get('memories.vod.nvenc.scale'),
|
2023-08-03 20:13:17 +00:00
|
|
|
|
2023-10-22 19:58:33 +00:00
|
|
|
'useTranspose' => SystemConfig::get('memories.vod.use_transpose'),
|
|
|
|
'useGopSize' => SystemConfig::get('memories.vod.use_gop_size'),
|
2023-04-10 23:45:04 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (!$local) {
|
|
|
|
return $env;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get temp directory
|
2023-10-22 19:58:33 +00:00
|
|
|
$tmpPath = SystemConfig::get('memories.vod.tempdir', sys_get_temp_dir().'/go-vod/');
|
2023-04-10 23:45:04 +00:00
|
|
|
|
|
|
|
// Make sure path ends with slash
|
|
|
|
if ('/' !== substr($tmpPath, -1)) {
|
|
|
|
$tmpPath .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add instance ID to path
|
2023-10-22 19:58:33 +00:00
|
|
|
$tmpPath .= SystemConfig::get('instanceid');
|
2023-04-10 23:45:04 +00:00
|
|
|
|
|
|
|
return array_merge($env, [
|
2023-10-22 19:58:33 +00:00
|
|
|
'bind' => SystemConfig::get('memories.vod.bind'),
|
|
|
|
'ffmpeg' => SystemConfig::get('memories.vod.ffmpeg'),
|
|
|
|
'ffprobe' => SystemConfig::get('memories.vod.ffprobe'),
|
2023-04-10 23:45:04 +00:00
|
|
|
'tempdir' => $tmpPath,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-04-11 00:57:57 +00:00
|
|
|
/**
|
|
|
|
* Get temp binary for go-vod.
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public static function getGoVodBin(): string
|
2023-04-11 00:57:57 +00:00
|
|
|
{
|
2023-10-22 19:58:33 +00:00
|
|
|
$path = SystemConfig::get('memories.vod.path');
|
2023-04-11 18:52:12 +00:00
|
|
|
|
|
|
|
return self::getTempBin($path, self::getName('go-vod', self::GOVOD_VER));
|
2023-04-11 00:57:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 23:45:04 +00:00
|
|
|
/**
|
|
|
|
* If local, restart the go-vod instance.
|
|
|
|
* If external, configure the go-vod instance.
|
|
|
|
*/
|
2023-10-14 09:07:18 +00:00
|
|
|
public static function startGoVod(): ?string
|
2023-04-10 23:45:04 +00:00
|
|
|
{
|
2023-04-11 18:52:12 +00:00
|
|
|
// Check if disabled
|
2023-10-22 19:58:33 +00:00
|
|
|
if (SystemConfig::get('memories.vod.disable')) {
|
2023-04-11 18:52:12 +00:00
|
|
|
// Make sure it's dead, in case the user just disabled it
|
2023-10-20 19:42:22 +00:00
|
|
|
self::pkill(self::getName('go-vod'));
|
2023-04-11 18:52:12 +00:00
|
|
|
|
2023-10-14 09:07:18 +00:00
|
|
|
return null;
|
2023-04-11 18:52:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 23:45:04 +00:00
|
|
|
// Check if external
|
2023-10-22 19:58:33 +00:00
|
|
|
if (SystemConfig::get('memories.vod.external')) {
|
2023-04-10 23:45:04 +00:00
|
|
|
self::configureGoVod();
|
|
|
|
|
2023-10-14 09:07:18 +00:00
|
|
|
return null;
|
2023-04-10 23:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get transcoder path
|
2023-04-11 00:57:57 +00:00
|
|
|
$transcoder = self::getGoVodBin();
|
2023-04-10 23:45:04 +00:00
|
|
|
if (empty($transcoder)) {
|
|
|
|
throw new \Exception('Transcoder not configured');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get local config
|
|
|
|
$env = self::getGoVodConfig(true);
|
|
|
|
$tmpPath = $env['tempdir'];
|
|
|
|
|
|
|
|
// (Re-)create temp dir
|
2023-10-14 23:06:25 +00:00
|
|
|
/** @psalm-suppress ForbiddenCode */
|
2023-04-10 23:45:04 +00:00
|
|
|
shell_exec("rm -rf '{$tmpPath}' && mkdir -p '{$tmpPath}' && chmod 755 '{$tmpPath}'");
|
|
|
|
|
|
|
|
// Check temp directory exists
|
|
|
|
if (!is_dir($tmpPath)) {
|
|
|
|
throw new \Exception("Temp directory could not be created ({$tmpPath})");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check temp directory is writable
|
|
|
|
if (!is_writable($tmpPath)) {
|
|
|
|
throw new \Exception("Temp directory is not writable ({$tmpPath})");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write config to file
|
|
|
|
$logFile = $tmpPath.'.log';
|
|
|
|
$configFile = $tmpPath.'.json';
|
|
|
|
file_put_contents($configFile, json_encode($env, JSON_PRETTY_PRINT));
|
|
|
|
|
|
|
|
// Kill the transcoder in case it's running
|
2023-10-20 19:42:22 +00:00
|
|
|
self::pkill(self::getName('go-vod'));
|
2023-04-10 23:45:04 +00:00
|
|
|
|
|
|
|
// Start transcoder
|
2023-10-14 23:06:25 +00:00
|
|
|
/** @psalm-suppress ForbiddenCode */
|
2023-04-10 23:45:04 +00:00
|
|
|
shell_exec("nohup {$transcoder} {$configFile} >> '{$logFile}' 2>&1 & > /dev/null");
|
|
|
|
|
|
|
|
// wait for 500ms
|
|
|
|
usleep(500000);
|
|
|
|
|
|
|
|
return $logFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test go-vod and (re)-start if it is not external.
|
|
|
|
*/
|
2023-04-18 21:02:34 +00:00
|
|
|
public static function testStartGoVod(): string
|
2023-04-10 23:45:04 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
return self::testGoVod();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// silently try to restart
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to (re)start go-vod
|
|
|
|
// If it is external, this only attempts to reconfigure
|
|
|
|
self::startGoVod();
|
|
|
|
|
|
|
|
// Test again
|
|
|
|
return self::testGoVod();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Test the go-vod instance that is running */
|
2023-04-18 21:02:34 +00:00
|
|
|
public static function testGoVod(): string
|
2023-04-10 23:45:04 +00:00
|
|
|
{
|
2023-04-11 18:52:12 +00:00
|
|
|
// Check if disabled
|
2023-10-22 19:58:33 +00:00
|
|
|
if (SystemConfig::get('memories.vod.disable')) {
|
2023-04-11 18:52:12 +00:00
|
|
|
throw new \Exception('Transcoding is disabled');
|
|
|
|
}
|
|
|
|
|
2023-04-10 23:45:04 +00:00
|
|
|
// TODO: check data mount; ignoring the result of the file for now
|
2023-04-14 23:02:13 +00:00
|
|
|
$testfile = realpath(__DIR__.'/../../exiftest.jpg');
|
2023-04-10 23:45:04 +00:00
|
|
|
|
|
|
|
// Make request
|
|
|
|
$url = self::getGoVodUrl('test', $testfile, 'test');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$client = new \GuzzleHttp\Client();
|
2023-04-11 02:17:16 +00:00
|
|
|
$res = $client->request('GET', $url, [
|
|
|
|
'timeout' => 1,
|
|
|
|
'connect_timeout' => 1,
|
|
|
|
]);
|
2023-04-10 23:45:04 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception('failed to connect to go-vod: '.$e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse body
|
2023-04-14 23:08:58 +00:00
|
|
|
$json = json_decode((string) $res->getBody(), true);
|
2023-04-10 23:45:04 +00:00
|
|
|
if (!$json) {
|
|
|
|
throw new \Exception('failed to parse go-vod response');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check version
|
|
|
|
$version = $json['version'];
|
|
|
|
$target = self::GOVOD_VER;
|
|
|
|
if (!version_compare($version, $target, '=')) {
|
|
|
|
throw new \Exception("version does not match {$version} <==> {$target}");
|
|
|
|
}
|
|
|
|
|
2023-04-18 21:02:34 +00:00
|
|
|
return $version;
|
2023-04-10 23:45:04 +00:00
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
/**
|
|
|
|
* POST a new configuration to go-vod.
|
|
|
|
*/
|
|
|
|
public static function configureGoVod(): bool
|
2023-04-10 23:45:04 +00:00
|
|
|
{
|
|
|
|
// Get config
|
|
|
|
$config = self::getGoVodConfig();
|
|
|
|
|
|
|
|
// Make request
|
|
|
|
$url = self::getGoVodUrl('config', '/config', 'config');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
$client->request('POST', $url, [
|
|
|
|
'json' => $config,
|
2023-04-11 02:17:16 +00:00
|
|
|
'timeout' => 1,
|
|
|
|
'connect_timeout' => 1,
|
2023-04-10 23:45:04 +00:00
|
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception('failed to connect to go-vod: '.$e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-14 09:07:18 +00:00
|
|
|
/**
|
|
|
|
* Detect the go-vod binary to use.
|
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public static function detectGoVod(): false|string
|
2023-04-10 21:32:35 +00:00
|
|
|
{
|
2023-10-22 19:58:33 +00:00
|
|
|
$goVodPath = SystemConfig::get('memories.vod.path');
|
2023-04-10 21:32:35 +00:00
|
|
|
|
|
|
|
if (empty($goVodPath) || !file_exists($goVodPath)) {
|
|
|
|
// Detect architecture
|
|
|
|
$arch = \OCA\Memories\Util::getArch();
|
2023-10-20 19:35:35 +00:00
|
|
|
$path = __DIR__."/../../bin-ext/go-vod-{$arch}";
|
2023-04-10 21:32:35 +00:00
|
|
|
$goVodPath = realpath($path);
|
|
|
|
|
|
|
|
if (!$goVodPath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set config
|
2023-10-22 19:58:33 +00:00
|
|
|
SystemConfig::set('memories.vod.path', $goVodPath);
|
2023-04-10 21:32:35 +00:00
|
|
|
|
|
|
|
// Make executable
|
|
|
|
if (!is_executable($goVodPath)) {
|
|
|
|
@chmod($goVodPath, 0755);
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-10 21:32:35 +00:00
|
|
|
|
|
|
|
return $goVodPath;
|
|
|
|
}
|
|
|
|
|
2023-10-14 23:06:25 +00:00
|
|
|
public static function detectFFmpeg(): ?string
|
2023-04-10 21:32:35 +00:00
|
|
|
{
|
2023-10-22 19:58:33 +00:00
|
|
|
$ffmpegPath = SystemConfig::get('memories.vod.ffmpeg');
|
|
|
|
$ffprobePath = SystemConfig::get('memories.vod.ffprobe');
|
2023-04-10 21:32:35 +00:00
|
|
|
|
|
|
|
if (empty($ffmpegPath) || !file_exists($ffmpegPath) || empty($ffprobePath) || !file_exists($ffprobePath)) {
|
2023-10-14 23:06:25 +00:00
|
|
|
// Use PATH environment variable to find ffmpeg
|
|
|
|
|
|
|
|
/** @psalm-suppress ForbiddenCode */
|
2023-04-10 21:32:35 +00:00
|
|
|
$ffmpegPath = shell_exec('which ffmpeg');
|
2023-10-14 23:06:25 +00:00
|
|
|
|
|
|
|
/** @psalm-suppress ForbiddenCode */
|
2023-04-10 21:32:35 +00:00
|
|
|
$ffprobePath = shell_exec('which ffprobe');
|
|
|
|
if (!$ffmpegPath || !$ffprobePath) {
|
2023-10-14 23:06:25 +00:00
|
|
|
return null;
|
2023-04-10 21:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trim
|
|
|
|
$ffmpegPath = trim($ffmpegPath);
|
|
|
|
$ffprobePath = trim($ffprobePath);
|
|
|
|
|
|
|
|
// Set config
|
2023-10-22 19:58:33 +00:00
|
|
|
SystemConfig::set('memories.vod.ffmpeg', $ffmpegPath);
|
|
|
|
SystemConfig::set('memories.vod.ffprobe', $ffprobePath);
|
2023-04-10 21:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if executable
|
|
|
|
if (!is_executable($ffmpegPath) || !is_executable($ffprobePath)) {
|
2023-10-14 23:06:25 +00:00
|
|
|
return null;
|
2023-04-10 21:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $ffmpegPath;
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
2023-04-18 21:02:34 +00:00
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
public static function testFFmpeg(string $path, string $name): string
|
2023-04-18 21:02:34 +00:00
|
|
|
{
|
2023-10-14 23:06:25 +00:00
|
|
|
/** @psalm-suppress ForbiddenCode */
|
2023-10-14 09:07:18 +00:00
|
|
|
$version = shell_exec("{$path} -version") ?: '';
|
2023-04-29 17:56:43 +00:00
|
|
|
if (!preg_match("/{$name} version \\S*/", $version, $matches)) {
|
2023-04-18 21:02:34 +00:00
|
|
|
throw new \Exception("failed to detect version, found {$version}");
|
|
|
|
}
|
|
|
|
|
2023-04-29 17:56:43 +00:00
|
|
|
return explode(' ', $matches[0])[2];
|
2023-04-18 21:02:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
public static function testSystemPerl(string $path): string
|
2023-04-18 21:02:34 +00:00
|
|
|
{
|
2023-10-14 23:06:25 +00:00
|
|
|
/** @psalm-suppress ForbiddenCode */
|
2023-04-18 21:02:34 +00:00
|
|
|
if (($out = shell_exec("{$path} -e 'print \"OK\";'")) !== 'OK') {
|
2023-10-14 23:06:25 +00:00
|
|
|
throw new \Exception('Failed to run test perl script: '.(string) $out);
|
2023-04-18 21:02:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-14 23:06:25 +00:00
|
|
|
/** @psalm-suppress ForbiddenCode */
|
2023-10-15 19:46:35 +00:00
|
|
|
return shell_exec("{$path} -e 'print $^V;'") ?: 'unknown version';
|
2023-04-18 21:02:34 +00:00
|
|
|
}
|
2023-10-20 19:42:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Kill all instances of a process by name.
|
|
|
|
* Similar to pkill, which may not be available on all systems.
|
|
|
|
*
|
|
|
|
* @param string $name Process name (only the first 12 characters are used)
|
|
|
|
*/
|
|
|
|
public static function pkill(string $name): void
|
|
|
|
{
|
|
|
|
// don't kill everything
|
|
|
|
if (empty($name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only use the first 12 characters
|
|
|
|
$name = substr($name, 0, 12);
|
|
|
|
|
|
|
|
// check if ps or busybox is available
|
|
|
|
$ps = 'ps';
|
|
|
|
|
|
|
|
/** @psalm-suppress ForbiddenCode */
|
|
|
|
if (!shell_exec('which ps')) {
|
|
|
|
if (!shell_exec('which busybox')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ps = 'busybox ps';
|
|
|
|
}
|
|
|
|
|
|
|
|
// get pids using ps as array
|
|
|
|
/** @psalm-suppress ForbiddenCode */
|
|
|
|
$pids = shell_exec("{$ps} -eao pid,comm | grep {$name} | awk '{print $1}'");
|
|
|
|
if (null === $pids || empty($pids)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$pids = array_filter(explode("\n", $pids));
|
|
|
|
|
|
|
|
// kill all pids
|
|
|
|
foreach ($pids as $pid) {
|
|
|
|
posix_kill((int) $pid, 9); // SIGKILL
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|