Skip to content

Instantly share code, notes, and snippets.

@gagimilicevic
Created March 24, 2020 17:01
Show Gist options
  • Save gagimilicevic/d7c9af1f92311153d5857154b80cfccd to your computer and use it in GitHub Desktop.
Save gagimilicevic/d7c9af1f92311153d5857154b80cfccd to your computer and use it in GitHub Desktop.
Vimeo video - get duration
https://stackoverflow.com/questions/7772825/how-to-get-length-of-a-vimeo-video
echo vimeoVideoDuration('https://vimeo.com/115134273');
// output: 63 (video duration in seconds)
/**
* Vimeo video duration in seconds
*
* @param $video_url
* @return integer|null Duration in seconds or null on error
*/
function vimeoVideoDuration($video_url) {
$video_id = (int)substr(parse_url($video_url, PHP_URL_PATH), 1);
$json_url = 'http://vimeo.com/api/v2/video/' . $video_id . '.xml';
$ch = curl_init($json_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$data = new SimpleXmlElement($data, LIBXML_NOCDATA);
if (!isset($data->video->duration)) {
return null;
}
$duration = $data->video->duration;
return $duration; // in seconds
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment