From 53a978e0215d1f7314b2a2e75a8025f4324c3967 Mon Sep 17 00:00:00 2001 From: Igor Kosteski Date: Sun, 18 Feb 2018 13:57:06 +0100 Subject: [PATCH 1/2] Fix Clipping video transcoding progress not correct --- src/FFMpeg/Format/Audio/DefaultAudio.php | 2 +- .../AbstractProgressListener.php | 5 +++-- src/FFMpeg/Format/ProgressableInterface.php | 3 ++- src/FFMpeg/Format/Video/DefaultVideo.php | 2 +- src/FFMpeg/Media/Audio.php | 2 +- src/FFMpeg/Media/Video.php | 19 +++++++++++++++++-- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/FFMpeg/Format/Audio/DefaultAudio.php b/src/FFMpeg/Format/Audio/DefaultAudio.php index 4226bff..4207911 100644 --- a/src/FFMpeg/Format/Audio/DefaultAudio.php +++ b/src/FFMpeg/Format/Audio/DefaultAudio.php @@ -121,7 +121,7 @@ abstract class DefaultAudio extends EventEmitter implements AudioInterface, Prog /** * {@inheritdoc} */ - public function createProgressListener(MediaTypeInterface $media, FFProbe $ffprobe, $pass, $total) + public function createProgressListener(MediaTypeInterface $media, FFProbe $ffprobe, $pass, $total, $duration = 0) { $format = $this; $listener = new AudioProgressListener($ffprobe, $media->getPathfile(), $pass, $total); diff --git a/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php b/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php index 3311266..169b41b 100644 --- a/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php +++ b/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php @@ -80,12 +80,13 @@ abstract class AbstractProgressListener extends EventEmitter implements Listener * * @throws RuntimeException */ - public function __construct(FFProbe $ffprobe, $pathfile, $currentPass, $totalPass) + public function __construct(FFProbe $ffprobe, $pathfile, $currentPass, $totalPass, $duration = 0) { $this->ffprobe = $ffprobe; $this->pathfile = $pathfile; $this->currentPass = $currentPass; $this->totalPass = $totalPass; + $this->duration = $duration; } /** @@ -255,7 +256,7 @@ abstract class AbstractProgressListener extends EventEmitter implements Listener } $this->totalSize = $format->get('size') / 1024; - $this->duration = $format->get('duration'); + $this->duration = (int) $this->duration > 0 ? (int) $this->duration : $format->get('duration'); $this->initialized = true; } diff --git a/src/FFMpeg/Format/ProgressableInterface.php b/src/FFMpeg/Format/ProgressableInterface.php index 4cd417d..497849c 100644 --- a/src/FFMpeg/Format/ProgressableInterface.php +++ b/src/FFMpeg/Format/ProgressableInterface.php @@ -24,8 +24,9 @@ interface ProgressableInterface extends EventEmitterInterface * @param FFProbe $ffprobe * @param Integer $pass The current pas snumber * @param Integer $total The total pass number + * @param Integer $duration The new video duration * * @return array An array of listeners */ - public function createProgressListener(MediaTypeInterface $media, FFProbe $ffprobe, $pass, $total); + public function createProgressListener(MediaTypeInterface $media, FFProbe $ffprobe, $pass, $total, $duration = 0); } diff --git a/src/FFMpeg/Format/Video/DefaultVideo.php b/src/FFMpeg/Format/Video/DefaultVideo.php index 1abfa67..0e7675c 100644 --- a/src/FFMpeg/Format/Video/DefaultVideo.php +++ b/src/FFMpeg/Format/Video/DefaultVideo.php @@ -125,7 +125,7 @@ abstract class DefaultVideo extends DefaultAudio implements VideoInterface /** * {@inheritdoc} */ - public function createProgressListener(MediaTypeInterface $media, FFProbe $ffprobe, $pass, $total) + public function createProgressListener(MediaTypeInterface $media, FFProbe $ffprobe, $pass, $total, $duration = 0) { $format = $this; $listeners = array(new VideoProgressListener($ffprobe, $media->getPathfile(), $pass, $total)); diff --git a/src/FFMpeg/Media/Audio.php b/src/FFMpeg/Media/Audio.php index c8fddb4..8a74962 100644 --- a/src/FFMpeg/Media/Audio.php +++ b/src/FFMpeg/Media/Audio.php @@ -62,7 +62,7 @@ class Audio extends AbstractStreamableMedia $listeners = null; if ($format instanceof ProgressableInterface) { - $listeners = $format->createProgressListener($this, $this->ffprobe, 1, 1); + $listeners = $format->createProgressListener($this, $this->ffprobe, 1, 1, 0); } $commands = $this->buildCommand($format, $outputPathfile); diff --git a/src/FFMpeg/Media/Video.php b/src/FFMpeg/Media/Video.php index 3d469ec..6fb75eb 100644 --- a/src/FFMpeg/Media/Video.php +++ b/src/FFMpeg/Media/Video.php @@ -24,6 +24,7 @@ use FFMpeg\Format\ProgressableInterface; use FFMpeg\Format\AudioInterface; use FFMpeg\Format\VideoInterface; use Neutron\TemporaryFilesystem\Manager as FsManager; +use FFMpeg\Filters\Video\ClipFilter; class Video extends Audio { @@ -78,9 +79,23 @@ class Video extends Audio try { /** add listeners here */ $listeners = null; - + if ($format instanceof ProgressableInterface) { - $listeners = $format->createProgressListener($this, $this->ffprobe, $pass + 1, $totalPasses); + $filters = clone $this->filters; + $duration = 0; + /* + check the filters of the video, + and if the video has the ClipFilter than: + take the new video duration and send to the + FFMpeg\Format\ProgressListener\AbstractProgressListener class + */ + foreach ($filters as $filter) { + if($filter instanceof ClipFilter){ + $duration = $filter->getDuration()->toSeconds(); + break; + } + } + $listeners = $format->createProgressListener($this, $this->ffprobe, $pass + 1, $totalPasses, $duration); } $this->driver->command($passCommands, false, $listeners); From 368c65a7d5bd8bc67336b74278ede3abdf5510fb Mon Sep 17 00:00:00 2001 From: Igor Kosteski Date: Mon, 19 Feb 2018 10:59:58 +0100 Subject: [PATCH 2/2] Comment fix, Video size calculation base on the new video duration and added case on the VideoProgressListenerTest test class. --- .../AbstractProgressListener.php | 5 ++--- src/FFMpeg/Media/Video.php | 10 ++++----- .../VideoProgressListenerTest.php | 21 +++++++++++++++++-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php b/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php index 169b41b..26eaf55 100644 --- a/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php +++ b/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php @@ -255,9 +255,8 @@ abstract class AbstractProgressListener extends EventEmitter implements Listener return; } - $this->totalSize = $format->get('size') / 1024; - $this->duration = (int) $this->duration > 0 ? (int) $this->duration : $format->get('duration'); - + $this->duration = (int) $this->duration > 0 ? $this->duration : $format->get('duration'); + $this->totalSize = $format->get('size') / 1024 * ($this->duration / $format->get('duration')); $this->initialized = true; } } diff --git a/src/FFMpeg/Media/Video.php b/src/FFMpeg/Media/Video.php index 6fb75eb..dd3fca1 100644 --- a/src/FFMpeg/Media/Video.php +++ b/src/FFMpeg/Media/Video.php @@ -83,12 +83,10 @@ class Video extends Audio if ($format instanceof ProgressableInterface) { $filters = clone $this->filters; $duration = 0; - /* - check the filters of the video, - and if the video has the ClipFilter than: - take the new video duration and send to the - FFMpeg\Format\ProgressListener\AbstractProgressListener class - */ + + // check the filters of the video, and if the video has the ClipFilter then + // take the new video duration and send to the + // FFMpeg\Format\ProgressListener\AbstractProgressListener class foreach ($filters as $filter) { if($filter instanceof ClipFilter){ $duration = $filter->getDuration()->toSeconds(); diff --git a/tests/Unit/Format/ProgressListener/VideoProgressListenerTest.php b/tests/Unit/Format/ProgressListener/VideoProgressListenerTest.php index 5367399..9e0bd28 100644 --- a/tests/Unit/Format/ProgressListener/VideoProgressListenerTest.php +++ b/tests/Unit/Format/ProgressListener/VideoProgressListenerTest.php @@ -11,7 +11,7 @@ class VideoProgressListenerTest extends TestCase /** * @dataProvider provideData */ - public function testHandle($size, $duration, + public function testHandle($size, $duration, $newVideoDuration, $data, $expectedPercent, $expectedRemaining, $expectedRate, $data2, $expectedPercent2, $expectedRemaining2, $expectedRate2, $currentPass, $totalPass @@ -26,7 +26,7 @@ class VideoProgressListenerTest extends TestCase 'duration' => $duration, )))); - $listener = new VideoProgressListener($ffprobe, __FILE__, $currentPass, $totalPass); + $listener = new VideoProgressListener($ffprobe, __FILE__, $currentPass, $totalPass, $newVideoDuration); $phpunit = $this; $n = 0; $listener->on('progress', function ($percent, $remaining, $rate) use (&$n, $phpunit, $expectedPercent, $expectedRemaining, $expectedRate, $expectedPercent2, $expectedRemaining2, $expectedRate2) { @@ -57,6 +57,7 @@ class VideoProgressListenerTest extends TestCase array( 147073958, 281.147533, + 281.147533, 'frame= 206 fps=202 q=10.0 size= 571kB time=00:00:07.12 bitrate= 656.8kbits/s dup=9 drop=0', 2, 0, @@ -71,6 +72,7 @@ class VideoProgressListenerTest extends TestCase array( 147073958, 281.147533, + 281.147533, 'frame= 206 fps=202 q=10.0 size= 571kB time=00:00:07.12 bitrate= 656.8kbits/s dup=9 drop=0', 1, 0, @@ -81,6 +83,21 @@ class VideoProgressListenerTest extends TestCase 3868, 1, 2 + ), + array( + 147073958, + 281.147533, + 35, + 'frame= 206 fps=202 q=10.0 size= 571kB time=00:00:07.12 bitrate= 656.8kbits/s dup=9 drop=0', + 60, + 0, + 0, + 'frame= 854 fps=113 q=20.0 size= 4430kB time=00:00:33.04 bitrate=1098.5kbits/s dup=36 drop=0', + 97, + 0, + 3868, + 2, + 2 ) ); }