forked from collindavis/ffmpeg-crawler
/
ffprobe.php
78 lines (64 loc) · 2.01 KB
/
ffprobe.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
<?php
//taken from https://github.com/paulofreitas/php-ffprobe
/*
* ffprobe class helper for ffmpeg 0.9+ (JSON support)
* Written by Paulo Freitas <me@paulofreitas.me> under CC BY-SA 3.0 license
*/
include_once('cli_colors.php');
class ffprobe
{
public function __construct($filename, $prettify = false)
{
if (!file_exists($filename)) {
throw new Exception(sprintf('File not exists: %s', $filename));
//str_replace(' ', "\\ ", $filename)
}
$this->__metadata = $this->__probe($filename, $prettify);
}
private function __probe($filename, $prettify)
{
// Start time
$init = microtime(true);
// Default options
$options = '-loglevel quiet -show_format -show_streams -print_format json';
if ($prettify)
{
$options .= ' -pretty';
}
// Avoid escapeshellarg() issues with UTF-8 filenames
setlocale(LC_CTYPE, 'en_US.UTF-8');
//$command = sprintf('ffprobe %s %s ', $options, escapeshellarg($filename));
$command = sprintf('./ffprobe %s "%s" ', $options, $filename);
// Run the ffprobe, save the JSON output then decode
$json = json_decode(shell_exec($command), true);
//if (!isset($json->format))
if (!isset($json['format']))
{
//throw new Exception('Unsupported file type: '.$filename."\r\nCommand:\r\n".$command."\r\n");
$output = 'ffprobe: Unsupported file type: '.$filename."\r\nCommand:\r\n".$command."\r\n";
$colors = new Colors();
$colors->getColoredString($output, 'black', 'red');
//The command finished with an error.
$error = R::dispense('error');
//$video->ownError = array($error);
$error->raw_output = $output;
$error->repaired = false;
$id = R::store($error);
}
// Save parse time (milliseconds)
$this->parse_time = round((microtime(true) - $init) * 1000);
return $json;
}
public function get($key)
{
if (isset($this->__metadata->$key))
{
return $this->__metadata->$key;
}
throw new Exception(sprintf('Undefined property: %s', $key));
}
public function get_all()
{
return $this->__metadata;
}
}