From 3dd7b12689c31d0055cd83344c5f739eafcfa362 Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Fri, 22 Jan 2016 13:33:55 +0530 Subject: [PATCH 01/12] Set bit rate properly By default the quality is set 4, due to this bit rate is not set properly. --- src/PHPVideoToolkit/VideoFormat.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PHPVideoToolkit/VideoFormat.php b/src/PHPVideoToolkit/VideoFormat.php index d508762..bd1d72b 100644 --- a/src/PHPVideoToolkit/VideoFormat.php +++ b/src/PHPVideoToolkit/VideoFormat.php @@ -605,6 +605,7 @@ public function setVideoBitrate($bitrate) } $this->_format['video_bitrate'] = $bitrate; + $this->setQualityVsStreamabilityBalanceRatio(NULL); return $this; //throw new Exception('Unrecognised video bitrate "'.$bitrate.'" set in \\PHPVideoToolkit\\'.get_class($this).'::setVideoBitrate'); From 4823811ca939c87b1800bbb805946d2d2f736793 Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Fri, 22 Jan 2016 13:35:40 +0530 Subject: [PATCH 02/12] Set bit rate properly By default the quality is set 4, due to this bit rate is not set properly. --- src/PHPVideoToolkit/AudioFormat.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PHPVideoToolkit/AudioFormat.php b/src/PHPVideoToolkit/AudioFormat.php index d80c91c..8743c18 100644 --- a/src/PHPVideoToolkit/AudioFormat.php +++ b/src/PHPVideoToolkit/AudioFormat.php @@ -270,6 +270,7 @@ public function setAudioBitrate($bitrate) } $this->_format['audio_bitrate'] = $bitrate; + $this->setQualityVsStreamabilityBalanceRatio(NULL); return $this; } From 1ee761485a64b70db588583b8cd3a5652401f5dd Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Fri, 22 Jan 2016 18:41:17 +0530 Subject: [PATCH 03/12] Major bugfix (correct codecs never applied) Correct audio codecs were never applied. For example, AudioFormat_Mkv sets audioCodec to libvorbis, but instead, setAudioCodec function instead sets codec as vorbis. Found the bug when working with mono files. --- src/PHPVideoToolkit/AudioFormat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHPVideoToolkit/AudioFormat.php b/src/PHPVideoToolkit/AudioFormat.php index d80c91c..9ddbd33 100644 --- a/src/PHPVideoToolkit/AudioFormat.php +++ b/src/PHPVideoToolkit/AudioFormat.php @@ -211,7 +211,7 @@ public function setAudioCodec($audio_codec) if($codecs_in_preference_order !== false){ $audio_codec = array_shift($codecs_in_preference_order); while(in_array($audio_codec, $codecs) === false && count($codecs_in_preference_order) > 0){ - $audio_codec = array_shift($codecs_in_preference_order); + array_push($codecs, $audio_codec); } } } From a2c5df735c8425700b5b39e78c40dae7107164bb Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Fri, 22 Jan 2016 19:41:37 +0530 Subject: [PATCH 04/12] Update AudioFormat.php Corrected typo --- src/PHPVideoToolkit/AudioFormat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHPVideoToolkit/AudioFormat.php b/src/PHPVideoToolkit/AudioFormat.php index d80c91c..d284be3 100644 --- a/src/PHPVideoToolkit/AudioFormat.php +++ b/src/PHPVideoToolkit/AudioFormat.php @@ -396,7 +396,7 @@ public function setAudioQuality($quality) } else if (is_int($quality) === false && is_float($quality) === false) { - throw new \InvalidArgumentException('The volume value must be an integer or float value.'); + throw new \InvalidArgumentException('Audio quality value must be an integer or float value.'); } // interpret quality into ffmpeg value From d4893b46655b613390648591bdc7659feba73e4d Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Sat, 23 Jan 2016 15:08:28 +0530 Subject: [PATCH 05/12] Get file volume information Added getFileVolumeComponent() function and relevant code to get mean and max volume of file. --- src/PHPVideoToolkit/MediaParser.php | 65 +++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/src/PHPVideoToolkit/MediaParser.php b/src/PHPVideoToolkit/MediaParser.php index e02e40f..a8497fa 100644 --- a/src/PHPVideoToolkit/MediaParser.php +++ b/src/PHPVideoToolkit/MediaParser.php @@ -57,6 +57,7 @@ public function getFileInformation($file_path, $read_from_cache=true) 'container' => $this->getFileContainerFormat($file_path, $read_from_cache), 'duration' => $this->getFileDuration($file_path, $read_from_cache), 'bitrate' => $this->getFileBitrate($file_path, $read_from_cache), + 'volume' => $this->getFileVolume($file_path, $read_from_cache), 'start' => $this->getFileStart($file_path, $read_from_cache), 'video' => $this->getFileVideoComponent($file_path, $read_from_cache), 'audio' => $this->getFileAudioComponent($file_path, $read_from_cache), @@ -249,6 +250,55 @@ public function getFileType($file_path, $read_from_cache=true) $this->_cacheSet($cache_key, $data); return $data; } + + /** + * Returns the files mean volume and max volume if available, otherwise returns null. + * + * @access public + * @author Samar Rizvi + * @param string $file_path + * @param boolean $read_from_cache + * @return mixed Returns an array of found data, otherwise returns null. + */ + public function getFileVolumeComponent($file_path, $read_from_cache=true) + { + $cache_key = 'media_parser/'.md5(realpath($file_path)).'_parsed_volume'; + if($read_from_cache === true && ($data = $this->_cacheGet($cache_key, -1)) !== -1) + { + return $data; + } + +// get the raw data + $raw_data = $this->getFileRawInformation($file_path, $read_from_cache); + +// grab the volume + $data = null; + + if(preg_match_all ('/^.*(mean_volume)(:)(\s+)([+-]?\d*\.\d+)(?![-+0-9\.])( )(dB)/im', $raw_data, $matches) > 0) + { + $data = array(); + + $float1=$matches[4][0]; + $word_unit=$matches[6][0]; + + $data['mean_volume'] = $float1 . $word_unit; + } + + if(preg_match_all ('/^.*(max_volume)(:)(\s+)([+-]?\d*\.\d+)(?![-+0-9\.])( )(dB)/im', $raw_data, $matches) > 0) + { + if(!is_array($data)) { + $data = array(); + } + + $float1=$matches[4][0]; + $word_unit=$matches[6][0]; + + $data['max_volume'] = $float1 . $word_unit; + } + + $this->_cacheSet($cache_key, $data); + return $data; + } /** * Returns any video information about the file if available. @@ -687,11 +737,20 @@ public function getFileRawInformation($file_path, $read_from_cache=true) return $data; } + $isWindowsPlatform = defined('PHP_WINDOWS_VERSION_BUILD'); + // execute the ffmpeg lookup $exec = new FfmpegProcess('ffmpeg', $this->_config); - $raw_data = $exec->setInputPath($real_file_path) - ->execute() - ->getBuffer(); + $exec_cmd = $exec->setInputPath($real_file_path) + ->addCommand('-af', 'volumedetect') + ->addCommand('-f', 'NULL'); + + if($isWindowsPlatform) { + $exec_cmd = $exec_cmd->addCommand('NUL'); + } + + $raw_data = $exec_cmd->execute() + ->getBuffer(); // check the process for any errors. if($exec->hasError() === true && ($last_line = $exec->getLastLine()) !== 'At least one output file must be specified') From e60ee6e8549664ccf4e522aa5c34d61aba4cdb3b Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Sat, 23 Jan 2016 15:12:33 +0530 Subject: [PATCH 06/12] Read Volume Component Added function readVolumeComponent() to read volume component. --- src/PHPVideoToolkit/Media.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/PHPVideoToolkit/Media.php b/src/PHPVideoToolkit/Media.php index c035889..b4ae1d9 100644 --- a/src/PHPVideoToolkit/Media.php +++ b/src/PHPVideoToolkit/Media.php @@ -1260,6 +1260,19 @@ public function readVideoComponent($read_from_cache=true) { return parent::getFileVideoComponent($this->_media_file_path, $read_from_cache); } + + /** + * Returns mean and max volume information of the file. + * + * @access public + * @author Samar Rizvi + * @param boolean $read_from_cache + * @return mixed Returns an array of found data, otherwise returns null. + */ + public function readVolumeComponent($read_from_cache=true) + { + return parent::getFileVolumeComponent($this->_media_file_path, $read_from_cache); + } /** * Returns any audio information about the file if available. From 1c73f2667a1c7de527c48a411bca9414f4509f93 Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Sat, 23 Jan 2016 17:01:18 +0530 Subject: [PATCH 07/12] Update MediaParser.php Corrected typo --- src/PHPVideoToolkit/MediaParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHPVideoToolkit/MediaParser.php b/src/PHPVideoToolkit/MediaParser.php index a8497fa..61065a7 100644 --- a/src/PHPVideoToolkit/MediaParser.php +++ b/src/PHPVideoToolkit/MediaParser.php @@ -57,7 +57,7 @@ public function getFileInformation($file_path, $read_from_cache=true) 'container' => $this->getFileContainerFormat($file_path, $read_from_cache), 'duration' => $this->getFileDuration($file_path, $read_from_cache), 'bitrate' => $this->getFileBitrate($file_path, $read_from_cache), - 'volume' => $this->getFileVolume($file_path, $read_from_cache), + 'volume' => $this->getFileVolumeComponent($file_path, $read_from_cache), 'start' => $this->getFileStart($file_path, $read_from_cache), 'video' => $this->getFileVideoComponent($file_path, $read_from_cache), 'audio' => $this->getFileAudioComponent($file_path, $read_from_cache), From 793d47e571c431c4468a522bfdd5bc046138e7f0 Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Sat, 23 Jan 2016 18:11:52 +0530 Subject: [PATCH 08/12] Correct volume validation Volume can end in 'dB', and can be float as well as integer. --- src/PHPVideoToolkit/AudioFormat.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/PHPVideoToolkit/AudioFormat.php b/src/PHPVideoToolkit/AudioFormat.php index d80c91c..7195e4a 100644 --- a/src/PHPVideoToolkit/AudioFormat.php +++ b/src/PHPVideoToolkit/AudioFormat.php @@ -351,7 +351,7 @@ public function setAudioChannels($channels) * @author Oliver Lillie * @param integer $volume The level of the volumn. Must be higher than or euqal to 0. * @return PHPVideoToolkit\AudioFormat Returns the current object. - * @throws \InvalidArgumentException If $volume value is not an integer. + * @throws \InvalidArgumentException If $volume value neither ends in 'dB' nor is an integer or float. * @throws \InvalidArgumentException If $volume is less than 0. */ public function setVolume($volume) @@ -362,11 +362,13 @@ public function setVolume($volume) return $this; } - if(is_int($volume) === false) + //Volume can also end in dB, and can be float as well as integer + if(preg_match('/db$/i', $volume) === false && is_numeric($volume) === false) { - throw new \InvalidArgumentException('The volumne value must be an integer.'); + throw new \InvalidArgumentException('The volume value must be an integer or float or end in "dB".'); } - else if($volume < 0) + //Make sure that volume is not less than 0 even if it ends in dB + else if(preg_replace('/db$/i',"",$volume) < 0) { throw new \InvalidArgumentException('Unrecognised volume value "'.$volume.'" set in \\PHPVideoToolkit\\'.get_class($this).'::setVolume. The value must be higher than or equal to 0.'); } From a607d75ed7392cdf86beb03fa9dadb5a9d0d0721 Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Mon, 8 Feb 2016 15:59:55 +0530 Subject: [PATCH 09/12] Update ExecBuffer.php Corrected typo --- src/PHPVideoToolkit/ExecBuffer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHPVideoToolkit/ExecBuffer.php b/src/PHPVideoToolkit/ExecBuffer.php index 7d1d798..912a759 100644 --- a/src/PHPVideoToolkit/ExecBuffer.php +++ b/src/PHPVideoToolkit/ExecBuffer.php @@ -944,7 +944,7 @@ public function setBufferOutput($buffer_output) { if(in_array($buffer_output, array(null, self::DEV_NULL, self::TEMP)) === false) { - $dir = dirname($buffer_ouput); + $dir = dirname($buffer_output); if(is_dir($dir) === false) { throw new \InvalidArgumentException('Buffer output parent directory "'.$dir.'" is not a directory.'); From ead91a44d957309bd0f2f2af45c038cef33c772b Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Mon, 8 Feb 2016 16:15:26 +0530 Subject: [PATCH 10/12] Fixed bug (unable to get volume on linux systems) No mean and max volume components were returned for linux systems. Now, it works fine. --- src/PHPVideoToolkit/MediaParser.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/PHPVideoToolkit/MediaParser.php b/src/PHPVideoToolkit/MediaParser.php index 61065a7..a494866 100644 --- a/src/PHPVideoToolkit/MediaParser.php +++ b/src/PHPVideoToolkit/MediaParser.php @@ -747,6 +747,8 @@ public function getFileRawInformation($file_path, $read_from_cache=true) if($isWindowsPlatform) { $exec_cmd = $exec_cmd->addCommand('NUL'); + } else { + $exec_cmd = $exec_cmd->addCommand('/dev/null'); } $raw_data = $exec_cmd->execute() From af8f816c48afdfa4a159f4a76514e640df378de6 Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Mon, 8 Feb 2016 16:20:14 +0530 Subject: [PATCH 11/12] Fixed FFmpeg error Fixed bug when Ffmpeg returns error **No such filter: '"volume'** on many systems due volume being enclosed in inverted commas. --- src/PHPVideoToolkit/AudioFormat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHPVideoToolkit/AudioFormat.php b/src/PHPVideoToolkit/AudioFormat.php index d80c91c..4c67582 100644 --- a/src/PHPVideoToolkit/AudioFormat.php +++ b/src/PHPVideoToolkit/AudioFormat.php @@ -71,7 +71,7 @@ public function __construct($input_output_type=Format::OUTPUT, Config $config=nu 'input' => '-request_channels ', 'output' => '-ac ', ), - 'audio_volume' => '-af "volume="', + 'audio_volume' => '-af volume=', )); $this->_restricted_audio_bitrates = null; From 70e2a5a84787145c575a0d2bbaebe31746ed73f5 Mon Sep 17 00:00:00 2001 From: Samar Rizvi Date: Mon, 8 Feb 2016 16:32:38 +0530 Subject: [PATCH 12/12] Corrected Volume Unit if volume ends with 'db' change it to 'dB', else ffmpeg may produce error in some cases. --- src/PHPVideoToolkit/AudioFormat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHPVideoToolkit/AudioFormat.php b/src/PHPVideoToolkit/AudioFormat.php index 7195e4a..ff670a8 100644 --- a/src/PHPVideoToolkit/AudioFormat.php +++ b/src/PHPVideoToolkit/AudioFormat.php @@ -373,7 +373,7 @@ public function setVolume($volume) throw new \InvalidArgumentException('Unrecognised volume value "'.$volume.'" set in \\PHPVideoToolkit\\'.get_class($this).'::setVolume. The value must be higher than or equal to 0.'); } - $this->_format['audio_volume'] = $volume; + $this->_format['audio_volume'] = str_replace('db', 'dB', $volume); return $this; }