Skip to content

Commit a1119dd

Browse files
committed
fixed bugs with progress handlers that prevented both methods of envying the handlers from working
1 parent 9339218 commit a1119dd

File tree

4 files changed

+98
-50
lines changed

4 files changed

+98
-50
lines changed

examples/progress-handler-native.php

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,53 @@
11
<?php
22

33
include_once './includes/bootstrap.php';
4+
5+
echo '<a href="?method=blocking">Blocking</a> | <a href="?method=non-blocking">Non blocking</a><br />';
46

57
try
68
{
79
$video = new \PHPVideoToolkit\Video($example_video_path, $config);
10+
$video->extractSegment(new \PHPVideoToolkit\Timecode(10), new \PHPVideoToolkit\Timecode(70));
811
$process = $video->getProcess();
912

10-
$progress_handler = new \PHPVideoToolkit\ProgressHandlerNative(function($data)
13+
if(isset($_GET['method']) === true && $_GET['method'] === 'blocking')
1114
{
12-
// echo '<pre>'.print_r($data, true).'</pre>';
13-
// do something here...
14-
// IMPORTANT NOTE: most modern browser don't support output buffering any more.
15-
}, $config);
15+
echo '<h2>Blocking Method</h2>';
1616

17-
/*
18-
...or...
17+
// If you use a blocking save but want to handle the progress during the block, then assign a callback within
18+
// the constructor of the progress handler.
19+
// IMPORTANT NOTE: most modern browser don't support output buffering any more.
20+
$progress_data = array();
21+
$progress_handler = new \PHPVideoToolkit\ProgressHandlerNative(function($data) use (&$progress_data)
22+
{
23+
// do something here like log to file or db.
24+
array_push($progress_data, round($data['percentage'], 2).': '.round($data['run_time'], 2));
25+
}, $config);
1926

20-
$progress_handler = new ProgressHandlerNative(null, $config);
27+
$output = $video->purgeMetaData()
28+
->setMetaData('title', 'Hello World')
29+
->save('./output/big_buck_bunny.3gp', null, \PHPVideoToolkit\Video::OVERWRITE_EXISTING, $progress_handler);
30+
31+
array_unshift($progress_data, 'Percentage Completed: Time taken');
32+
\PHPVideoToolkit\Trace::vars(implode(PHP_EOL, $progress_data));
33+
}
34+
else
35+
{
36+
echo '<h2>Non Blocking Method</h2>';
2137

22-
...then after a call to saveNonBlocking...
23-
24-
while($progress_handler->completed !== true)
25-
{
26-
// note setting true in probe() automatically tells the probe to wait after the data is returned.
27-
echo '<pre>'.print_r($progress_handler->probe(true), true).'</pre>';
28-
}
29-
30-
*/
38+
// use a non block save to probe the progress handler after the save has been made.
39+
// IMPORTANT: this method only works with ->saveNonBlocking as otherwise the progress handler
40+
// probe will quit after one cycle.
41+
$progress_handler = new \PHPVideoToolkit\ProgressHandlerNative(null, $config);
42+
$output = $video->purgeMetaData()
43+
->setMetaData('title', 'Hello World')
44+
->saveNonBlocking('./output/big_buck_bunny.3gp', null, \PHPVideoToolkit\Video::OVERWRITE_EXISTING, $progress_handler);
3145

32-
$output = $video->purgeMetaData()
33-
->setMetaData('title', 'Hello World')
34-
->save('./output/big_buck_bunny.3gp', null, \PHPVideoToolkit\Video::OVERWRITE_EXISTING, $progress_handler);
46+
while($progress_handler->completed !== true)
47+
{
48+
\PHPVideoToolkit\Trace::vars($progress_handler->probe(true, 1));
49+
}
50+
}
3551

3652
echo '<h1>Executed Command</h1>';
3753
\PHPVideoToolkit\Trace::vars($process->getExecutedCommand());
@@ -70,4 +86,4 @@
7086
\PHPVideoToolkit\Trace::vars($e);
7187

7288
echo '<a href="?reset=1">Reset Process</a>';
73-
}
89+
}

examples/progress-handler-output.php

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,53 @@
22

33
include_once './includes/bootstrap.php';
44

