如何使用YouTube API获取视频时长?

11
我想获取一个Youtube视频的时长。这是我尝试过的代码:

Here's the code I tried:


$vidID="voNEBqRZmBc";
//http://www.youtube.com/watch?v=voNEBqRZmBc
$url = "http://gdata.youtube.com/feeds/api/videos/". $vidID;
$doc = new DOMDocument;
$doc->load($url);
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;

请检查XML文件。我已获得此视频的标题。我想从<yt:duration seconds='29'/>中获取持续时间。

如何获取<yt>标签中的seconds属性?

6个回答

17

由于即将废弃YouTube v2 API,这里提供更新的解决方案。

  1. 首先克隆Google API PHP客户端这里
  2. 接下来,您需要在Google 这里注册一个应用程序,创建一个新项目,在“API和认证→API”部分下启用YouTube Data API,并
  3. 在“API和认证→凭据”下创建一个新的OAuth Client ID,并将完整路径添加到您的重定向URI列表中。 (即http://example.com/get_youtube_duration.php
  4. 使用代码(该代码是从示例代码改编而来),同时确保填写$OAUTH2_CLIENT_ID和$OAUTH2_CLIENT_SECRET变量与步骤3中自己的值相对应。
  5. 为$videoId变量硬编码一个值或使用video_id获取参数进行设置(即http://example.com/get_youtube_duration.php?video_id=XXXXXXX
  6. 访问您的脚本,点击授权应用程序的链接,并使用您的Google帐户获取令牌,然后重定向回代码并显示持续时间。

这是您的输出:

Video Duration

0 mins

29 secs

一些额外的资源: https://developers.google.com/youtube/v3/docs/videos#contentDetails.duration https://developers.google.com/youtube/v3/docs/videos/list

以下仅适用于V2 API。

这对我起作用是基于 feed 中只有一个 duration 元素的假设。

<?php

$vidID="voNEBqRZmBc";
//http://www.youtube.com/watch?v=voNEBqRZmBc
$url = "http://gdata.youtube.com/feeds/api/videos/". $vidID;
$doc = new DOMDocument;
$doc->load($url);
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
$duration = $doc->getElementsByTagName('duration')->item(0)->getAttribute('seconds');

print "TITLE: ".$title."<br />";
print "Duration: ".$duration ."<br />";

输出:

TITLE: Introducing iTime
Duration: 29

1
这将在2015年4月API 2停用后停止工作。你知道有什么替代方案吗? - Daniele B
嗨,Daniele - 暂时我不清楚,但我会去看看并了解一下。 - Chris Rasco
1
@DanieleB 我已经更新了我的答案。我没有深入研究如何在没有浏览器用户交互的情况下进行身份验证,但上述代码可以与新的API一起使用。 - Chris Rasco

11

这里是 YouTube API V3,附有获取视频时长的示例,但别忘了要在 https://console.developers.google.com/project 创建你自己的 API 密钥。

     $vidkey = "cHPMH2sa2Q" ; video key for example 
$apikey = "xxxxxxxxxxxxxxxxxxxxx" ;
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$vidkey&key=$apikey");
$VidDuration =json_decode($dur, true);
foreach ($VidDuration['items'] as $vidTime) 
{
$VidDuration= $vidTime['contentDetails']['duration'];
}

1
感谢Alaa Sadik的回答。 :) - Developer
持续时间似乎不是正确的格式。 - Sizzling Code

3

使用SimpleXML的简短而简单的解决方案:

function getYouTubeVideoDuration($id) {
 $xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/'.$id);
 return strval($xml->xpath('//yt:duration[@seconds]')[0]->attributes()->seconds);
}

@Pat:给你链接:http://codepad.viper-7.com/ttfsag - Amal Murali
1
谢谢...哦!我刚刚被瑞克滚了!我不知道人们还会这样做,哈哈,谢谢,我需要一个好笑话。 - tacudtap
GData API仍然有效吗? - Nimrod007
我认为这个已经不起作用了。 - MastaBaba

2

所有之前的答案都以文本格式提供时间长度:

这个解决方案将为您提供小时/分钟/秒,让您可以将其转换为任何您想要的格式:

function getDurationInSeconds($talkId){
   $vidkey = $talkId;
   $apikey = "xxxxx";
   $dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$vidkey&key=$apikey");
   $VidDuration =json_decode($dur, true);
   foreach ($VidDuration['items'] as $vidTime)
   {
       $VidDuration= $vidTime['contentDetails']['duration'];
   }
   preg_match_all('/(\d+)/', $VidDuration, $parts);
   $hours = intval(floor($parts[0][0]/60) * 60 * 60);
   $minutes = intval($parts[0][0]%60 * 60);
   $seconds = intval($parts[0][1]);
   $totalSec = $hours + $minutes + $seconds; // This is the example in seconds
   return $totalSec;
}

duration: "PT26M"怎么样? - Hamzeh Soboh
PT1H21S怎么样? - Huzoor Bux

0

非常容易

function getYoutubeDuration($videoid) {
      $xml = simplexml_load_file('https://gdata.youtube.com/feeds/api/videos/' . $videoid . '?v=2');
      $result = $xml->xpath('//yt:duration[@seconds]');
      $total_seconds = (int) $result[0]->attributes()->seconds;

      return $total_seconds;
}

//now call this pretty function. As parameter I gave a video id to my function and bam!
echo getYoutubeDuration("y5nKxHn4yVA");

谷歌数据 API 还能用吗? - Nimrod007

-2

你也可以用JSON获取时长

       $video_id = "<VIDEO_ID>";
       http://gdata.youtube.com/feeds/api/videos/$video_id?v=2&alt=jsonc

哦!抱歉,您可以通过以下方式获得:

$duration = $xml->getElementsByTagName('duration')->item(0)->getAttribute('seconds');

请再仔细阅读我的问题。 - Developer
GData API仍然有效吗? - Nimrod007
@Nimrod007 不是。 - Skora

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接