Skip to content

Instantly share code, notes, and snippets.

@atomuseq
Last active August 11, 2016 22:38
Show Gist options
  • Save atomuseq/1a66cda39920fb3b64ce to your computer and use it in GitHub Desktop.
Save atomuseq/1a66cda39920fb3b64ce to your computer and use it in GitHub Desktop.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2016 Eugene Gavalidi <atomuseq@gmail.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
<?php
class Movie
{
protected $results;
public function __construct($movie)
{
$this->results = $this->executable($movie);
}
public function getDuration($type = false)
{
if (preg_match($this->regex('Duration'), $this->results(), $matches))
{
list(, $hours, $minutes, $seconds) = $matches;
if ($type === false)
{
$hours = $hours !== '00' ? "{$hours}:" : '';
$minutes = $minutes !== '00' ? "{$minutes}:" : '00:';
return $hours . $minutes . $seconds;
}
return ($hours * 3600) + ($minutes * 60) + $seconds;
}
return false;
}
public function getFrameSize($type = false)
{
if (preg_match($this->regex('FrameSize'), $this->results(), $matches))
{
list(, $width, $height) = $matches;
if ($type === false)
{
return $width . 'x' . $height;
}
return compact('width', 'height');
}
return false;
}
public function getFrameRate()
{
if (preg_match($this->regex('FrameRate'), $this->results(), $matches))
{
list(, $fps) = $matches;
return round($fps) . ' fps';
}
return false;
}
public function getQuality()
{
$frameSize = $this->getFrameSize(true);
return $frameSize['height'] . 'p';
}
public function getVideoCodec($type = false)
{
if (preg_match($this->regex('VideoCodec'), $this->results(), $matches))
{
list(, $codec, $information) = $matches;
if ($type === false)
{
return $codec;
}
return $codec . ' ' . $information;
}
return false;
}
public function getAudioCodec($type = false)
{
if (preg_match($this->regex('AudioCodec'), $this->results(), $matches))
{
list(, $codec, $information) = $matches;
if ($type === false)
{
return $codec;
}
return $codec . ' ' . $information;
}
return false;
}
public function results()
{
return $this->results;
}
public function makeThumbnail($path, $name, $count = 1)
{
for($i = 1; $i <= $count; ++$i)
{
$thumbnail = $count > 1 ? $name . '_' . $i . '.jpg' : $name . '.jpg';
exec(sprintf('ffmpeg -i %s -an -ss %s -r 1 -vframes 1 -y %s 2>&1',
$this->movie, rand(1, $this->getDuration(true)), $path . '/' . $thumbnail
));
}
}
protected function regex($name)
{
$regex = [
'Duration' => '/Duration: ([0-9]{2}):([0-9]{2}):([0-9]{2})(\.([0-9]+))?/',
'FrameSize' => '/Video:.+?([1-9][0-9]*)x([1-9][0-9]*)/',
'FrameRate' => '/([0-9\.]+)\sfps,\s/',
'VideoCodec' => '/Video:\s(.*?)\s([^,]+),/',
'AudioCodec' => '/Audio:\s(.*?)\s([^,]+),/',
];
if ($name && key_exists($name, $regex))
{
return $regex[$name];
}
return false;
}
protected function executable($movie)
{
if (is_file($movie))
{
exec('ffmpeg -i ' . escapeshellarg($movie) . ' 2>&1', $output, $value);
$results = join(PHP_EOL, $output);
if (! preg_match('/FFmpeg version/i', $results))
{
throw new Exception('FFmpeg is not installed');
}
return $results;
}
return false;
}
}
<?php
require 'Movie.php';
$movie = new Movie('movie.mp4');
echo $movie->getDuration() . PHP_EOL; // 03:23
echo $movie->getFrameSize() . PHP_EOL; // 1280x720
echo $movie->getFrameRate() . PHP_EOL; // 24 fps
echo $movie->getQuality() . PHP_EOL; // 720p
echo $movie->getVideoCodec() . PHP_EOL; // h264
echo $movie->getAudioCodec() . PHP_EOL; // aac
echo $movie->getDuration(true) . PHP_EOL; // int (seconds)
echo $movie->getFrameSize(true) . PHP_EOL; // array (width, height)
echo $movie->getVideoCodec(true) . PHP_EOL; // string "h264 (High) (avc1 / 0x31637661)"
echo $movie->getAudioCodec(true) . PHP_EOL; // string "aac (mp4a / 0x6134706D)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment