用于获取频道上所有视频的YouTube API
我们需要YouTube频道名称的视频列表(使用API) 我们可以使用以下API获得频道列表(仅频道名称):用于获取频道上所有视频的YouTube API,youtube,youtube-api,Youtube,Youtube Api,我们需要YouTube频道名称的视频列表(使用API) 我们可以使用以下API获得频道列表(仅频道名称): https://gdata.youtube.com/feeds/api/channels?v=2&q=tendulkar 以下是频道的直接链接 https://www.youtube.com/channel/UCqAEtEr0A0Eo2IVcuWBfB9g 或 现在,我们需要频道>>UCQaeter0A0EO2IVCUWBF9G或HC-8jgBP-4rlI的视频 我们试过了
https://gdata.youtube.com/feeds/api/channels?v=2&q=tendulkar
以下是频道的直接链接
https://www.youtube.com/channel/UCqAEtEr0A0Eo2IVcuWBfB9g
或
现在,我们需要频道>>UCQaeter0A0EO2IVCUWBF9G或HC-8jgBP-4rlI的视频
我们试过了
但是,这没有帮助
我们需要频道上所有的视频。上传到一个频道的视频可以来自多个用户,因此我认为提供一个用户参数不会有任何帮助…使用API版本2(已弃用),上传的URL(频道UCQater0A0EO2IVCUWB9G)为:
有一个API版本3。如文档所述(),您可以使用频道资源类型和操作列表获取频道中的所有视频。必须使用参数“通道id”执行此操作。您需要查看。您将发现关于如何访问API的文档。你也可以找到 你也可以自己提出要求。以下是从频道检索最新视频的示例URL:
https://www.googleapis.com/youtube/v3/search?key={your_key_here}&channelId={channel_id_here}&part=snippet,id&order=date&maxResults=20
之后,您将收到一个带有视频ID和详细信息的JSON
,您可以这样构造视频URL:
http://www.youtube.com/watch?v={video_id_here}
试着用下面的方法。这可能对你有帮助
在这里,您可以指定频道名称和“q”,您可以指定搜索关键字。首先,您需要获取播放列表的ID,该列表表示从用户/频道上传的内容: 您可以使用
forUsername={username}
param指定用户名,或者指定mine=true
来获得您自己的用户名(您需要首先进行身份验证)。包括part=contentDetails
以查看播放列表
GEThttps://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=jambrose42&key={YOUR_API_KEY}
在结果中,“relatedPlaylists”
将包括“likes”
和“uploads”
播放列表。抓取“上传”
播放列表ID。还要注意“ID”
是您的频道ID,以备将来参考
接下来,获取该播放列表中的视频列表:
只需输入播放ID
GEThttps://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=50&playlistId=UUpRmvjdu3ixew5ahydZ67uA&key={YOUR_API_KEY}
只需三个步骤:
v3
中列出频道中的所有视频
有两个步骤:
https://www.googleapis.com/youtube/v3/channels?id={channel Id}&key={API key}&part=contentDetails
https://www.googleapis.com/youtube/v3/playlistItems?playlistId={“uploads”Id}&key={API key}&part=snippet&maxResults=50
最近,我不得不从一个频道检索所有视频,根据YouTube开发者文档:
其中,$service
是您的Google\u service\u YouTube
对象
因此,您必须从频道获取信息以检索“上载”播放列表,该列表实际上包含频道上载的所有视频:
如果此API是新的,我强烈建议将代码示例从默认代码段转换为完整示例
因此,从一个频道检索所有视频的基本代码可以是:
class YouTube
{
const DEV_KEY = 'YOUR_DEVELOPPER_KEY';
private $client;
private $youtube;
private $lastChannel;
public function __construct()
{
$this->client = new Google_Client();
$this->client->setDeveloperKey(self::DEV_KEY);
$this->youtube = new Google_Service_YouTube($this->client);
$this->lastChannel = false;
}
public function getChannelInfoFromName($channel_name)
{
if ($this->lastChannel && $this->lastChannel['modelData']['items'][0]['snippet']['title'] == $channel_name)
{
return $this->lastChannel;
}
$this->lastChannel = $this->youtube->channels->listChannels('snippet, contentDetails, statistics', array(
'forUsername' => $channel_name,
));
return ($this->lastChannel);
}
public function getVideosFromChannelName($channel_name, $max_result = 5)
{
$this->getChannelInfoFromName($channel_name);
$params = [
'playlistId' => $this->lastChannel['modelData']['items'][0]['contentDetails']['relatedPlaylists']['uploads'],
'maxResults'=> $max_result,
];
return ($this->youtube->playlistItems->listPlaylistItems('snippet,contentDetails', $params));
}
}
$yt = new YouTube();
echo '<pre>' . print_r($yt->getVideosFromChannelName('CHANNEL_NAME'), true) . '</pre>';
classyoutube
{
const DEV_KEY='YOUR_developer_KEY';
私人客户;
私用$youtube;
私人频道;
公共函数构造()
{
$this->client=new Google_client();
$this->client->setDeveloperKey(self::DEV_KEY);
$this->youtube=新的谷歌服务\u youtube($this->client);
$this->lastChannel=false;
}
公共函数getChannelInfoFromName($channel\u name)
{
如果($this->lastChannel&&$this->lastChannel['modelData']['items'][0]['snippet']['title']==$channel\u name)
{
返回$this->lastChannel;
}
$this->lastChannel=$this->youtube->channels->listChannels('snippet,contentDetails,statistics',数组(
“forUsername”=>$channel\u name,
));
返回($this->lastChannel);
}
公共函数getVideosFromChannelName($channel\u name,$max\u result=5)
{
$this->getChannelInfoFromName($channel\u name);
$params=[
'playlaid'=>$this->lastChannel['modelData']['items'][0]['contentDetails']['relatedPlaylists']['uploads'],
“maxResults”=>$max\u结果,
];
返回($this->youtube->playlitems->listplaylitems('snippet,contentDetails',$params));
}
}
$yt=新建YouTube();
回显“”。打印($yt->getVideosFromChannelName('CHANNEL\u NAME'),true)。“”;
因为回答这个问题的每个人都有问题,因为500视频限制这里有一个替代解决方案,使用Python 3中的youtube\u dl。另外,不需要API密钥
安装youtube\u dl:sudo pip3安装youtube dl
。ID将以UC开头。将频道的C替换为上传的U(即UU…),这是上传播放列表
使用youtube dl的播放列表下载功能。理想情况下,您不希望下载默认播放列表中的每个视频,而只下载元数据
示例(警告--需要几十分钟):
下面是一个不需要任何特殊包的Python替代方案。由普罗维
<?php
$baseUrl = 'https://www.googleapis.com/youtube/v3/';
// https://developers.google.com/youtube/v3/getting-started
$apiKey = 'API_KEY';
// If you don't know the channel ID see below
$channelId = 'CHANNEL_ID';
$params = [
'id'=> $channelId,
'part'=> 'contentDetails',
'key'=> $apiKey
];
$url = $baseUrl . 'channels?' . http_build_query($params);
$json = json_decode(file_get_contents($url), true);
$playlist = $json['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
$params = [
'part'=> 'snippet',
'playlistId' => $playlist,
'maxResults'=> '50',
'key'=> $apiKey
];
$url = $baseUrl . 'playlistItems?' . http_build_query($params);
$json = json_decode(file_get_contents($url), true);
$videos = [];
foreach($json['items'] as $video)
$videos[] = $video['snippet']['resourceId']['videoId'];
while(isset($json['nextPageToken'])){
$nextUrl = $url . '&pageToken=' . $json['nextPageToken'];
$json = json_decode(file_get_contents($nextUrl), true);
foreach($json['items'] as $video)
$videos[] = $video['snippet']['resourceId']['videoId'];
}
print_r($videos);
function playlistItemsListByPlaylistId($service, $part, $params) {
$params = array_filter($params);
$response = $service->playlistItems->listPlaylistItems(
$part,
$params
);
print_r($response);
}
playlistItemsListByPlaylistId($service,
'snippet,contentDetails',
array('maxResults' => 25, 'playlistId' => 'id of "uploads" playlist'));
class YouTube
{
const DEV_KEY = 'YOUR_DEVELOPPER_KEY';
private $client;
private $youtube;
private $lastChannel;
public function __construct()
{
$this->client = new Google_Client();
$this->client->setDeveloperKey(self::DEV_KEY);
$this->youtube = new Google_Service_YouTube($this->client);
$this->lastChannel = false;
}
public function getChannelInfoFromName($channel_name)
{
if ($this->lastChannel && $this->lastChannel['modelData']['items'][0]['snippet']['title'] == $channel_name)
{
return $this->lastChannel;
}
$this->lastChannel = $this->youtube->channels->listChannels('snippet, contentDetails, statistics', array(
'forUsername' => $channel_name,
));
return ($this->lastChannel);
}
public function getVideosFromChannelName($channel_name, $max_result = 5)
{
$this->getChannelInfoFromName($channel_name);
$params = [
'playlistId' => $this->lastChannel['modelData']['items'][0]['contentDetails']['relatedPlaylists']['uploads'],
'maxResults'=> $max_result,
];
return ($this->youtube->playlistItems->listPlaylistItems('snippet,contentDetails', $params));
}
}
$yt = new YouTube();
echo '<pre>' . print_r($yt->getVideosFromChannelName('CHANNEL_NAME'), true) . '</pre>';
import youtube_dl, pickle
# UCVTyTA7-g9nopHeHbeuvpRA is the channel id (1517+ videos)
PLAYLIST_ID = 'UUVTyTA7-g9nopHeHbeuvpRA' # Late Night with Seth Meyers
with youtube_dl.YoutubeDL({'ignoreerrors': True}) as ydl:
playd = ydl.extract_info(PLAYLIST_ID, download=False)
with open('playlist.pickle', 'wb') as f:
pickle.dump(playd, f, pickle.HIGHEST_PROTOCOL)
vids = [vid for vid in playd['entries'] if 'A Closer Look' in vid['title']]
print(sum('Trump' in vid['title'] for vid in vids), '/', len(vids))
import urllib
import json
def get_all_video_in_channel(channel_id):
api_key = YOUR API KEY
base_video_url = 'https://www.youtube.com/watch?v='
base_search_url = 'https://www.googleapis.com/youtube/v3/search?'
first_url = base_search_url+'key={}&channelId={}&part=snippet,id&order=date&maxResults=25'.format(api_key, channel_id)
video_links = []
url = first_url
while True:
inp = urllib.urlopen(url)
resp = json.load(inp)
for i in resp['items']:
if i['id']['kind'] == "youtube#video":
video_links.append(base_video_url + i['id']['videoId'])
try:
next_page_token = resp['nextPageToken']
url = first_url + '&pageToken={}'.format(next_page_token)
except:
break
return video_links
function getVideos(nextPageToken, vidsDone, params) {
$.getJSON("https://www.googleapis.com/youtube/v3/playlistItems", {
key: params.accessKey,
part: "snippet",
maxResults: 50,
playlistId: params.playlistId,
fields: "items(snippet(publishedAt, resourceId/videoId, title)), nextPageToken",
pageToken: ( nextPageToken || '')
},
function(data) {
// commands to process JSON variable, extract the 50 videos info
if ( vidsDone < params.vidslimit) {
// Recursive: the function is calling itself if
// all videos haven't been loaded yet
getVideos( data.nextPageToken, vidsDone, params);
}
else {
// Closing actions to do once we have listed the videos needed.
}
});
}
// Looping through an array of video id's
function fetchViddetails(i) {
$.getJSON("https://www.googleapis.com/youtube/v3/videos", {
key: document.getElementById("accesskey").value,
part: "snippet,statistics",
id: vidsList[i]
}, function(data) {
// Commands to process JSON variable, extract the video
// information and push it to a global array
if (i < vidsList.length - 1) {
fetchViddetails(i+1) // Recursive: calls itself if the
// list isn't over.
}
});
import urllib.request
import json
key = "YOUR_YOUTUBE_API_v3_BROWSER_KEY"
#List of channels : mention if you are pasting channel id or username - "id" or "forUsername"
ytids = [["bbcnews","forUsername"],["UCjq4pjKj9X4W9i7UnYShpVg","id"]]
newstitles = []
for ytid,ytparam in ytids:
urld = "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&"+ytparam+"="+ytid+"&key="+key
with urllib.request.urlopen(urld) as url:
datad = json.loads(url.read())
uploadsdet = datad['items']
#get upload id from channel id
uploadid = uploadsdet[0]['contentDetails']['relatedPlaylists']['uploads']
#retrieve list
urld = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=50&playlistId="+uploadid+"&key="+key
with urllib.request.urlopen(urld) as url:
datad = json.loads(url.read())
for data in datad['items']:
ntitle = data['snippet']['title']
nlink = data['contentDetails']['videoId']
newstitles.append([nlink,ntitle])
for link,title in newstitles:
print(link, title)
pip3 install -U yt-videos-list # macOS
pip install -U yt-videos-list # Windows
# if that doesn't work, try
python3 -m pip install -U yt-videos-list # macOS
python -m pip install -U yt-videos-list # Windows
python3 # macOS
python # Windows
from yt_videos_list import ListCreator
lc = ListCreator()
help(lc) # display API information - shows available parameters and functions
my_url = 'https://www.youtube.com/user/1veritasium'
lc.create_list_for(url=my_url)