问题
I am having a big problem. Problem is how to get the time duration of vimeo video? Here is the scenario.
I have a input field in this field say i enter youtube url now i want to put a validation that video should only be of 1 min, if yes then i store this in database else i show an error message.
is it possible to do this thing for vimeo video files?
回答1:
Usage
echo vimeoVideoDuration('https://vimeo.com/115134273');
// output: 63 (video duration in seconds)
Function
/**
* 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
}
回答2:
Yes, as a matter of fact its in their Simple API
http://vimeo.com/api/docs/simple-api
duration Duration of the video in seconds
回答3:
Depending on the video, you might have to authenticate. Here is a code I've made:
public function getVimeoVideoDuration($vimeoId)
{
$authorization = 'myaccesstoken';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.vimeo.com/videos/{$vimeoId}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer {$authorization}",
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if (empty($err)) {
$info = json_decode($response);
if(isset($info->duration)){
return (int)$info->duration;
}
}
return false;
}
The authorization code can be requested here.
回答4:
Vimeo now has a new API, check here: vimeo api
and all you need is :
1. create a app under your vimeo account here https://developer.vimeo.com/apps
2. get your client_id, client_secret, client_token of your vimeo app;
3. use one of those official lib(php, python, node): https://github.com/vimeo
it is very easy but don't forget to optimise our API call as here says because vimeo api has a rate limit: https://developer.vimeo.com/api/common-formats#json-filter
来源:https://stackoverflow.com/questions/7772825/how-to-get-length-of-a-vimeo-video