From a58d146badc3dc550bf5e2b70ba64f2255965b86 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sun, 15 Nov 2020 10:18:53 +0100 Subject: [PATCH] . --- .idea/workspace.xml | 30 ++-- scripts/cron/cron.php | 4 +- scripts/lib/medialibrary.php | 257 ++++++++++++++++++----------------- scripts/transcode.php | 2 +- 4 files changed, 147 insertions(+), 146 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b8c2fad..2c183c0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,8 +3,8 @@ - - + + @@ -1290,12 +1290,12 @@ - + - + @@ -1319,11 +1319,11 @@ - + - + diff --git a/scripts/cron/cron.php b/scripts/cron/cron.php index bfcd6e1..bb8cc4f 100644 --- a/scripts/cron/cron.php +++ b/scripts/cron/cron.php @@ -90,8 +90,8 @@ function cronEcoMode($cronmin) function cronTranscode($cronmin) { - //_logSection('Transcode'); - //httpRequest('https://salon.home.tortuga.enhydra.fr/scripts/transcode.php', 'get', [], null, 10, true); + _logSection('Transcode'); + httpRequest('https://salon.home.tortuga.enhydra.fr/scripts/transcode.php', 'get', [], null, 10, true); } function _logSection($t) diff --git a/scripts/lib/medialibrary.php b/scripts/lib/medialibrary.php index bbf1693..7b844ca 100644 --- a/scripts/lib/medialibrary.php +++ b/scripts/lib/medialibrary.php @@ -3,7 +3,7 @@ function getMediaContents($dir, $context, $seen = null) { - global $videoExt,$seen; + global $videoExt, $seen; $files = scandir($dir); $results = array(); @@ -18,7 +18,7 @@ function getMediaContents($dir, $context, $seen = null) continue; } $normPath = normPath($path); - $results[] = array('path' => $normPath, 'srt' => hasSRT($normPath), 'seen' => seen($normPath), 'dir' => false, 'name' => mediaName(implode(' ', $e), $context, false, $path)); + $results[] = array('path' => $normPath, 'x264path' => getX264Version($path, false), 'srt' => hasSRT($normPath), 'seen' => seen($normPath), 'dir' => false, 'name' => mediaName(implode(' ', $e), $context, false, $path)); } } else if ($value != "." && $value != ".." && $value != '@eaDir') { $files = getMediaContents($path, $context, $seen); @@ -29,7 +29,7 @@ function getMediaContents($dir, $context, $seen = null) $results[] = $files[0]; } else { $normPath = normPath($path); - $results[] = array('path' => $normPath, 'seen' => seen($normPath), 'dir' => true, 'id' => hash('sha256', $path), 'name' => mediaName($value, $context, true, $path), 'items' => $files); + $results[] = array('path' => $normPath, 'x264path' => getX264Version($path, false), 'seen' => seen($normPath), 'dir' => true, 'id' => hash('sha256', $path), 'name' => mediaName($value, $context, true, $path), 'items' => $files); } } } @@ -130,57 +130,58 @@ function mediaName($name, $context, $dir, $path) } return $name; } + // //// /volume1/@appstore/ffmpeg/bin/ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 /volume1/Share/Videos/Series/Westworld/S01/E01.mkv //// ////ffmpeg -i /volume1/Share/Videos/Séries/Westworld/S01/E01.mkv -c:v libx264 -crf 19 -preset slow -c:a aac -b:a 192k -ac 2 /volume1/Share/Videos/Séries/Westworld/S01/E01.x264.mkv // -//function transcodeOneVideo() -//{ -// if (getState('night') == '1') { -// return; -// } -// -// $list = getVideosList(true); -// -// foreach ($list as $item) { -// -// /** @var SplFileInfo $item */ -// $path = $item->getRealPath(); -// $to = preg_replace('/\.' . $item->getExtension() . '$/', '.x264.' . $item->getExtension(), $path); -// -// -// if (file_exists($to) && filesize($to) != 0 && videosHaveSameDuration($path, $to)) { -// continue; -// } else { -// echo $to . '/exists :' . file_exists($to) . '/size:' . filesize($to) . "\n\n"; -// } -// -// set_time_limit(0); -// ignore_user_abort(true); -// -// if (!getNightMode()) { -// wakeupPC('avion'); -// sleep(15); -// } -// -// $resize = getVideoHeight($path) > 1080 ? ' 1' : ' 0'; -// $cmd = 'C:\Scripts\convertvideo.bat "' . nasToAvion($item) . '" "' . nasToAvion($to) . '" ' . $item->getExtension() . ' ' . getVideoCodec($path) . $resize; -// print_r(sshCommand($cmd, 'avion')); -// return; -// } -// echo 'no file to transcode'; -//} -// -//function videosHaveSameDuration($v1, $v2, $tolerance = 10) -//{ -// $d1 = getVideoDuration($v1); -// $d2 = getVideoDuration($v2); -// $diff = abs($d1 - $d2); -// $res = $diff < $tolerance; -// echo $v1 . '/' . $v2 . '/' . $d1 . '/' . $d2 . '/' . $diff . '/' . $res . "\n"; -// return $res; -//} +function transcodeOneVideo() +{ + if (getState('night') == '1') { + return; + } + + $list = getVideosList(true); + + foreach ($list as $item) { + + /** @var SplFileInfo $item */ + $path = $item->getRealPath(); + $to = preg_replace('/\.' . $item->getExtension() . '$/', '.x264.' . $item->getExtension(), $path); + + + if (file_exists($to) && filesize($to) != 0 && videosHaveSameDuration($path, $to)) { + continue; + } else { + echo $to . '/exists :' . file_exists($to) . '/size:' . filesize($to) . "\n\n"; + } + + set_time_limit(0); + ignore_user_abort(true); + + if (!getNightMode()) { + wakeupPC('avion'); + sleep(15); + } + + $resize = getVideoHeight($path) > 1080 ? ' 1' : ' 0'; + $cmd = 'C:\Scripts\convertvideo.bat "' . nasToAvion($item) . '" "' . nasToAvion($to) . '" ' . $item->getExtension() . ' ' . getVideoCodec($path) . $resize; + print_r(sshCommand($cmd, 'avion')); + return; + } + echo 'no file to transcode'; +} + +function videosHaveSameDuration($v1, $v2, $tolerance = 10) +{ + $d1 = getVideoDuration($v1); + $d2 = getVideoDuration($v2); + $diff = abs($d1 - $d2); + $res = $diff < $tolerance; + echo $v1 . '/' . $v2 . '/' . $d1 . '/' . $d2 . '/' . $diff . '/' . $res . "\n"; + return $res; +} function nasToAvion($path) { @@ -214,82 +215,82 @@ class DirFilter extends RecursiveFilterIterator } } -//function getVideosList($shuffle = false) -//{ -// global $videoExt; -// $res = array(); -// $dirit = new RecursiveDirectoryIterator('/nas/Videos'); -// $filtered = new DirFilter($dirit, ['@eaDir']); -// $iterator = new RecursiveIteratorIterator($filtered, RecursiveIteratorIterator::CHILD_FIRST); -// -// $toTranscode = ['hevc', 'vc1']; -// -// foreach ($iterator as $f) { -// /** @var $f SplFileInfo */ -// if (!$f->isFile()) { -// continue; -// } -// if (!in_array($f->getExtension(), $videoExt)) { -// continue; -// } -// $path = $f->getRealPath(); -// -// $codec = getVideoCodec($path); -// -// if (!in_array($codec, $toTranscode)) { -// continue; -// } -// if (preg_match('/[\&\[\]\|]/', $f->getFilename(), $matches)) { -// continue; -// } -// $res[] = $f; -// } -// if ($shuffle) { -// shuffle($res); -// } -// return $res; -//} -// -//function getVideoCodec($path) -//{ -// $cacheKey = 'codec_3_' . md5($path . '-' . filemtime($path) . '-' . filesize($path)); -// $res = getState($cacheKey, null); -// if (null === $res) { -// $codecfile = $path . '.vc'; -// if (file_exists($codecfile) && filesize($codecfile) > 0) { -// $res = file_get_contents($codecfile); -// unlink($codecfile); -// } else if (stristr($path, '.x264.')) { -// $res = 'h264'; -// } else { -// $res = `ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$path"`; -// $res = trim($res); -// } -// echo $path . '::' . $res . "\n"; -// setState($cacheKey, $res); -// } -// return $res; -//} -// -//function getVideoDuration($path) -//{ -// $cacheKey = 'duration_4_' . md5($path . '-' . filemtime($path) . '-' . filesize($path)); -// $res = getState($cacheKey, null); -// if (null === $res) { -// $res = `ffprobe -i "$path" -show_entries format=duration -v quiet -of csv="p=0"`; -// setState($cacheKey, $res); -// } -// return (float)$res; -//} -// -//function getVideoHeight($path) -//{ -// $cacheKey = 'height_2_' . md5($path . '-' . filemtime($path)); -// $res = getState($cacheKey, null); -// if (null === $res) { -// $res = `ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=nw=1:nk=1 $path`; -// $res = trim($res); -// setState($cacheKey, $res); -// } -// return (int)$res; -//} \ No newline at end of file +function getVideosList($shuffle = false) +{ + global $videoExt; + $res = array(); + $dirit = new RecursiveDirectoryIterator('/nas/Videos'); + $filtered = new DirFilter($dirit, ['@eaDir']); + $iterator = new RecursiveIteratorIterator($filtered, RecursiveIteratorIterator::CHILD_FIRST); + + $toTranscode = ['hevc', 'vc1', 'xvid']; + + foreach ($iterator as $f) { + /** @var $f SplFileInfo */ + if (!$f->isFile()) { + continue; + } + if (!in_array($f->getExtension(), $videoExt)) { + continue; + } + $path = $f->getRealPath(); + + $codec = getVideoCodec($path); + + if (!in_array($codec, $toTranscode)) { + continue; + } + if (preg_match('/[\&\[\]\|]/', $f->getFilename(), $matches)) { + continue; + } + $res[] = $f; + } + if ($shuffle) { + shuffle($res); + } + return $res; +} + +function getVideoCodec($path) +{ + $cacheKey = 'codec_3_' . md5($path . '-' . filemtime($path) . '-' . filesize($path)); + $res = getState($cacheKey, null); + if (null === $res) { + $codecfile = $path . '.vc'; + if (file_exists($codecfile) && filesize($codecfile) > 0) { + $res = file_get_contents($codecfile); + unlink($codecfile); + } else if (stristr($path, '.x264.')) { + $res = 'h264'; + } else { + $res = `ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$path"`; + $res = trim($res); + } + echo $path . '::' . $res . "\n"; + setState($cacheKey, $res); + } + return $res; +} + +function getVideoDuration($path) +{ + $cacheKey = 'duration_4_' . md5($path . '-' . filemtime($path) . '-' . filesize($path)); + $res = getState($cacheKey, null); + if (null === $res) { + $res = `ffprobe -i "$path" -show_entries format=duration -v quiet -of csv="p=0"`; + setState($cacheKey, $res); + } + return (float)$res; +} + +function getVideoHeight($path) +{ + $cacheKey = 'height_2_' . md5($path . '-' . filemtime($path)); + $res = getState($cacheKey, null); + if (null === $res) { + $res = `ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=nw=1:nk=1 $path`; + $res = trim($res); + setState($cacheKey, $res); + } + return (int)$res; +} \ No newline at end of file diff --git a/scripts/transcode.php b/scripts/transcode.php index cb1d0af..1f958ed 100644 --- a/scripts/transcode.php +++ b/scripts/transcode.php @@ -1,4 +1,4 @@