-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathchaining-processes.php
154 lines (131 loc) · 5.71 KB
/
chaining-processes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace PHPVideoToolkit;
include_once './includes/bootstrap.php';
ob_start();
echo '<p>This is an example to chain processing on from one output to another to another and so on.</p><hr />';
echo '<p>.mov status: <strong id="status-1">---</strong></p>';
echo '<p>resize mov status: <strong id="status-2">---</strong></p>';
echo '<p>jpeg from resized mov status: <strong id="status-3">---</strong></p>';
ob_flush();
try
{
$video = new Video($example_video_path);
$progress_handler = new ProgressHandlerNative();
$process = $video->extractSegment(new Timecode(10), new Timecode(20))
->saveNonBlocking('./output/big_buck_bunny.mov', null, Media::OVERWRITE_EXISTING, $progress_handler);
$dot_count = -1;
while($progress_handler->completed !== true)
{
$dot_count += 1;
$data = $progress_handler->probe(true);
if($data['started'] === true)
{
echo '<script>document.getElementById("status-1").innerHTML = '.json_encode('Encoding to mov '.$data['percentage'].'% '.str_pad('', $dot_count, '.')).'</script>';
echo ' ';
ob_flush();
//sleep(1);
if($dot_count > 10)
{
$dot_count = -1;
}
}
else
{
echo '<script>document.getElementById("status-1").innerHTML = '.json_encode('Waiting for encoding to start').'</script>';
echo ' ';
ob_flush();
}
}
if($process->hasError() === true)
{
echo '<script>document.getElementById("status-1").innerHTML = '.json_encode('mov encoding encountered an error: '.$process->getErrorCode().'.').'</script>';
ob_flush();
// an error was encountered, do something with it.
}
else
{
$mov = $process->getOutput();
echo '<script>document.getElementById("status-1").innerHTML = '.json_encode('mov Encoded OK, output: "'.$mov->getMediaPath().'".').'</script>';
ob_flush();
// encoding has completed and no error was detected so
// we can get the output from the process.
}
$format = $mov->getDefaultFormat(Format::OUTPUT);
$format->setVideoDimensions(100, 100);
$progress_handler = new ProgressHandlerNative();
$process = $mov->saveNonBlocking('./output/big_buck_bunny_resized.mov', $format, Media::OVERWRITE_EXISTING, $progress_handler);
$dot_count = -1;
while($progress_handler->completed !== true)
{
$dot_count += 1;
$data = $progress_handler->probe(true);
if($data['started'] === true)
{
echo '<script>document.getElementById("status-2").innerHTML = '.json_encode('Encoding to mov '.$data['percentage'].'% '.str_pad('', $dot_count, '.')).'</script>';
echo ' ';
ob_flush();
//sleep(1);
if($dot_count > 10)
{
$dot_count = -1;
}
}
else
{
echo '<script>document.getElementById("status-2").innerHTML = '.json_encode('Waiting for encoding to start').'</script>';
echo ' ';
ob_flush();
}
}
if($process->hasError() === true)
{
echo '<script>document.getElementById("status-2").innerHTML = '.json_encode('Encoding encountered an error: '.$process->getErrorCode().'.').'</script>';
ob_flush();
// an error was encountered, do something with it.
}
else
{
$resized_mov = $process->getOutput();
echo '<script>document.getElementById("status-2").innerHTML = '.json_encode('Encoded OK, output: "'.$resized_mov->getMediaPath().'".').'</script>';
ob_flush();
// encoding has completed and no error was detected so
// we can get the output from the process.
}
$format = new ImageFormat_Jpeg('output');
$process = $resized_mov->save('./output/big_buck_bunny_resized.jpg', $format, Media::OVERWRITE_EXISTING);
if($process->hasError() === true)
{
echo '<script>document.getElementById("status-3").innerHTML = '.json_encode('Encoding encountered an error: '.$process->getErrorCode().'.').'</script>';
ob_flush();
// an error was encountered, do something with it.
}
else
{
$jpeg = $process->getOutput();
echo '<script>document.getElementById("status-3").innerHTML = '.json_encode('Encoded OK, output: "'.$jpeg->getMediaPath().'".').'</script>';
ob_flush();
// encoding has completed and no error was detected so
// we can get the output from the process.
}
}
catch(FfmpegProcessOutputException $e)
{
echo '<h1>Error</h1>';
Trace::vars($e);
if($process->isCompleted())
{
echo '<hr /><h2>Executed Command</h2>';
Trace::vars($process->getExecutedCommand());
echo '<hr /><h2>FFmpeg Process Messages</h2>';
Trace::vars($process->getMessages());
echo '<hr /><h2>Buffer Output</h2>';
Trace::vars($process->getBuffer(true));
}
}
catch(Exception $e)
{
echo '<h1>Error</h1>';
Trace::vars($e->getMessage());
echo '<h2>Exception</h2>';
Trace::vars($e);
}