-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfunctions.php
557 lines (502 loc) · 15.6 KB
/
functions.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
defined('SOURCE_PATH') || die('SOURCE_PATH not defined!');
/**
* Get Directory Files
*
* Scans a given directory and returns an array containing any files in that directory.
*
* @param string $path The full path of the directory we want the files of
* @return array Either an empty array (if no files exist) or an array of files
* @since 0.1
*/
function _get_files( $path )
{
$cont = scandir($path);
$files = array();
foreach($cont as $item)
{
// Skip directories ...
if( $item == '.' || $item == '..' || is_dir($path . $item) )
continue;
$files[] = $item;
}
return $files;
}
/**
* Get Source Files
*
* Retrieves all files in the {@link SOURCE_PATH} directory, defined in config.php.
*
* @param void
* @return array
* @since 0.1
*/
function _source_files()
{
return _get_files(SOURCE_PATH);
}
/**
* Get Converted Files
*
* Retrieves all files in the {@link OUTPUT_PATH} directory, defined in config.php.
*
* @param void
* @return array
* @since 0.1
*/
function _converted_files()
{
return _get_files(OUTPUT_PATH);
}
/**
* JSON-encoded response
*
* Outputs a JSON-encoded array of data and sends the correct headers as well.
* It will send an Error 500 if the second parameter is set to true, otherwise
* it sends 200 OK header.
*
* This function also haults the script, preventing any additional output that might
* cause errors.
*
* @param mixed $data Data can be a string, integer, or an array to be encoded.
* @param bool $error Whether or not the response is an error or not.
* @return void
* @since 0.1
*/
function json_response( $data, $error=false )
{
if( $error )
header('HTTP/1.1 500 JSON Error');
else
header('HTTP/1.1 200 OK');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 1970 00:00:00 GMT');
header('Content-type: application/json');
// Convert strings/integers into an array before outputting data...
if(!is_array($data))
echo json_encode(array($data), true);
else
echo json_encode($data, true);
exit;
}
/**
* Check $_POST Variable
*
* This checks to see if the given array key is set in the $_POST array and if not,
* it returns the given default value.
*
* @param string $key The key to check the $_POST array for
* @param mixed $default A default value to use if it's not set
* @return mixed
* @since 0.1
*/
function _chkVal($key, $default=false) {
return isset($_POST[$key]) ? $_POST[$key] : $default;
}
/**
* Convert Seconds to Minutes
*
* Converts given seconds into HH:MM:SS format
*
* @param integer $sec The number of seconds to convert
* @return string HH:MM:SS string
* @since 0.1
*/
function sec2min( $sec )
{
return sprintf('%02d:%02d:%02d', floor($sec/3600), floor(($sec/60)%60), $sec%60);
}
/**
* FFMPEG Convert Video
*
* This is a class used to process requests for converting videos via FFMPEG,
* although at this time it doesn't actually execute the command.
*
* It handles status logging, error logging, etc.
*
* @author Amereservant <[email protected]>
* @copyright 2012 Amereservant
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License
* @version 0.1
* @link http://myownhomeserver.com
* @since 0.1
*/
class ffmpegConvert
{
/**
* File Key (Used mainly to identify the file log)
*
* This should only be set via the {@link _setFkey()} method!
*
* @var string
* @access protected
* @since 0.1
*/
protected $fkey;
/**
* Log File(s) Path
*
* @var string
* @access protected
* @since 0.1
*/
protected $logPath = '';
/**
* Errors
*
* @var array
* @access protected
* @since 0.1
*/
protected $error = array();
/**
* FFMPEG Execution Path (includes directory/ffmpeg.exe)
*
* @var string
* @access protected
* @since 0.1
*/
protected $ffmpegPath = '';
/**
* Progress Log (stores ffmpeg progress data only!)
*
* This is the file the output from ffmpeg.exe will be written to.
* Nothing else should be written to this file or it can cause parse errors.
*
* This is set by the {@link _setProgressLogFkey()} method.
*
* @var string
* @access protected
* @since 0.1
*/
protected $progressLog = '';
/**
* Constructor
*
* The {@link $fkey} can be set via this parameter (it's recommended).
* This method sets the paths from CONSTANTS defined in config.php and registers
* shutdown functions to automatically write log errors when
* the script ends.
*
* @param string $fkey The file key to identify the log/file by.
* @return void
* @access public
* @since 0.1
*/
public function __construct($fkey='')
{
$this->logPath = LOG_PATH;
$this->ffmpegPath = FFMPEG_PATH;
$this->_setFkey($fkey);
register_shutdown_function(array(&$this, 'logErrors'));
}
/**
* Set Fkey
*
* Sets the {@link $fkey} property and calls the {@link _setStatusLogFkey()} method
* to set the Status Log file.
*
* This method should be the ONLY way the {@link $fkey} property is set!
*
* @param string $fkey The file key to assign to the {@link $fkey} property.
* @return void
* @access private
* @since 0.1
*/
private function _setFkey($fkey)
{
$this->fkey = $fkey;
$this->_setProgressLog();
}
/**
* Set Progress Log File
*
* Set's the {@link $progressLog} property based on the current {@link $fkey} value.
*
* @param void
* @return void
* @access private
* @since 0.1
*/
private function _setProgressLog()
{
$this->progressLog = $this->fkey .'.ffmpeg.log';
}
/**
* Execute FFMPEG Conversion
*
* This checks the input data for valid values, then via POST to the execution URL
* (Defined as EXEC_URL in config.php), it initiates the ffmpeg command and begins
* the video conversion.
*
* The script located at EXEC_URL actually calls PHP's exec() function...
*
* @param string $inFile The input filename (without path)
* @param string $outFile The output filename (without path)
* @param string $params The ffmpeg parameters to use for converting the video
* @return void
* @access public
* @since 0.1
*/
public function exec( $inFile, $outFile, $params )
{
// Set an fkey if it hasn't already been set. This probably should be removed...
if( strlen($this->fkey) < 1 ) {
$fkey = hash('crc32', time() . $inFile, false);
$this->_setFkey($fkey);
}
// Verify the ffmpeg path is valid
if( strlen($this->ffmpegPath) < 1 || !file_exists($this->ffmpegPath) )
$this->error[] = 'Invalid FFMPEG Path `'. $this->ffmpegPath .'`!';
// Verify the source file exists
if( strlen($inFile) < 1 || !file_exists(SOURCE_PATH . $inFile) )
$this->error[] = 'Invalid input file `'. $inFile .'`!';
// Verify the output filename has been given
if( strlen($outFile) < 1 )
$this->error[] = 'Invalid output file!';
// Verify the conversion parameters have been given
if( strlen($params) < 1 )
$this->error[] = 'No parameters were given! Please specify conversion parameters.';
// Check if there are any errors and stop if so...
if( count($this->error) > 0 )
{
$this->logErrors();
return false;
}
// Write status message updating us on where the script is at ...
$this->writeStatus("Sending FFMPEG exec command to ". EXEC_URL ." ...");
$cmd = ' -i "'. SOURCE_PATH . $inFile .'" '. $params .' "'. OUTPUT_PATH . $outFile .'" 2> '. $this->logPath . $this->progressLog;
// Write the execution command to the status log
$this->writeStatus($cmd);
$data = array(
'cmd' => $cmd,
'ffmpegpw' => FFMPEG_PW,
'fkey' => $this->fkey
);
// Form the POST data string
$pdata = http_build_query($data);
//<< NOTE: cURL doesn't work for this. >>//
$fh = fsockopen($_SERVER['HTTP_HOST'], 80, $errno, $errstr, 30);
// POST the data to the execution script
fputs($fh, 'POST '. EXEC_URL ." HTTP/1.1\n");
fputs($fh, 'Host: '. $_SERVER['HTTP_HOST'] ."\n");
fputs($fh, "Content-type: application/x-www-form-urlencoded\n");
fputs($fh, "Content-length: ". strlen($pdata) ."\n");
fputs($fh, "User-agent: FFmpeg PHP Progress script\n");
fputs($fh, "Connection: close\n\n");
fputs($fh, $pdata);
fclose($fh);
return;
}
/**
* Validate Progress
*
* This validates that the progress file DOES exist and the {@link $fkey} has
* been set.
*
* @param void
* @return bool true if it does exist, false if not or fkey not set
* @access protected
* @since 0.1
*/
protected function validateProgress()
{
if( strlen($this->fkey) < 1 )
$this->addError('The `$fkey` property is not set! LINE:'. __LINE__);
return strlen($this->progressLog) > 0 && file_exists($this->logPath . $this->progressLog);
}
/**
* JSON-encoded Status
*
* Retrieves the current encoding time and total time, then outputs the data
* in a JSON-encoded array.
*
* This is used when polling for progress updates.
*
* @param void
* @return void
* @access public
* @since 0.1
*/
public function jsonStatus()
{
// Get the current Encoded time
$eTime = $this->getEncodedTime();
// Get the total Length time
$tTime = $this->getTotalTime();
$array = array(
'time_encoded' => $eTime,
'time_total' => $tTime,
'time_encoded_min' => sec2min($eTime),
'time_total_min' => sec2min($tTime)
);
json_response($array);
}
/**
* Get Encoded Time
*
* Sort of like "elapsed" time, this retrieves the number of seconds that have
* been processed of the video so far.
* This gets called by the {@link jsonStatus()} method.
*
* @param void
* @return integer Number of encoded seconds of the video
* @access protected
* @since 0.1
*/
protected function getEncodedTime()
{
return $this->parseLogTime('encoded');
}
/**
* Get Total Time
*
* Retrieves the total number of seconds of the video's length.
* This gets called by the {@link jsonStatus()} method.
*
* @param void
* @return integer Number of total seconds of the video
* @access protected
* @since 0.1
*/
protected function getTotalTime()
{
return $this->parseLogTime('total');
}
/**
* Parse Log Time
*
* Parses the {@link $progressLog} time and returns the requested type of seconds.
*
* @param string $type Either 'total' or 'encoded'
* @return integer Number of seconds for requested value
* @access protected
* @since 0.1
*/
protected function parseLogTime( $type )
{
// Make sure a valid type is being requested
if( $type != 'total' && $type != 'encoded' )
{
$err = 'Invalid Log time type `'. $type .'`!';
$this->addError($err);
exit($err);
}
// Validate the progress file
if(!$this->validateProgress())
{
$err = 'ffmpeg-progress: FFMPEG progress log does not exist! FILE: `'.
$this->logPath . $this->progressLog .'`';
$this->addError($err);
exit($err);
}
// Determine the correct set of separation values
if( $type == 'encoded' )
$eKey = array('time=', ' bitrate=');
else
$eKey = array('Duration: ', ', start: ');
// Open and parse the log file
$contents = file_get_contents($this->logPath . $this->progressLog);
$times = explode($eKey[0], $contents);
$ctime = count($times) - 1;
$timed = explode($eKey[1], $times[$ctime]);
$tt = explode(':', $timed[0]);
// Calculate total seconds ... cannot do $tt[0] * 3600 + $tt[1] * 60 + $tt[2]
// since this was returning invalid values...
$hsec = $tt[0] * 3600;
$msec = $tt[1] * 60;
$sec = $tt[2];
$ttsec = $hsec + $msec + $sec;
// Return rounded seconds
return round($ttsec);
}
/**
* Log Errors
*
* Writes any current errors in the {@link $errors} property to the log and clears
* the {@link $errors} property so duplicates won't be written.
*
* @param void
* @return void
* @access public
* @since 0.1
*/
public function logErrors()
{
foreach($this->error as $errMsg) {
$this->writeLog($errMsg, date('d-m-y') .'.error.log');
}
// Reset error message array since they've been logged ...
$this->error = array();
}
/**
* Write Status Message to Log
*
* Writes informative messages to a status log (NOT the progress log) that may
* be helpful for debugging purposes.
*
* @param string The status message to write.
* @return void
* @access public
* @since 0.1
*/
public function writeStatus( $msg )
{
if( strlen($msg) < 1 )
return;
$this->writeLog($msg, date('d-m-y') .'.status.log');
return;
}
/**
* Add Error Message
*
* Adds an error message to the {@link $error} property so it will be added to
* the error log when it is written.
*
* @param string $msg The error message to add
* @return void
* @access public
* @since 0.1
*/
public function addError( $msg )
{
$this->error[] = $msg;
$this->logErrors();
}
/**
* Write Log Data
*
* Used to write the log data based on given params.
* It's responsible for formatting the log file entry and then writing it to
* the logfile specified by the $file parameter.
*
* @param string $msg The message to write to the log file.
* @param string $file The log file's filename to write given message to.
* This should NOT contain the path, just filename only.
* @return void
* @access protected
* @since 0.1
*/
protected function writeLog($msg, $file)
{
// Log file entry format
$logf = '['. date('d.m.y H:i:s') .'] ['. $_SERVER['REMOTE_ADDR'] ."] %s \n";
// If the log file wasn't specified, append a log entry alerting us of it ...
if( strlen($file) < 4 )
{
$filename = $this->logPath . date('d-m-y') .'.error.log';
$this->addError('writeLog() called without valid $file parameter! '.
'LINE:'. __LINE__ .' FILE: '. __FILE__);
}
else
{
$filename = $this->logPath . $file;
}
$logStr = sprintf($logf, $msg);
// Write the log file data
$hdl = fopen($filename, 'a+');
fwrite($hdl, $logStr);
fclose($hdl);
return;
}
}