如何使用YouTube API来获取视频的持续时间? [英] How to get video duration using YouTube API?
问题描述
我想YouTube视频的持续时间。这里的code我尝试:
$ vidID =voNEBqRZmBc;
//http://www.youtube.com/watch?v=voNEBqRZmBc
$ URL =http://gdata.youtube.com/feeds/api/videos/。 $ vidID;
$ DOC =新的DOMDocument;
$ doc->负载($网址);
$标题= $ doc->的getElementsByTagName(称号) - >项目(0) - >的nodeValue;
请检查XML文件。我得到这个视频的标题。我想从&LT得到持续的时间; YT:持续时间秒= '29'/>
如何让秒
属性此< YouTube和GT;
标签
由于YouTube版API即将去precation,这里是一个更新的解决方案。
- 将在这里克隆谷歌API PHP客户端 启动
- 接下来,你需要创建一个新项目注册到这里谷歌的应用程序,打开YouTube数据API在API和验证]> API部分,和
- 创建的API和权威性>凭据的新的OAuth客户端ID和完整路径添加到您的重定向的URI列表。 (即<一href=\"http://example.com/get_youtube_duration.php\">http://example.com/get_youtube_duration.php)
- 使用这 code(这是改编自<一个href=\"https://developers.google.com/youtube/v3/$c$c_samples/php#updating_a_video_by_adding_new_tags\">this样品code),同时确保填写$ OAUTH2_CLIENT_ID和$ OAUTH2_CLIENT_SECRET变量与自己的价值观,从第3步。
- 硬code为$ VIDEOID变量的值或将其设置使用VIDEO_ID得到的参数(即<一href=\"http://example.com/get_youtube_duration.php?video_id=XXXXXXX\">http://example.com/get_youtube_duration.php?video_id=XXXXXXX)
- 访问你的脚本,点击链接授权应用程序和使用您的谷歌帐户,获得令牌,该令牌重定向你回到你的code和显示的时间。
下面是你的输出:
视频时长0分钟29秒
一些额外的资源:
<一href=\"https://developers.google.com/youtube/v3/docs/videos#contentDetails.duration\">https://developers.google.com/youtube/v3/docs/videos#contentDetails.duration
<一href=\"https://developers.google.com/youtube/v3/docs/videos/list\">https://developers.google.com/youtube/v3/docs/videos/list
下面的作品只有V2 API。
这为我工作基础上,假设只有1在饲料中持续时间元素。
&LT; PHP$ vidID =voNEBqRZmBc;
//http://www.youtube.com/watch?v=voNEBqRZmBc
$ URL =http://gdata.youtube.com/feeds/api/videos/。 $ vidID;
$ DOC =新的DOMDocument;
$ doc-&GT;负载($网址);
$标题= $ doc-&GT;的getElementsByTagName(称号) - &GT;项目(0) - &GT;的nodeValue;
$持续时间= $ doc-&GT;的getElementsByTagName('时间') - &GT;项(0) - &GT;的getAttribute(秒);打印标题:$标题&LT; BR /&gt;中;
打印时间:$时间&LT; BR /&gt;中;
输出:
标题:ITIME简介
持续时间:29
I want to get the duration of a Youtube video. 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;
Please check the XML file. I got the title of this video. I want to get duration from <yt:duration seconds='29'/>
.
How to get the seconds
attribute this <yt>
tag?
Due to the upcoming deprecation of the Youtube v2 API, here is an updated solution.
- Start by cloning the Google API PHP Client here
- Next, you'll want to register an application with Google here by creating a new Project, turning on the Youtube Data API under the "APIs and auth > API" section, and
- Create a new OAuth Client ID under "APIs and auth > Credentials" and add the full path to your redirect uri list. (i.e. http://example.com/get_youtube_duration.php)
- Use this code (which was adapted from this sample code), while making sure to fill in the $OAUTH2_CLIENT_ID and $OAUTH2_CLIENT_SECRET variables with your own values from step 3.
- Hardcode a value for the $videoId variable or set it using the video_id get parameter (i.e. http://example.com/get_youtube_duration.php?video_id=XXXXXXX)
- Visit your script, click the link to authorize the app and use your google account to get a token, which redirects you back to your code and will display the duration.
Here is your output:
Video Duration
0 mins
29 secs
Some additional resources: https://developers.google.com/youtube/v3/docs/videos#contentDetails.duration https://developers.google.com/youtube/v3/docs/videos/list
The following works for the V2 API only.
This worked for me based on the assumption that there is only 1 duration element in the feed.
<?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 />";
Output:
TITLE: Introducing iTime
Duration: 29
这篇关于如何使用YouTube API来获取视频的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!