-
Notifications
You must be signed in to change notification settings - Fork 3
/
ffmpeg-video-filter.php
307 lines (225 loc) · 7.04 KB
/
ffmpeg-video-filter.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
require_once("ffmpeg-filter.php");
class ffmpeg_video_filter {
public $ffmpeg_wrapper;
public $filters;
public $string;
public $input_files;
public $last_stream;
public function __construct($wrapper) {
$this->ffmpeg_wrapper = $wrapper;
$this->filters = array();
$this->input_files = array();
$this->last_stream = 0;
}
private function new_id() {
$stream_id = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"),0,5);
return $stream_id;
}
private function new_input_id() {
$stream_id = "_" . substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"),0,3);
return $stream_id;
}
private function stream_prefix() {
return "t";
}
private function push_filter($new_filter) {
if ($this->ffmpeg_wrapper->video_filter_complex_flag) {
throw new Exception("Can't add filter over complex filter", 1);
}
array_push($this->filters, $new_filter);
}
public function add_input($file, $next_filter = null) {
$stream_id = $this->new_input_id();
$input_filter = new ffmpeg_filter();
$input_filter->id = $stream_id;
$input_filter->name = "movie";
$input_filter->values = array($file);
$input_filter->input_streams = array();
$input_filter->output_streams = array($stream_id);
if ($next_filter != null) {
$input_filter->filter = $next_filter;
}
$this->push_filter($input_filter);
return $stream_id;
}
public function get_string() {
$this->string = "";
if (count($this->filters) == 0) {
return;
}
return $this->build();
}
public function add_simple_filter_with_filter($name,$values,$next_filter) {
$stream_id = $this->new_id();
$filter = new ffmpeg_filter();
$filter->id = $stream_id;
$filter->name = $name;
$filter->values = $values;
$filter->filter = $next_filter;
// chain algorithem
$filter->input_streams = array($this->last_stream);
$filter->output_streams = array($this->new_stream());
$this->push_filter($filter);
}
public function add_simple_filter($name,$values) {
$stream_id = $this->new_id();
$filter = new ffmpeg_filter();
$filter->id = $stream_id;
$filter->name = $name;
$filter->values = $values;
// chain algorithem
$filter->input_streams = array($this->last_stream);
$filter->output_streams = array($this->new_stream());
$this->push_filter($filter);
}
private function stream_str($s_id) {
return "[" . $this->stream_prefix() . $s_id . "]";
}
private function new_stream() {
$s_id = $this->last_stream + 1;
$this->last_stream = $s_id;
return $s_id;
}
private function build() {
$this->string .= "-vf" . " " . "'";
// add input files
foreach ($this->input_files as $file) {
$this->string .= $file->get_string();
foreach ($file->output_streams as $out_stream) {
$this->string .= $this->stream_str($out_stream);
}
$this->string .= ";";
}
// filter files
$this->string .= "[in]fifo" . $this->stream_str(0) . ";";
foreach ($this->filters as $filter) {
foreach ($filter->input_streams as $in_stream) {
$this->string .= $this->stream_str($in_stream);
}
$this->string .= $filter->get_string();
foreach ($filter->output_streams as $out_stream) {
$this->string .= $this->stream_str($out_stream);
}
$this->string .= ";";
}
$this->string .= $this->stream_str($this->last_stream) . "fifo" . "[out]";
$this->string .= "'" . " ";
return $this->string;
}
// *************************************************************************************************** //
// ** ffmpeg-video-filter :: public functions ** //
// *************************************************************************************************** //
public function scale($to_width,$to_height) {
$this->add_simple_filter("scale",array($to_width,$to_height));
}
public function pad_center($output_width,$output_height) {
$this->add_simple_filter("pad",array($output_width,$output_height,"(ow-iw)/2","(oh-ih)/2"));
}
public function crop($crop_width,$crop_heigth,$x,$y) {
$this->add_simple_filter("crop",array($crop_width,$crop_heigth,$x,$y));
}
public function cropdetect($limit = 24,$round = 2,$reset = 0) {
$this->add_simple_filter("cropdetect",array($limit,$round,$reset));
}
public function select_iframes() {
$this->add_simple_filter("select",array("select=eq(pict_type\,I)"));
}
public function select_scence_frames($val) {
$this->add_simple_filter("select",array("gt(scene\," . $val . ")"));
}
public function hflip() {
$this->add_simple_filter("hflip",array());
}
public function vflip() {
$this->add_simple_filter("vflip",array());
}
public function fix_letterbox($letterbox_array,$width,$height) {
$flag = false;
if (!empty($letterbox_array)) {
// add crop filter to filter-out the letterbox
//
// Multiple bugs here...
if ($width == 1280 && $height == 720) {
$flag = true;
$this->crop($letterbox_array["0"],
$letterbox_array["1"],
$letterbox_array["2"],
$letterbox_array["3"]
);
}
}
return $flag;
}
public function transpose($transpose_degree) {
switch ($transpose_degree) {
case 0:
return false;
break;
case 90: case 270:
$info = $this->ffmpeg_wrapper->last_input_info_video;
$currnet_width = $info["width"];
$current_height = $info["height"];
$new_width = intval($current_height * ($current_height / $currnet_width));
$new_heigth = intval($current_height);
$padcenter_filter = ffmpeg_filter::filter_with_params(
"pad",
array($currnet_width,$current_height,"(ow-iw)/2","(oh-ih)/2"),
array(),
array()
);
$scale_filter = ffmpeg_filter::filter_with_params(
"scale",
array($new_width,$new_heigth),
array(),
array(),
$padcenter_filter
);
$this->add_simple_filter_with_filter("transpose",array(1),$scale_filter);
if ($transpose_degree == 270) {
$this->hflip();
}
return true;
break;
case 180:
$this->hflip();
$this->vflip();
return true;
break;
default:
return false;
break;
}
return false;
}
public function overlay($movie,$x,$y) {
$overlay_stream_id = $this->add_input($movie);
$overlay = new ffmpeg_filter();
$overlay->id = $this->new_id();
$overlay->name = "overlay";
$overlay->values = array($x,$y);
// chain algorithem
$overlay->input_streams = array($this->last_stream, $overlay_stream_id); // 2 inputs
$overlay->output_streams = array($this->new_stream());
$this->push_filter($overlay);
}
public function overlay_with_filters($movie,$x,$y,$input_filter = null,$overlay_filter = null) {
if ($input_filter != null) {
$overlay_stream_id = $this->add_input($movie,$input_filter);
} else {
$overlay_stream_id = $this->add_input($movie);
}
$overlay = new ffmpeg_filter();
$overlay->id = $this->new_id();
$overlay->name = "overlay";
$overlay->values = array($x,$y);
if ($overlay_filter != null) {
$overlay->filter = $overlay_filter;
}
// chain algorithem
$overlay->input_streams = array($this->last_stream, $overlay_stream_id); // 2 inputs
$overlay->output_streams = array($this->new_stream());
$this->push_filter($overlay);
}
}
?>