在youtube-api下载自己的视频

时间:2013-11-26 16:37:19

标签: api youtube download youtube-api youtube-javascript-api

yt-api有没有办法下载自己上传的视频?我想制作像pixabay这样的视频。

Thomas131

3 个答案:

答案 0 :(得分:1)

没有API。您只能通过UI完成。

答案 1 :(得分:0)

尽管我无法为此找到一个API,但是看来他们的下载系统可以很容易地与脚本一起使用。如果您先进入Video Studio,然后再进入您的视频,然后将鼠标悬停在...上,然后将鼠标悬停在下载上,您将看到如下所示的链接:

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)

答案 2 :(得分:-1)

由于您使用youtube-javascript-api对此进行了标记,因此我猜您要使用Javascript来构建下载应用程序。

以下是Javascript客户端:https://code.google.com/p/google-api-javascript-client/

here's the Javascript, HTML and CSS code可让您访问“我上传的视频”,即您上传的视频。

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