Skip to content

Instantly share code, notes, and snippets.

@vdonchev
Forked from xeoncross/video_duration.php
Created February 15, 2021 06:41
Show Gist options
  • Save vdonchev/6991078bb4cb2fe2647375704d3785dc to your computer and use it in GitHub Desktop.
Save vdonchev/6991078bb4cb2fe2647375704d3785dc to your computer and use it in GitHub Desktop.
Get video duration from ffmpeg
<?php
$file = 'TrunkMonkey_Rescue.mov';
$result = shell_exec('ffmpeg -i ' . escapeshellcmd($file) . ' 2>&1');
preg_match('/(?<=Duration: )(\d{2}:\d{2}:\d{2})\.\d{2}/', $result, $match);
print_r($match);
/*
Array
(
[0] => 00:00:30.23
)
*/
$time = explode(':', $match[1]) + array(00,00,00);
print_r($time);
$half = floor(join($time) / 2);
$half = join(':', str_split(str_pad($half, 6, 0, STR_PAD_LEFT), 2));
print $half . "\n";
$video = __DIR__ . '/' . $file;
$thumbnail = __DIR__ . '/'. $file . '.jpg';
// -ss = Seek to given time position in seconds.
// -an = Disable audio recording
// -r fps = Set frame rate
$result = shell_exec("ffmpeg -i $video -deinterlace -an -ss $half -t $half -r 1 -y -vcodec mjpeg -f mjpeg $thumbnail 2>&1");
// Even on failure, $result will be a string of all the output so I don't think this is needed
if( ! $result)
{
throw new Exception('Error creating video thumbnail');
}
print $result . "\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment