Created
August 9, 2018 05:32
-
-
Save anuj9196/0ef9ab697825468d55d374f2b5869b81 to your computer and use it in GitHub Desktop.
Check music duration and create music loop in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* prepare music loop | |
* | |
* requires: james-heinrich/getid3 package | |
* install using composer: | |
* `composer require james-heinrich/getid3` | |
* | |
* @param $music_path, Local Music file absolute path | |
* @param $total_media_count, Number of Images/Videos to add in the video | |
* @param $each_media_delay, Delay duration for each Image/Video | |
* @return mixed, New looped music file path | |
* @throws Exception, In case of eny error, error message is thrown as exception | |
* @internal param $count | |
* @internal param $delay | |
*/ | |
private function _prepareMusicLoop($music_path, $total_media_count, $each_media_delay) | |
{ | |
if (!is_file($music_path)) { | |
throw new Exception('File not found'); | |
} | |
// calculate total duration for music to produce | |
$total_music_duration = $total_media_count * $each_media_delay; | |
// add duration of one slide extra for safe side | |
$total_music_duration += $each_media_delay; | |
// check length of music file | |
// $musicFile = new MusicFile($music_path); | |
// $duration = $musicFile->getDurationEstimate(); | |
// $duration = $musicFile->getDuration(); | |
$getID3 = new \getID3; | |
$file = $getID3->analyze($music_path); | |
$duration = $file['playtime_seconds']; | |
if ($duration === null) | |
return $music_path; | |
$this->info(sprintf( | |
'%s :::: Original duration of music file: %s seconds', | |
date('d-m-Y H:i:s'), | |
$duration | |
)); | |
$this->info(sprintf( | |
'%s :::: Music duration to produce: %s seconds', | |
date('d-m-Y H:i:s'), | |
$total_music_duration | |
)); | |
if ($total_music_duration <= $duration) { | |
$this->info(sprintf( | |
'%s :::: Music duration is more than required. Skipping', | |
date('d-m-Y H:i:s') | |
)); | |
return $music_path; | |
} | |
$this->info(sprintf( | |
'%s :::: Music duration is less than required. Looping music to achieve required duration', | |
date('d-m-Y H:i:s') | |
)); | |
$musicFile = new MusicLoop($music_path); | |
try { | |
$new_music_file = $musicFile->loop($duration, $total_music_duration); | |
$this->success(sprintf( | |
'%s :::: Music file prepared: %s', | |
date('d-m-Y H:i:s'), | |
$new_music_file | |
)); | |
$getID32 = new \getID3; | |
$file = $getID32->analyze($new_music_file); | |
$this->info(sprintf( | |
'%s :::: Duration of new music file: %s', | |
date('d-m-Y H:i:s'), | |
$file['playtime_seconds'] | |
)); | |
} catch (Exception $e) { | |
$this->err(sprintf( | |
'%s :::: Error looping music file: %s', | |
date('d-m-Y H:i:s'), | |
$e->getMessage() | |
)); | |
return $music_path; | |
} | |
return $new_music_file; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment