├── README.md └── ffprobe.php /README.md: -------------------------------------------------------------------------------- 1 | php-ffprobe 2 | =========== 3 | 4 | PHP wrapper class for `ffprobe` media prober. 5 | 6 | This simple class was born [as a response](http://stackoverflow.com/a/11807265/222758) on StackOverflow. 7 | 8 | I had plans to further improve it a lot and release as a Composer package, but in the mean time I began to be very busy at work and had no time to continue. 9 | 10 | Luckily, at the daily rush I found the project [PHP-FFMpeg](https://github.com/PHP-FFMpeg/PHP-FFMpeg) which is far beyond anything that I had planned to do. 11 | Since I'm using this great package in my projects, I have plans to be contributing on it to make it even better to the community. I strongly recommend it! 12 | 13 | And if you want to make your own wrapper for `ffprobe`, this base class should help you get a start. :) 14 | -------------------------------------------------------------------------------- /ffprobe.php: -------------------------------------------------------------------------------- 1 | under CC BY-SA 3.0 license 6 | */ 7 | class ffprobe 8 | { 9 | public function __construct($filename, $prettify = false) 10 | { 11 | if (!file_exists($filename)) { 12 | throw new Exception(sprintf('File not exists: %s', $filename)); 13 | } 14 | 15 | $this->__metadata = $this->__probe($filename, $prettify); 16 | } 17 | 18 | private function __probe($filename, $prettify) 19 | { 20 | // Start time 21 | $init = microtime(true); 22 | 23 | // Default options 24 | $options = '-loglevel quiet -show_format -show_streams -print_format json'; 25 | 26 | if ($prettify) { 27 | $options .= ' -pretty'; 28 | } 29 | 30 | // Avoid escapeshellarg() issues with UTF-8 filenames 31 | setlocale(LC_CTYPE, 'en_US.UTF-8'); 32 | 33 | // Run the ffprobe, save the JSON output then decode 34 | $json = json_decode(shell_exec(sprintf('ffprobe %s %s', $options, 35 | escapeshellarg($filename)))); 36 | 37 | if (!isset($json->format)) { 38 | throw new Exception('Unsupported file type'); 39 | } 40 | 41 | // Save parse time (milliseconds) 42 | $this->parse_time = round((microtime(true) - $init) * 1000); 43 | 44 | return $json; 45 | } 46 | 47 | public function __get($key) 48 | { 49 | if (isset($this->__metadata->$key)) { 50 | return $this->__metadata->$key; 51 | } 52 | 53 | throw new Exception(sprintf('Undefined property: %s', $key)); 54 | } 55 | } 56 | --------------------------------------------------------------------------------