From 7dd744abeb183ab42ebdf3148edcd5b2e8bf2578 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Mon, 22 Oct 2018 15:24:40 +0200 Subject: [PATCH] Add mhor/php-mediainfo to manage mediainfo data --- Blacklight/ReleaseExtra.php | 373 ++++++------------ .../processing/post/ProcessAdditional.php | 22 +- composer.json | 1 + composer.lock | 48 ++- 4 files changed, 187 insertions(+), 257 deletions(-) diff --git a/Blacklight/ReleaseExtra.php b/Blacklight/ReleaseExtra.php index 76517b896..5b3263364 100755 --- a/Blacklight/ReleaseExtra.php +++ b/Blacklight/ReleaseExtra.php @@ -6,9 +6,9 @@ use App\Models\AudioData; use App\Models\VideoData; use App\Models\ReleaseUnique; use App\Models\ReleaseSubtitle; -use Blacklight\utility\Utility; use App\Models\ReleaseExtraFull; use Illuminate\Support\Facades\DB; +use Mhor\MediaInfo\Container\MediaInfoContainer; class ReleaseExtra { @@ -114,277 +114,153 @@ class ReleaseExtra } /** - * @param $releaseID - * @param $xml + * @param $releaseID + * @param \Mhor\MediaInfo\Container\MediaInfoContainer $arrXml */ - public function addFromXml($releaseID, $xml): void + public function addFromXml($releaseID, MediaInfoContainer $arrXml): void { - $xmlObj = @simplexml_load_string($xml); - $arrXml = Utility::objectsIntoArray($xmlObj); $containerFormat = ''; $overallBitRate = ''; + $general = $arrXml->getGeneral(); + $audios = $arrXml->getAudios(); + $videos = $arrXml->getVideos(); + $subtitles = $arrXml->getSubtitles(); - $mediaInfoVersion = 0; - if (\in_array('File', $arrXml, false) && \in_array('track', $arrXml['File'], false)) { - $mediaInfoVersion = 1; + if (! empty($general->get('unique_id')->getShortName()) && (int) $general->get('unique_id')->getShortName() !== 1) { + $uniqueId = $general->get('unique_id')->getShortName(); + $this->addUID($releaseID, $uniqueId); } - if (\in_array('@attributes', $arrXml, false) && \in_array('@version', $arrXml['@attributes'], false)) { - $mediaInfoVersion = (int) $arrXml['@attributes']['version']; + + if ($general->get('format') !== null) { + $containerFormat = $general->get('format')->getFullName; } - if ($mediaInfoVersion < 1) { - if (isset($arrXml['File']['track'])) { - $mediaInfoVersion = 1; + + if ($general->get('overall_bit_rate') !== null) { + $overallBitRate = $general->get('overall_bit_rate')->getFullName(); + } + + if ($general->get('overall_bit_rate') !== null) { + $overallBitRate = $general->get('overall_bit_rate')->getFullName(); + } + + $videoDuration = $videoFormat = $videoCodec = $videoWidth = $videoHeight = $videoAspect = $videoFrameRate = $videoLibrary = $videoBitRate = ''; + + foreach ($videos as $video) { + if ($video->get('duration') !== null) { + $videoDuration = $general->get('duration')->getMiliseconds(); } - if (isset($arrXml['@attributes']['version'])) { - $mediaInfoVersion = (int) $arrXml['@attributes']['version']; + + if ($video->get('format') !== null) { + $videoFormat = $video->get('format')->getFullName(); } + + if ($video->get('codec_id') !== null) { + $videoCodec = $video->get('codec_id'); + } + + if ($video->get('width') !== null) { + $videoWidth = $video->get('width')->getAbsoluteValue(); + } + + if ($video->get('height') !== null) { + $videoHeight = $video->get('height')->getAbsoluteValue(); + } + + if ($video->get('display_aspect_ratio') !== null) { + $videoAspect = $video->get('display_aspect_ratio')->getTextValue(); + } + + if ($video->get('frame_rate') !== null) { + $videoFrameRate = $video->get('frame_rate')->getTextValue(); + } + + if ($video->get('encoded_library') !== null) { + $videoLibrary = $video->get('encoded_library'); + } + + if ($video->get('encoded_library_version') !== null) { + $videoLibrary .= $video->get('encoded_library_version'); + } + + if ($video->get('writing_library') !== null) { + $videoLibrary = $video->get('writing_library')->getFullName(); + } + + if ($video->get('nominal_bit_rate') !== null) { + $videoBitRate = $video->get('nominal_bit_rate')->getTextValue(); + } + + if (! empty($videoBitRate)) { + $overallBitRate = $videoBitRate; + } + + $this->addVideo($releaseID, $containerFormat, $overallBitRate, $videoDuration, $videoFormat, $videoCodec, $videoWidth, $videoHeight, $videoAspect, $videoFrameRate, $videoLibrary); } - switch ($mediaInfoVersion) { + $audioID = 1; + $audioFormat = $audioMode = $audioBitRateMode = $audioBitRate = $audioChannels = $audioSampleRate = $audioLibrary = $audioLanguage = $audioTitle = ''; - /* - * MediaInfo Schema v1 (1.x - 7.99) - */ - case 1: - foreach ($arrXml['File']['track'] as $track) { - if (isset($track['@attributes']['type'])) { - if ($track['@attributes']['type'] === 'General') { - if (isset($track['Format'])) { - $containerFormat = $track['Format']; - } - if (isset($track['Overall_bit_rate'])) { - $overallBitRate = $track['Overall_bit_rate']; - } - if (isset($track['Unique_ID']) && preg_match('/(?P^\d+)/i', $track['Unique_ID'], $matches)) { - $uniqueId = $matches['uid']; - $this->addUID($releaseID, $uniqueId); - } - } elseif ($track['@attributes']['type'] === 'Video') { - $videoDuration = $videoFormat = $videoCodec = $videoWidth = $videoHeight = $videoAspect = $videoFrameRate = $videoLibrary = ''; - if (isset($track['Duration'])) { - $videoDuration = $track['Duration']; - } - if (isset($track['Format'])) { - $videoFormat = $track['Format']; - } - if (isset($track['Codec_ID'])) { - $videoCodec = $track['Codec_ID']; - } - if (isset($track['Width'])) { - $videoWidth = preg_replace('/\D/', '', $track['Width']); - } - if (isset($track['Height'])) { - $videoHeight = preg_replace('/\D/', '', $track['Height']); - } - if (isset($track['Display_aspect_ratio'])) { - $videoAspect = $track['Display_aspect_ratio']; - } - if (isset($track['Frame_rate'])) { - $videoFrameRate = str_replace(' fps', '', $track['Frame_rate']); - } - if (isset($track['Writing_library'])) { - $videoLibrary = $track['Writing_library']; - } - $this->addVideo($releaseID, $containerFormat, $overallBitRate, $videoDuration, $videoFormat, $videoCodec, $videoWidth, $videoHeight, $videoAspect, $videoFrameRate, $videoLibrary); - } elseif ($track['@attributes']['type'] === 'Audio') { - $audioID = 1; - $audioFormat = $audioMode = $audioBitRateMode = $audioBitRate = $audioChannels = $audioSampleRate = $audioLibrary = $audioLanguage = $audioTitle = ''; - if (isset($track['@attributes']['streamid'])) { - $audioID = $track['@attributes']['streamid']; - } - if (isset($track['Format'])) { - $audioFormat = $track['Format']; - } - if (! empty($track['Mode'])) { - $audioMode = $track['Mode']; - } - if (isset($track['Bit_rate_mode'])) { - $audioBitRateMode = $track['Bit_rate_mode']; - } - if (isset($track['Bit_rate'])) { - $audioBitRate = $track['Bit_rate']; - } - if (isset($track['Channel_s_'])) { - $audioChannels = $track['Channel_s_']; - } - if (isset($track['Sampling_rate'])) { - $audioSampleRate = $track['Sampling_rate']; - } - if (! empty($track['Writing_library'])) { - $audioLibrary = $track['Writing_library']; - } - if (! empty($track['Language'])) { - $audioLanguage = $track['Language']; - } - if (! empty($track['Title'])) { - $audioTitle = $track['Title']; - } - $this->addAudio($releaseID, $audioID, $audioFormat, $audioMode, $audioBitRateMode, $audioBitRate, $audioChannels, $audioSampleRate, $audioLibrary, $audioLanguage, $audioTitle); - } elseif ($track['@attributes']['type'] === 'Text') { - $subsID = 1; - $subsLanguage = 'Unknown'; - if (isset($track['@attributes']['streamid'])) { - $subsID = $track['@attributes']['streamid']; - } - if (isset($track['Language'])) { - $subsLanguage = $track['Language']; - } - $this->addSubs($releaseID, $subsID, $subsLanguage); - } - } - } - break; - case 2: - /* - * MediaInfo Schema v2 (mediaInfo version > 7.99) - */ - foreach ($arrXml['media']['track'] as $track) { - $type = ''; + foreach ($audios as $audio) { + if ($audio->get('id') !== null) { + $audioID = $audio->get('id')->getFullName(); + } - if (isset($track['@attributes']['type'])) { - $type = $track['@attributes']['type']; - } + if ($audio->get('format') !== null) { + $audioFormat = $audio->get('format')->getFullName(); + } - if (isset($track['type'])) { - $type = $track['type']; - } + if ($audio->get('mode') !== null) { + $audioMode = $audio->get('mode')->getFullName(); + } - if ($type === 'General') { - if (! empty($track['UniqueID']) && (int) $track['UniqueID'] !== 1) { - $uniqueId = $track['UniqueID']; - $this->addUID($releaseID, $uniqueId); - } + if ($audio->get('format_settings_sbr') !== null) { + $audioMode = $audio->get('format_settings_sbr')->getFullName(); + } - if (isset($track['Format'])) { - $containerFormat = $track['Format']; - } + if ($audio->get('bit_rate_mode') !== null) { + $audioBitRateMode = $audio->get('bit_rate_mode')->getFullName(); + } - if (isset($track['OverallBitRate_Nominal'])) { - $overallBitRate = $track['OverallBitRate_Nominal']; - } + if ($audio->get('bit_rate') !== null) { + $audioBitRate = $audio->get('bit_rate')->getTextValue(); + } - if (isset($track['OverallBitRate'])) { - $overallBitRate = $track['OverallBitRate']; - } - } elseif ($type === 'Video') { - $videoDuration = $videoFormat = $videoCodec = $videoWidth = $videoHeight = $videoAspect = $videoFrameRate = $videoLibrary = $videoBitRate = ''; + if ($audio->get('channel_s') !== null) { + $audioChannels = $audio->get('channel_s')->getAbsoluteValue(); + } - if (isset($track['Duration'])) { - $videoDuration = $track['Duration']; - } + if ($audio->get('sampling_rate') !== null) { + $audioSampleRate = $audio->get('sampling_rate')->getTextValue(); + } - if (isset($track['Format'])) { - $videoFormat = $track['Format']; - } + if ($audio->get('encoded_library') !== null) { + $audioLibrary = $audio->get('encoded_library'); + } - if (isset($track['CodecID'])) { - $videoCodec = $track['CodecID']; - } + if ($audio->get('language') !== null) { + $audioLanguage = $audio->get('language'); + } - if (isset($track['Width'])) { - $videoWidth = $track['Width']; - } + if ($audio->get('title') !== null) { + $audioTitle = $audio->get('title'); + } - if (isset($track['Height'])) { - $videoHeight = $track['Height']; - } + $this->addAudio($releaseID, $audioID, $audioFormat, $audioMode, $audioBitRateMode, $audioBitRate, $audioChannels, $audioSampleRate, $audioLibrary, $audioLanguage, $audioTitle); + } - if (isset($track['DisplayAspectRatio'])) { - $videoAspect = $track['DisplayAspectRatio']; - } + foreach ($subtitles as $subtitle) { + $subsID = 1; + $subsLanguage = 'Unknown'; - if (isset($track['FrameRate'])) { - $videoFrameRate = $track['FrameRate']; - } + if ($subtitle->get('id') !== null) { + $subsID = $subtitle->get('id')->getFullName(); + } - if (isset($track['Encoded_Library_Version'])) { - $videoLibrary = $track['Encoded_Library_Version']; - } + if ($subtitle->get('language') !== null) { + $subsLanguage = $subtitle->get('language'); + } - if (isset($track['Encoded_Library'])) { - $videoLibrary = $track['Encoded_Library']; - } - - if (isset($track['BitRate'])) { - $videoBitRate = $track['BitRate']; - } - - if (isset($track['BitRate_Nominal'])) { - $videoBitRate = $track['BitRate_Nominal']; - } - - if (! empty($videoBitRate)) { - $overallBitRate = $videoBitRate; - } - - $this->addVideo($releaseID, $containerFormat, $overallBitRate, $videoDuration, $videoFormat, $videoCodec, $videoWidth, $videoHeight, $videoAspect, $videoFrameRate, $videoLibrary); - } elseif ($type === 'Audio') { - $audioID = 1; - $audioFormat = $audioMode = $audioBitRateMode = $audioBitRate = $audioChannels = $audioSampleRate = $audioLibrary = $audioLanguage = $audioTitle = ''; - - if (isset($track['ID'])) { - $audioID = $track['ID']; - } - - if (isset($track['Format'])) { - $audioFormat = $track['Format']; - } - - if (! empty($track['Mode'])) { - $audioMode = $track['Mode']; - } - - if (! empty($track['Format_Settings_Mode'])) { - $audioMode = $track['Format_Settings_Mode']; - } - - if (isset($track['BitRate_Mode'])) { - $audioBitRateMode = $track['BitRate_Mode']; - } - - if (isset($track['BitRate'])) { - $audioBitRate = $track['BitRate']; - } - - if (isset($track['Channels'])) { - $audioChannels = $track['Channels']; - } - - if (isset($track['SamplingRate'])) { - $audioSampleRate = $track['SamplingRate']; - } - - if (! empty($track['Encoded_Library'])) { - $audioLibrary = $track['Encoded_Library']; - } - - if (! empty($track['Language'])) { - $audioLanguage = $track['Language']; - } - - if (! empty($track['Title'])) { - $audioTitle = $track['Title']; - } - - $this->addAudio($releaseID, $audioID, $audioFormat, $audioMode, $audioBitRateMode, $audioBitRate, $audioChannels, $audioSampleRate, $audioLibrary, $audioLanguage, $audioTitle); - } elseif ($type === 'Text') { - $subsID = 1; - $subsLanguage = 'Unknown'; - - if (isset($track['ID'])) { - $subsID = $track['ID']; - } - - if (isset($track['Language'])) { - $subsLanguage = $track['Language']; - } - - $this->addSubs($releaseID, $subsID, $subsLanguage); - } - } - break; - default: - break; + $this->addSubs($releaseID, $subsID, $subsLanguage); } } @@ -521,13 +397,14 @@ class ReleaseExtra } /** - * @param $id - * @param $xml + * @param $id + * @param \Mhor\MediaInfo\Container\MediaInfoContainer $xmlArray */ - public function addFull($id, $xml): void + public function addFull($id, MediaInfoContainer $xmlArray): void { $ckid = ReleaseExtraFull::query()->where('releases_id', $id)->first(); if ($ckid === null) { + $xml = $xmlArray->__toXML(); ReleaseExtraFull::query()->insert(['releases_id' => $id, 'mediainfo' => $xml]); } } diff --git a/Blacklight/processing/post/ProcessAdditional.php b/Blacklight/processing/post/ProcessAdditional.php index 3a660b283..341204d0b 100755 --- a/Blacklight/processing/post/ProcessAdditional.php +++ b/Blacklight/processing/post/ProcessAdditional.php @@ -29,6 +29,7 @@ use dariusiii\rarinfo\ArchiveInfo; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\File; use FFMpeg\Filters\Video\ResizeFilter; +use Mhor\MediaInfo\MediaInfo; class ProcessAdditional { @@ -389,6 +390,11 @@ class ProcessAdditional */ private $ffprobe; + /** + * @var \Mhor\MediaInfo\MediaInfo + */ + private $mediaInfo; + /** * ProcessAdditional constructor. * @@ -427,6 +433,9 @@ class ProcessAdditional $this->sphinx = $options['SphinxSearch'] instanceof SphinxSearch ? $options['SphinxSearch'] : new SphinxSearch(); $this->ffmpeg = FFMpeg::create(['timeout' => Settings::settingValue('..timeoutseconds')]); $this->ffprobe = FFProbe::create(); + $this->mediaInfo = new MediaInfo(); + $this->mediaInfo->setConfig('use_oldxml_mediainfo_output_format', true); + $this->mediaInfo->setConfig('command', Settings::settingValue('apps..mediainfopath')); $this->_innerFileBlacklist = Settings::settingValue('indexer.ppa.innerfileblacklist') === '' ? false : Settings::settingValue('indexer.ppa.innerfileblacklist'); $this->_maxNestedLevels = (int) Settings::settingValue('..maxnestedlevels') === 0 ? 3 : (int) Settings::settingValue('..maxnestedlevels'); @@ -1905,9 +1914,9 @@ class ProcessAdditional // Create the image. if ($this->ffprobe->isValid($fileLocation)) { - $video = $this->ffmpeg->open($fileLocation); - $sample = $video->frame(TimeCode::fromString($time === '' ? '00:00:03:00' : $time)); - $sample->save($fileName); + $this->ffmpeg->open($fileLocation) + ->frame(TimeCode::fromString($time === '' ? '00:00:03:00' : $time)) + ->save($fileName); } // Check if the file exists. @@ -2063,14 +2072,11 @@ class ProcessAdditional // Look for the video file. if (is_file($fileLocation)) { - // Run media info on it. - $xmlArray = runCmd( - $this->_killString.Settings::settingValue('apps..mediainfopath').'" --Output=XML "'.$fileLocation.'"' - ); + $xmlArray = $this->mediaInfo->getInfo($fileLocation, false); // Check if we got it. - if (! preg_match('//i', $xmlArray)) { + if ($xmlArray === null) { return false; } diff --git a/composer.json b/composer.json index 9e9869e3a..37494d264 100755 --- a/composer.json +++ b/composer.json @@ -154,6 +154,7 @@ "league/climate": "^3.4", "mayconbordin/l5-fixtures": "dev-master", "messerli90/igdb": "^1.0", + "mhor/php-mediainfo": "^4.1", "monolog/monolog": "^1.22", "nzedb/Git.php": "dev-master", "php-ffmpeg/php-ffmpeg": "^0.13.0", diff --git a/composer.lock b/composer.lock index bc989557b..925f789d4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fd5f689ffbd7233972d5253585e5f549", + "content-hash": "7c0f640d2c9872c181f6dd1581652905", "packages": [ { "name": "aharen/omdbapi", @@ -4639,6 +4639,52 @@ ], "time": "2017-08-06T17:14:20+00:00" }, + { + "name": "mhor/php-mediainfo", + "version": "4.1.2", + "source": { + "type": "git", + "url": "https://github.com/mhor/php-mediainfo.git", + "reference": "ebcf39d68d3b2c70a34798ea4132fe8b457ee295" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mhor/php-mediainfo/zipball/ebcf39d68d3b2c70a34798ea4132fe8b457ee295", + "reference": "ebcf39d68d3b2c70a34798ea4132fe8b457ee295", + "shasum": "" + }, + "require": { + "php": ">=5.6.0", + "symfony/filesystem": "~2.3|~3.0|~4.0", + "symfony/process": "~2.3|~3.0|~4.0" + }, + "require-dev": { + "phpunit/phpunit": "~5.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Mhor\\MediaInfo\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "mhor", + "email": "maxime.horcholle@gmail.com" + } + ], + "description": "PHP wrapper around the mediainfo command", + "time": "2018-07-03T21:29:10+00:00" + }, { "name": "monolog/monolog", "version": "1.23.0",