Pregunta

Is there a way in the yt-api to download self uploaded videos? I want to make something like pixabay for videos.

Thomas131

¿Fue útil?

Solución

There is no API for that. You can do it through UI only.

Otros consejos

Although I have not been able to find an API for this, it seems that their download system can be utilized with a script fairly easily. If you go into the Video Studio, and then to your Videos, and you hover over the ... and hover over Download you will see a link like this:

https://www.youtube.com/download_my_video?v=[video_id]&t=[key]

If you check a bunch of different videos you will see that the key is always the same. So all you need to do is use the API to find all your video IDs, then feed it that list and the aforementioned key and you will be able to download all your videos programatically. I should add you need to highjack some cookies from your current browsing session as well and send that along with the headers of your requests. It's plenty ghetto but it's working for me:

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)

Since you tagged this with youtube-javascript-api, I am guess you want to use Javascript to build the download application.

Here's the Javascript client: https://code.google.com/p/google-api-javascript-client/

And here's the Javascript, HTML and CSS code that will let you access "My Uploaded Videos", which is the videos you uploaded.

Javascript Code:

// 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 Markup for the Page:

<!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;
}

Source: https://developers.google.com/youtube/v3/code_samples/javascript#my_uploaded_videos

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top