5+
echo '<a href="?method=blocking">Blocking</a> | <a href="?method=non-blocking">Non blocking</a><br />';
6+
57
try
68
{
79
$video = new \PHPVideoToolkit\Video($example_video_path, $config);
10+
$video->extractSegment(new \PHPVideoToolkit\Timecode(10), new \PHPVideoToolkit\Timecode(70));
811
$process = $video->getProcess();
912

10-
$progress_handler = new \PHPVideoToolkit\ProgressHandlerOutput(function($data)
13+
if(isset($_GET['method']) === true && $_GET['method'] === 'blocking')
1114
{
12-
// echo '<pre>'.print_r($data, true).'</pre>';
13-
// do something here...
14-
// IMPORTANT NOTE: most modern browser don't support output buffering any more.
15-
}, $config);
16-
17-
/*
18-
...or...
15+
echo '<h2>Blocking Method</h2>';
1916

20-
$progress_handler = new ProgressHandlerOutput(null, $config);
17+
// If you use a blocking save but want to handle the progress during the block, then assign a callback within
18+
// the constructor of the progress handler.
19+
// IMPORTANT NOTE: most modern browser don't support output buffering any more.
20+
$progress_data = array();
21+
$progress_handler = new \PHPVideoToolkit\ProgressHandlerOutput(function($data) use (&$progress_data)
22+
{
23+
// do something here like log to file or db.
24+
array_push($progress_data, round($data['percentage'], 2).': '.round($data['run_time'], 2));
25+
}, $config);
2126

22-
...then after a call to saveNonBlocking...
23-
24-
while($progress_handler->completed !== true)
25-
{
26-
// note setting true in probe() automatically tells the probe to wait after the data is returned.
27-
echo '<pre>'.print_r($progress_handler->probe(true), true).'</pre>';
28-
}
29-
30-
*/
27+
$output = $video->purgeMetaData()
28+
->setMetaData('title', 'Hello World')
29+
->save('./output/big_buck_bunny.3gp', null, \PHPVideoToolkit\Video::OVERWRITE_EXISTING, $progress_handler);
30+
31+
array_unshift($progress_data, 'Percentage Completed: Time taken');
32+
\PHPVideoToolkit\Trace::vars(implode(PHP_EOL, $progress_data));
33+
}
34+
else
35+
{
36+
echo '<h2>Non Blocking Method</h2>';
3137

32-
$output = $video->purgeMetaData()
33-
->setMetaData('title', 'Hello World')
34-
->save('./output/big_buck_bunny.3gp', null, \PHPVideoToolkit\Video::OVERWRITE_EXISTING, $progress_handler);
38+
// use a non block save to probe the progress handler after the save has been made.
39+
// IMPORTANT: this method only works with ->saveNonBlocking as otherwise the progress handler
40+
// probe will quit after one cycle.
41+
$progress_handler = new \PHPVideoToolkit\ProgressHandlerOutput(null, $config);
42+
$output = $video->purgeMetaData()
43+
->setMetaData('title', 'Hello World')
44+
->saveNonBlocking('./output/big_buck_bunny.3gp', null, \PHPVideoToolkit\Video::OVERWRITE_EXISTING, $progress_handler);
3545

46+
while($progress_handler->completed !== true)
47+
{
48+
\PHPVideoToolkit\Trace::vars($progress_handler->probe(true, 1));
49+
}
50+
}
51+
3652
echo '<h1>Executed Command</h1>';
3753
\PHPVideoToolkit\Trace::vars($process->getExecutedCommand());
3854
echo '<hr /><h1>FFmpeg Process Messages</h1>';
@@ -70,4 +86,4 @@
7086
\PHPVideoToolkit\Trace::vars($e);
7187

7288
echo '<a href="?reset=1">Reset Process</a>';
73-
}
89+
}

src/PHPVideoToolkit/Media.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,27 @@ public function save($save_path=null, Format $output_format=null, $overwrite=Med
680680
// exec the buffer
681681
// set the blocking mode
682682
// and execute the ffmpeg process.
683-
$this->_process->setOutputPath($this->_processing_path)
684-
->getExecBuffer()
685-
->setBlocking($this->_blocking === null ? true : $this->_blocking)
686-
->execute();
687-
683+
$buffer = $this->_process->setOutputPath($this->_processing_path)
684+
->getExecBuffer()
685+
->setBlocking($this->_blocking === null ? true : $this->_blocking);
686+
687+
if($progress_handler !== null && $progress_handler->getNonBlockingCompatibilityStatus() === false)
688+
{
689+
$buffer->execute(
690+
function() use ($progress_handler)
691+
{
692+
if($progress_handler !== null)
693+
{
694+
$progress_handler->callback();
695+
}
696+
}
697+
);
698+
}
699+
else
700+
{
701+
$buffer->execute();
702+
}
703+
688704
// now we work out what we are returning as it depends on the blocking status.
689705
if($this->_blocking === true)
690706
{

src/PHPVideoToolkit/ProgressHandlerAbstract.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function probe($probe_then_wait=true, $seconds=1)
7373
{
7474
if($this->_wait_on_next_probe === true)
7575
{
76-
if(is_int($seconds) === false)
76+
if(is_int($seconds) === false && is_float($seconds) === false)
7777
{
7878
throw new Exception('$seconds must be an integer.');
7979
}
@@ -95,7 +95,7 @@ public function callback()
9595
if(is_callable($this->_callback) === true)
9696
{
9797
$data = $this->_processOutputFile();
98-
call_user_func($this->_callback, $data, $output_object);
98+
call_user_func($this->_callback, $data);
9999
}
100100
}
101101

0 commit comments

Comments
 (0)