Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not receiving progress notifications #794

Open
morphx666 opened this issue Feb 5, 2021 · 3 comments · May be fixed by #905
Open

Not receiving progress notifications #794

morphx666 opened this issue Feb 5, 2021 · 3 comments · May be fixed by #905
Labels

Comments

@morphx666
Copy link

Q A
Bug? Maybe
New Feature? no
Version Used 0.17
FFmpeg Version FFmpeg 3.4.8
OS CentOS 7

Actual Behavior

Not receiving progress notifications

Expected Behavior

Progress updates should be firing while transcoding video

Steps to Reproduce

Here's the code I'm using:

require 'vendor/autoload.php';

$ffp = FFMpeg\FFProbe::create();
$d = $ffp
	->streams("video.mov")   // extracts streams informations
	->videos()               // filters video streams
	->first()                // returns the first video stream
	->getDimensions();       // returns a FFMpeg\Coordinate\Dimension object

$a = $d->getRatio();
$w = 1024;
$h = $a->calculateHeight($w);

$ffm = FFMpeg\FFMpeg::create([
	"timeout" 		=> 3600,
	"ffmpeg.threads" 	=> 8,
]);
	$video = $ffm->open('video.mov');

$video
	->filters()
	->resize(new FFMpeg\Coordinate\Dimension($h, $w), // Vertical video
			 FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_FIT)
	->synchronize();

$format = new FFMpeg\Format\Video\X264();
$format->on('progress', function($video, $format, $percentage) {
		echo "$percentage % transcoded";
	});
$format->setAudioCodec("libmp3lame");
$format
	->setKiloBitrate(1000)
	->setAudioChannels(1)
	->setAudioKiloBitrate(128);

$video->save($format, 'export-x264.mp4')
@vietnguyen09
Copy link

vietnguyen09 commented Feb 20, 2021

I have this problem too, can someone help, please?

UPDATE WHAT I HAVE FOUND

After digging inside the framework, it seems like the ProgressListener weird work. Here is what I found.

AbstractProgressListener.php line 180
In private function parseProgress($progress) you guy can see:

if ($this->lastOutput !== null) {

$this->lastOutput is set at the line 203 $this->lastOutput = $currentTime; and it always null so the $this->remaining always goes null too. This will lead to return $this->getProgressInfo(); will return null and fail to fire the ->on('progress') method.

AbstractProgressListener.php line 232

private function getProgressInfo()
{
        //This will return null everytime.
	if ($this->remaining === null) {
		return null;
	}

	return array(
		'percent'   => $this->percent,
		'remaining' => $this->remaining,
		'rate'      => $this->rate
	);
}

HOW TO FIX?
I have to touch the framework files while waiting for the author to fix it (or at least give the answer to why it doesn't work). And this fixed way is not recommended.

In AbstractProgressListener.php find private function parseProgress($progress) and replace with this.

private function parseProgress($progress)
{
	if (!$this->initialized) {
		$this->initialize();
	}
	
	if (null === $this->totalSize || null === $this->duration) {
		return;
	}
	$matches = array();

	if (preg_match($this->getPattern(), $progress, $matches) !== 1) {
		return null;
	}

	$currentDuration = $this->convertDuration($matches[2]);
	$currentTime = microtime(true);
	$currentSize = trim(str_replace('kb', '', strtolower(($matches[1]))));
	$percent = max(0, min(1, $currentDuration / $this->duration));
	$this->lastOutput = $currentTime;
	
	$delta = $currentTime - $this->lastOutput;

	// Check the type of the currentSize variable and convert it to an integer if needed.
	if(!is_numeric($currentSize)) {
		$currentSize = (int)$currentSize;
	}

	$deltaSize = $currentSize - $this->currentSize;
	$rate = $deltaSize * $delta;
	if ($rate > 0) {
		$totalDuration = $this->totalSize / $rate;
		$this->remaining = floor($totalDuration - ($totalDuration * $percent));
		$this->rate = floor($rate);
	} else {
		$this->remaining = 0;
		$this->rate = 0;
	}

	$percent = $percent / $this->totalPass + ($this->currentPass - 1) / $this->totalPass;

	$this->percent = floor($percent * 100);
	$this->currentSize = (int) $currentSize;
	$this->currentTime = $currentDuration;
	
	return $this->getProgressInfo();
}

The ->on('progress') will working again.

P/S: I found that the HLS stream file will fail in getting size

@jens1o jens1o added the bug label Mar 3, 2021
@tamkeen-tms
Copy link

tamkeen-tms commented Apr 6, 2021

The progress event is called, for me, only when converting from MP4, not MPG for example!

@FacuM FacuM linked a pull request Jun 6, 2023 that will close this issue
@ekeyte
Copy link

ekeyte commented Jul 3, 2023

The PR above would definitely help a lot if we could get that merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants