5

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

Thomas131

Thomas131
  • 131
  • 1
  • 12

3 Answers3

2

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

Ibrahim Ulukaya
  • 12,767
  • 1
  • 33
  • 36
  • Is this still the case? Is there any API to download my uploaded videos? – mukulg Oct 30 '18 at 15:15
  • 1
    @mukulg as of march 2019, as far as I can tell, but I have presented a work around in my answer below. – deweydb Mar 21 '19 at 17:17
  • @mukulg You can use youtube-dl which is fantastic alternative. I have recently tested this and its working as expected. It allow you to download the highest quality for a video, and provide you have ffmpeg installed it will also automatically add the audio stream for you. We're actually using this to download videos in our YT account. – rv.kvetch Mar 11 '21 at 20:28
2

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)
deweydb
  • 2,238
  • 2
  • 30
  • 37
  • Is there a solution that works with an oauth2 barer-token header instead of a browser cookie? I need to download videos from about 100 authenticated channels, and its easier to generate tokens than having to log in to each one manually. – Sam Rockett May 13 '19 at 13:59
  • @SamRockett, No, but I believe that it should be possible to build a scraper to do it. Take a look at selenium + chrome driver. Best of luck. – deweydb May 14 '19 at 21:08
-1

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

Shiva
  • 20,575
  • 14
  • 82
  • 112