yt-api中有下载自上传视频的方法吗?我想为视频制作一些类似pixabay的东西。
托马斯131
这方面没有API。你只能通过UI来完成。
虽然我还没有找到一个API,但他们的下载系统似乎可以很容易地与脚本一起使用。如果你进入视频工作室,然后转到你的视频,然后你将鼠标悬停在。。。并将鼠标悬停在下载上,你会看到这样的链接:
https://www.youtube.com/download_my_video?v=[video_id]&t=[key]
如果你查看一堆不同的视频,你会发现密钥总是一样的。因此,您所需要做的就是使用API查找您的所有视频ID,然后将该列表和上述密钥提供给它,您将能够以编程方式下载所有视频。我应该补充一下,你还需要从当前浏览会话中获取一些cookie,并将其与请求的标题一起发送。这里有很多贫民区,但对我有用:
import requests
import csv
headers = {
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
"cookie": "[your cookie here]"
}
key = "[your key here]"
with open("hashes.csv", "r") as hashes:
hashes_reader = csv.reader(hashes)
for row in hashes_reader:
print(row)
video_id = row[1]
url = "https://www.youtube.com/download_my_video?v=%s&t=%s" % (video_id, key)
with requests.get(url, headers=headers, stream=True) as r:
r.raise_for_status()
with open(row[0]+".mp4", "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
由于您使用youtube javascript api对此进行了标记,我猜您希望使用javascript来构建下载应用程序。
这是Javascript客户端:https://code.google.com/p/google-api-javascript-client/
这里有Javascript、HTML和CSS代码,可以让你访问"我上传的视频",也就是你上传的视频。
Javascript代码:
// Some variables to remember state.
var playlistId, nextPageToken, prevPageToken;
// Once the api loads call a function to get the uploads playlist id.
function handleAPILoaded() {
requestUserUploadsPlaylistId();
}
//Retrieve the uploads playlist id.
function requestUserUploadsPlaylistId() {
// https://developers.google.com/youtube/v3/docs/channels/list
var request = gapi.client.youtube.channels.list({
mine: true,
part: 'contentDetails'
});
request.execute(function(response) {
playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;
requestVideoPlaylist(playlistId);
});
}
// Retrieve a playist of videos.
function requestVideoPlaylist(playlistId, pageToken) {
$('#video-container').html('');
var requestOptions = {
playlistId: playlistId,
part: 'snippet',
maxResults: 10
};
if (pageToken) {
requestOptions.pageToken = pageToken;
}
var request = gapi.client.youtube.playlistItems.list(requestOptions);
request.execute(function(response) {
// Only show the page buttons if there's a next or previous page.
nextPageToken = response.result.nextPageToken;
var nextVis = nextPageToken ? 'visible' : 'hidden';
$('#next-button').css('visibility', nextVis);
prevPageToken = response.result.prevPageToken
var prevVis = prevPageToken ? 'visible' : 'hidden';
$('#prev-button').css('visibility', prevVis);
var playlistItems = response.result.items;
if (playlistItems) {
$.each(playlistItems, function(index, item) {
displayResult(item.snippet);
});
} else {
$('#video-container').html('Sorry you have no uploaded videos');
}
});
}
// Create a thumbnail for a video snippet.
function displayResult(videoSnippet) {
var title = videoSnippet.title;
var videoId = videoSnippet.resourceId.videoId;
$('#video-container').append('<p>' + title + ' - ' + videoId + '</p>');
}
// Retrieve the next page of videos.
function nextPage() {
requestVideoPlaylist(playlistId, nextPageToken);
}
// Retrieve the previous page of videos.
function previousPage() {
requestVideoPlaylist(playlistId, prevPageToken);
}
页面的HTML标记:
<!doctype html>
<html>
<head>
<title>My Uploads</title>
<link rel="stylesheet" type="text/css" href="my_uploads.css">
</head>
<body>
<div id="login-container" class="pre-auth">
This application requires access to your YouTube account.
Please <a href="#" id="login-link">authorize</a> to continue.
</div>
<div id="video-container"></div>
<div class="button-container">
<button id="prev-button" class="paging-button" onclick="previousPage();">Previous Page</button>
<button id="next-button" class="paging-button" onclick="nextPage();">Next Page</button>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="auth.js"></script>
<script type="text/javascript" src="my_uploads.js"></script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
</body>
</html>
CSS:
.paging-button {
visibility: hidden;
}
.button-container {
clear: both;
}
来源:https://developers.google.com/youtube/v3/code_samples/javascript#my_uploaded_videos