| 
									
										
										
										
											2016-11-15 16:54:46 -03:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * This file is part of PHP-FFmpeg. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * (c) Alchemy <info@alchemy.fr> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * For the full copyright and license information, please view the LICENSE | 
					
						
							|  |  |  |  * file that was distributed with this source code. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FFMpeg\Media; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Alchemy\BinaryDriver\Exception\ExecutionFailureException; | 
					
						
							|  |  |  | use FFMpeg\Filters\Waveform\WaveformFilterInterface; | 
					
						
							|  |  |  | use FFMpeg\Filters\Waveform\WaveformFilters; | 
					
						
							|  |  |  | use FFMpeg\Driver\FFMpegDriver; | 
					
						
							|  |  |  | use FFMpeg\FFProbe; | 
					
						
							|  |  |  | use FFMpeg\Exception\RuntimeException; | 
					
						
							|  |  |  | use FFMpeg\Coordinate\TimeCode; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Waveform extends AbstractMediaType | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** @var Video */ | 
					
						
							|  |  |  |     private $video; | 
					
						
							| 
									
										
										
										
											2016-11-16 12:43:40 -03:00
										 |  |  |     private $width; | 
					
						
							|  |  |  |     private $height; | 
					
						
							| 
									
										
										
										
											2016-11-15 16:54:46 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-16 12:43:40 -03:00
										 |  |  |     public function __construct(Video $video, FFMpegDriver $driver, FFProbe $ffprobe, $width, $height) | 
					
						
							| 
									
										
										
										
											2016-11-15 16:54:46 -03:00
										 |  |  |     { | 
					
						
							|  |  |  |         parent::__construct($video->getPathfile(), $driver, $ffprobe); | 
					
						
							|  |  |  |         $this->video = $video; | 
					
						
							| 
									
										
										
										
											2016-11-16 12:43:40 -03:00
										 |  |  |         $this->width = $width; | 
					
						
							|  |  |  |         $this->height = $height; | 
					
						
							| 
									
										
										
										
											2016-11-15 16:54:46 -03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns the video related to the waveform. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Video | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getVideo() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->video; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * {@inheritdoc} | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return WaveformFilters | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function filters() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new WaveformFilters($this); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * {@inheritdoc} | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Waveform | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function addFilter(WaveformFilterInterface $filter) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->filters->add($filter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Saves the waveform in the given filename. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string  $pathfile | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Waveform | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws RuntimeException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function save($pathfile) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * might be optimized with http://ffmpeg.org/trac/ffmpeg/wiki/Seeking%20with%20FFmpeg | 
					
						
							|  |  |  |          * @see http://ffmpeg.org/ffmpeg.html#Main-options
 | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         $commands = array( | 
					
						
							|  |  |  |             '-i', 'input', '-filter_complex', | 
					
						
							| 
									
										
										
										
											2016-11-16 12:43:40 -03:00
										 |  |  |             'showwavespic=s='.$this->width.'x'.$this->height, | 
					
						
							| 
									
										
										
										
											2016-11-15 16:54:46 -03:00
										 |  |  |             '-frames:v', '1', | 
					
						
							|  |  |  |             $this->pathfile | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($this->filters as $filter) { | 
					
						
							|  |  |  |             $commands = array_merge($commands, $filter->apply($this)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $commands = array_merge($commands, array($pathfile)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $this->driver->command($commands); | 
					
						
							|  |  |  |         } catch (ExecutionFailureException $e) { | 
					
						
							|  |  |  |             $this->cleanupTemporaryFile($pathfile); | 
					
						
							|  |  |  |             throw new RuntimeException('Unable to save waveform', $e->getCode(), $e); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |