如何获取 vimeo 视频的长度?

发布于 2024-12-10 03:20:44 字数 188 浏览 3 评论 0原文

我有一个大问题。问题是如何获取vimeo视频的时长?这是场景。

我在此字段中有一个输入字段,表示我现在输入 YouTube 网址 验证视频长度只能为 1 分钟,如果是,那么我 将其存储在数据库中,否则我会显示错误消息。

是否可以对 vimeo 视频文件执行此操作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

奈何桥上唱咆哮 2024-12-17 03:20:44

用法

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
}

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
}
疑心病 2024-12-17 03:20:44

Vimeo 现在有一个新的 API,请在此处查看:
vimeo api

,您所需要的只是:
1. 在您的 vimeo 帐户下创建一个应用程序 https://developer.vimeo.com/apps< br>
2. 获取您的 vimeo 应用程序的 client_id、client_secret、client_token;
3. 使用官方库之一(php、python、node): https://github.com/vimeo

这非常简单,但不要忘记优化我们的 API 调用,如下所示,因为 vimeo api 有速率限制:
https://developer.vimeo.com/api/common-formats#json-过滤器

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

长途伴 2024-12-17 03:20:44

是的,事实上它在他们的 Simple API

http:// vimeo.com/api/docs/simple-api

持续时间视频的持续时间(以秒为单位)

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

許願樹丅啲祈禱 2024-12-17 03:20:44

根据视频的不同,您可能需要进行身份验证。
这是我制作的代码:

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;
    }

可以在此处请求授权代码。

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.

只为一人 2024-12-17 03:20:44
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; 
} 

echo vimeoVideoDuration('https://vimeo.com/547217109');

// 以秒为单位返回

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; 
} 

echo vimeoVideoDuration('https://vimeo.com/547217109');

// return in seconds

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文