From 9314f64f54cd434fb6ccdefc5ae7537676cb7a60 Mon Sep 17 00:00:00 2001 From: Ivan Ganev Date: Thu, 2 Nov 2017 17:28:12 +0200 Subject: [PATCH] added waveform color option --- .gitignore | 1 + src/FFMpeg/Media/Audio.php | 4 +-- src/FFMpeg/Media/Waveform.php | 52 +++++++++++++++++++++++++++++-- tests/Unit/Media/WaveformTest.php | 2 +- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 7d052cb..4f3f707 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ composer.phar composer.lock phpunit.xml sami.phar +.idea/ \ No newline at end of file diff --git a/src/FFMpeg/Media/Audio.php b/src/FFMpeg/Media/Audio.php index 19f3478..605345b 100644 --- a/src/FFMpeg/Media/Audio.php +++ b/src/FFMpeg/Media/Audio.php @@ -110,8 +110,8 @@ class Audio extends AbstractStreamableMedia * @param integer $height * @return Waveform */ - public function waveform($width = 640, $height = 120) + public function waveform($width = 640, $height = 120, $colors = [Waveform::DEFAULT_COLOR]) { - return new Waveform($this, $this->driver, $this->ffprobe, $width, $height); + return new Waveform($this, $this->driver, $this->ffprobe, $width, $height, $colors); } } diff --git a/src/FFMpeg/Media/Waveform.php b/src/FFMpeg/Media/Waveform.php index 0954c00..6c66ed7 100644 --- a/src/FFMpeg/Media/Waveform.php +++ b/src/FFMpeg/Media/Waveform.php @@ -20,17 +20,21 @@ use FFMpeg\Exception\RuntimeException; class Waveform extends AbstractMediaType { + const DEFAULT_COLOR = '#000000'; + /** @var Video */ private $audio; private $width; private $height; - public function __construct(Audio $audio, FFMpegDriver $driver, FFProbe $ffprobe, $width, $height) + public function __construct(Audio $audio, FFMpegDriver $driver, FFProbe $ffprobe, $width, $height, $colors = [self::DEFAULT_COLOR]) { parent::__construct($audio->getPathfile(), $driver, $ffprobe); $this->audio = $audio; $this->width = $width; $this->height = $height; + + $this->setColors($colors); } /** @@ -65,6 +69,50 @@ class Waveform extends AbstractMediaType return $this; } + /** + * Parameter should be an array containing at least one valid color represented as a HTML color string. For + * example #FFFFFF or #000000. By default the color is set to black. Keep in mind that if you save the waveform + * as jpg file, it will appear completely black and to avoid this you can set the waveform color to white (#FFFFFF). + * Saving waveforms to png is strongly suggested. + * @param array $colors + */ + public function setColors(array $colors) + { + foreach ($colors as $row => $value) + { + if ($value{0} != '#' || strlen($value) != 7) + { + //invalid color + unset($colors[$row]); + } + } + + if (count($colors)) + { + $this->colors = $colors; + } + } + + /** + * Returns an array of colors that will be passed to ffmpeg to use for waveform generation. Colors are applied ONLY + * to the waveform. Background cannot be controlled that easily and it is probably easier to save the waveform + * as a transparent png file and then add background of choice. + * @return array + */ + public function getColors() + { + return $this->colors; + } + + /** + * Compiles the selected colors into a string, using a pipe separator. + * @return string + */ + protected function compileColors() + { + return implode('|', $this->colors); + } + /** * Saves the waveform in the given filename. * @@ -82,7 +130,7 @@ class Waveform extends AbstractMediaType */ $commands = array( '-i', $this->pathfile, '-filter_complex', - 'showwavespic=s='.$this->width.'x'.$this->height, + 'showwavespic=colors='.$this->compileColors().':s='.$this->width.'x'.$this->height, '-frames:v', '1' ); diff --git a/tests/Unit/Media/WaveformTest.php b/tests/Unit/Media/WaveformTest.php index cd4f08f..9e03a58 100644 --- a/tests/Unit/Media/WaveformTest.php +++ b/tests/Unit/Media/WaveformTest.php @@ -62,7 +62,7 @@ class WaveformTest extends AbstractMediaTestCase array( array( '-i', NULL, '-filter_complex', - 'showwavespic=s=640x120', + 'showwavespic=colors=#000000:s=640x120', '-frames:v', '1', ), ),