2011-03-01 99 views
12

在我正在開發的網站上,我嵌入了來自YouTube的視頻並希望獲得視頻標題及其說明。獲取嵌入式YouTube視頻的標題和說明

如何獲取該信息?

+0

我在這裏創建了一個函數來解決這個問題:http://stackoverflow.com/a/13786724/1078904 – 2012-12-09 12:19:15

回答

10

爲了得到描述元素,你需要訪問的的GDATA版本視頻的信息,並且您可以在路徑上使用alt = json返回json。在這種情況下,oHg5SJYRHA0是在YouTube上使用的視頻的網址結尾處找到的視頻ID,例如, www.youtube.com/watch?v=oHg5SJYRHA0

http://gdata.youtube.com/feeds/api/videos/oHg5SJYRHA0?v=2&alt=json&prettyprint=true

(該prettyprint被格式化,以使該易於閱讀,你不需要它,你在做什麼)

你可以抓住的JSON,將其添加到可變的,並且使用訪問它的jQuery:

var youTubeURL = 'http://gdata.youtube.com/feeds/api/videos/oHg5SJYRHA0?v=2&alt=json'; 
var json = (function() { 
    var json = null; 
    $.ajax({ 
     'async': false, 
     'global': false, 
     'url': youTubeURL, 
     'dataType': "json", 
     'success': function(data) { 
      json = data; 
     } 
    }); 
    return json; 
})(); 

然後,使用對象表示法訪問它

+0

標題和說明屬性是否已棄用?我爲標題和說明獲取「https://youtube.com/devicesupport」值。 – 2015-05-12 13:31:36

+0

@SheebanSingaram yikes。是的,它看起來像,我在YouTube API V2.0頁面上看到了棄用警告:https://developers.google.com/youtube/2.0/developers_guide_protocol。我會看看這個並更新我的答案。 – 2015-05-12 21:37:38

+0

pLease切換到API V3.0 – 2015-05-20 09:32:11

6

你可以用oembed來完成。 例子:

 
http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3DM3r2XDceM6A&format=json 
+5

似乎無法獲得描述。 – ZYiOS 2012-06-28 12:10:44

+1

描述不包含在oEmbed數據中,如鏈接到的文檔頁面所示。這個答案沒有解決這個問題。 – 2014-06-13 16:24:28

1

我有點遲疑地閱讀了這個話題。 我做了這樣的事情使用JSON和YT API的

$json = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos/".$rs['vid']."?v=2&prettyprint=true&alt=jsonc")); 

注:$ RS [ 'VID']從我的數據庫dinamically retrived視頻ID。

一旦你把內容在手柄$ JSON,你可以retrive這樣的:

$json->data->description; 
$json->data->title; 

用var_dump($ JSON),查看您可以訪問所有的值。

9

Youtube API V2.0已被棄用。它顯示標題「youtube.com/devicesupport」的某些錯誤值。 pLease打開到API V3.0

你可以引用下面的PHP代碼並根據你的需要修改你的js或jquery。

function youtube_title($id) { 
$id = 'YOUTUBE_ID'; 
// returns a single line of JSON that contains the video title. Not a giant request. 
$videoTitle = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$id."&key=YOUR_API_KEY&fields=items(id,snippet(title),statistics)&part=snippet,statistics"); 
// despite @ suppress, it will be false if it fails 
if ($videoTitle) { 
$json = json_decode($videoTitle, true); 

return $json['items'][0]['snippet']['title']; 
} else { 
return false; 
} 
} 

更新:

jQuery代碼,以獲得標題 -

$.getJSON('https://www.googleapis.com/youtube/v3/videos?id={VIDEOID}&key={YOUR API KEY}&part=snippet&callback=?',function(data){ 
    if (typeof(data.items[0]) != "undefined") { 
     console.log('video exists ' + data.items[0].snippet.title); 
     } else { 
     console.log('video not exists'); 
    } 
    }); 
+0

如果你沒有一個API密鑰,只想要標題,該怎麼辦? – SomeRandomName 2015-05-20 14:39:51

+0

在v2.0中是可能的,在v3.0中你只需要api密鑰..並且獲得api密鑰並不困難...... – 2015-05-21 10:49:22

+0

我得到了一個解決方案,我們可以得到沒有API密鑰的標題...通過使用下面的鏈接http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=videoid&format=json ...但事情是videoid應該是已知的。 – 2015-05-22 05:39:35

-1

的GData已被棄用,但仍然可以通過調用該端點獲得視頻說明:

https://www.googleapis.com/youtube/v3/videos?part=snippet&id=[video_id]&key=[api_key]

它將返回形式的響應:

{ 
"kind": "youtube#videoListResponse", 
"etag": "\"...\"", 
"pageInfo": { 
    "totalResults": 1, 
    "resultsPerPage": 1 
}, 
"items": [ 
    { 
    "kind": "youtube#video", 
    "etag": "\"...\"", 
    "id": "...", 
    "snippet": { 
    "publishedAt": "...", 
    "channelId": "...", 
    "title": "...", 
    "description": "...", 
    "thumbnails": { ... }, 
    "channelTitle": "...", 
    "tags": [ ... ], 
    "categoryId": "...", 
    "liveBroadcastContent": "...", 
    "localized": { 
    "title": "...", 
    "description": "..." 
    }, 
    "defaultAudioLanguage": "..." 
    } 
    } 
] 
} 

描述可以發現在items.localized.description