Skip to content

Commit 6902088

Browse files
committed
Merge pull request #82 from SamarRizvi/master
[Enhancement] Get file volume component
2 parents a9c1601 + 6d30071 commit 6902088

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

src/PHPVideoToolkit/AudioFormat.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __construct($input_output_type=Format::OUTPUT, Config $config=nu
7171
'input' => '-request_channels <setting>',
7272
'output' => '-ac <setting>',
7373
),
74-
'audio_volume' => '-af "volume=<setting>"',
74+
'audio_volume' => '-af volume=<setting>',
7575
));
7676

7777
$this->_restricted_audio_bitrates = null;
@@ -352,7 +352,7 @@ public function setAudioChannels($channels)
352352
* @author Oliver Lillie
353353
* @param integer $volume The level of the volumn. Must be higher than or euqal to 0.
354354
* @return PHPVideoToolkit\AudioFormat Returns the current object.
355-
* @throws \InvalidArgumentException If $volume value is not an integer.
355+
* @throws \InvalidArgumentException If $volume value neither ends in 'dB' nor is an integer or float.
356356
* @throws \InvalidArgumentException If $volume is less than 0.
357357
*/
358358
public function setVolume($volume)
@@ -363,16 +363,18 @@ public function setVolume($volume)
363363
return $this;
364364
}
365365

366-
if(is_int($volume) === false)
366+
//Volume can also end in dB, and can be float as well as integer
367+
if(preg_match('/db$/i', $volume) === false && is_numeric($volume) === false)
367368
{
368-
throw new \InvalidArgumentException('The volumne value must be an integer.');
369+
throw new \InvalidArgumentException('The volume value must be an integer or float or end in "dB".');
369370
}
370-
else if($volume < 0)
371+
//Make sure that volume is not less than 0 even if it ends in dB
372+
else if(preg_replace('/db$/i',"",$volume) < 0)
371373
{
372374
throw new \InvalidArgumentException('Unrecognised volume value "'.$volume.'" set in \\PHPVideoToolkit\\'.get_class($this).'::setVolume. The value must be higher than or equal to 0.');
373375
}
374376

375-
$this->_format['audio_volume'] = $volume;
377+
$this->_format['audio_volume'] = str_replace('db', 'dB', $volume);
376378
return $this;
377379
}
378380

src/PHPVideoToolkit/ExecBuffer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ public function setBufferOutput($buffer_output)
944944
{
945945
if(in_array($buffer_output, array(null, self::DEV_NULL, self::TEMP)) === false)
946946
{
947-
$dir = dirname($buffer_ouput);
947+
$dir = dirname($buffer_output);
948948
if(is_dir($dir) === false)
949949
{
950950
throw new \InvalidArgumentException('Buffer output parent directory "'.$dir.'" is not a directory.');

src/PHPVideoToolkit/Media.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,19 @@ public function readVideoComponent($read_from_cache=true)
12601260
{
12611261
return parent::getFileVideoComponent($this->_media_file_path, $read_from_cache);
12621262
}
1263+
1264+
/**
1265+
* Returns mean and max volume information of the file.
1266+
*
1267+
* @access public
1268+
* @author Samar Rizvi
1269+
* @param boolean $read_from_cache
1270+
* @return mixed Returns an array of found data, otherwise returns null.
1271+
*/
1272+
public function readVolumeComponent($read_from_cache=true)
1273+
{
1274+
return parent::getFileVolumeComponent($this->_media_file_path, $read_from_cache);
1275+
}
12631276

12641277
/**
12651278
* Returns any audio information about the file if available.

src/PHPVideoToolkit/MediaParser.php

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function getFileInformation($file_path, $read_from_cache=true)
5757
'container' => $this->getFileContainerFormat($file_path, $read_from_cache),
5858
'duration' => $this->getFileDuration($file_path, $read_from_cache),
5959
'bitrate' => $this->getFileBitrate($file_path, $read_from_cache),
60+
'volume' => $this->getFileVolumeComponent($file_path, $read_from_cache),
6061
'start' => $this->getFileStart($file_path, $read_from_cache),
6162
'video' => $this->getFileVideoComponent($file_path, $read_from_cache),
6263
'audio' => $this->getFileAudioComponent($file_path, $read_from_cache),
@@ -249,6 +250,55 @@ public function getFileType($file_path, $read_from_cache=true)
249250
$this->_cacheSet($cache_key, $data);
250251
return $data;
251252
}
253+
254+
/**
255+
* Returns the files mean volume and max volume if available, otherwise returns null.
256+
*
257+
* @access public
258+
* @author Samar Rizvi
259+
* @param string $file_path
260+
* @param boolean $read_from_cache
261+
* @return mixed Returns an array of found data, otherwise returns null.
262+
*/
263+
public function getFileVolumeComponent($file_path, $read_from_cache=true)
264+
{
265+
$cache_key = 'media_parser/'.md5(realpath($file_path)).'_parsed_volume';
266+
if($read_from_cache === true && ($data = $this->_cacheGet($cache_key, -1)) !== -1)
267+
{
268+
return $data;
269+
}
270+
271+
// get the raw data
272+
$raw_data = $this->getFileRawInformation($file_path, $read_from_cache);
273+
274+
// grab the volume
275+
$data = null;
276+
277+
if(preg_match_all ('/^.*(mean_volume)(:)(\s+)([+-]?\d*\.\d+)(?![-+0-9\.])( )(dB)/im', $raw_data, $matches) > 0)
278+
{
279+
$data = array();
280+
281+
$float1=$matches[4][0];
282+
$word_unit=$matches[6][0];
283+
284+
$data['mean_volume'] = $float1 . $word_unit;
285+
}
286+
287+
if(preg_match_all ('/^.*(max_volume)(:)(\s+)([+-]?\d*\.\d+)(?![-+0-9\.])( )(dB)/im', $raw_data, $matches) > 0)
288+
{
289+
if(!is_array($data)) {
290+
$data = array();
291+
}
292+
293+
$float1=$matches[4][0];
294+
$word_unit=$matches[6][0];
295+
296+
$data['max_volume'] = $float1 . $word_unit;
297+
}
298+
299+
$this->_cacheSet($cache_key, $data);
300+
return $data;
301+
}
252302

253303
/**
254304
* Returns any video information about the file if available.
@@ -687,11 +737,22 @@ public function getFileRawInformation($file_path, $read_from_cache=true)
687737
return $data;
688738
}
689739

740+
$isWindowsPlatform = defined('PHP_WINDOWS_VERSION_BUILD');
741+
690742
// execute the ffmpeg lookup
691743
$exec = new FfmpegProcess('ffmpeg', $this->_config);
692-
$raw_data = $exec->setInputPath($real_file_path)
693-
->execute()
694-
->getBuffer();
744+
$exec_cmd = $exec->setInputPath($real_file_path)
745+
->addCommand('-af', 'volumedetect')
746+
->addCommand('-f', 'NULL');
747+
748+
if($isWindowsPlatform) {
749+
$exec_cmd = $exec_cmd->addCommand('NUL');
750+
} else {
751+
$exec_cmd = $exec_cmd->addCommand('/dev/null');
752+
}
753+
754+
$raw_data = $exec_cmd->execute()
755+
->getBuffer();
695756

696757
// check the process for any errors.
697758
if($exec->hasError() === true && ($last_line = $exec->getLastLine()) !== 'At least one output file must be specified')

0 commit comments

Comments
 (0)