Python 3 Script to Upload Videos to Youtube From Command Line Using Youtube Data API V3 Full Project For Beginners

 

Introduction

Command-line script to upload videos to Youtube using theYoutube APIv3. It should work on any platform (GNU/Linux, BSD, OS X, Windows, …) that runs Python.

Dependencies

Check if your operating system provides those packages (check also those deb/rpm/mac files), otherwise install them with pip:

Install

Or run directly from sources:

Setup

You’ll see that there is no email/password options. Instead, the Youtube API uses OAuth 2.0 to authenticate the upload. The first time you try to upload a video, you will be asked to follow a URL in your browser to get an authentication token. If you have multiple channels for the logged in user, you will also be asked to pick which one you want to upload the videos to. You can use multiple credentials, just use the option --credentials-file. Also, check the token expiration policies.

The package used to include a default client_secrets.json file. It does not work anymore, Google has revoked it. So you now must create and use your own OAuth 2.0 file, it’s a free service. Steps:

  • Go to the Google console.
  • Create project.
  • Side menu: APIs & auth -> APIs
  • Top menu: Enabled API(s): Enable all Youtube APIs.
  • Side menu: APIs & auth -> Credentials.
  • Create a Client ID: Add credentials -> OAuth 2.0 Client ID -> Other -> Name: youtube-upload -> Create -> OK
  • Download JSON: Under the section “OAuth 2.0 client IDs”. Save the file to your local system.
  • Use this JSON as your credentials file: --client-secrets=CLIENT_SECRETS or copy it to ~/client_secrets.json.

Note: client_secrets.json is a file you can download from the developer console, the credentials file is something auto generated after the first time the script is run and the google account sign in is followed, the file is stored at ~/.youtube-upload-credentials.json.

Examples

  • Upload a video (a valid ~/.client_secrets.json should exist, check the Setup section):
  • Upload a video with extra metadata, with your own client secrets and credentials file, and to a playlist (if not found, it will be created):

Other extra medata available :

  • Upload a video using a browser GUI to authenticate:
  • Split a video with ffmpeg

If your video is too big or too long for Youtube limits, split it before uploading:

  • Use a HTTP proxy

Set environment variables http_proxy and https_proxy:

Get available categories

  • Search “youtube categories” -> youtube.videoCategories.list
  • This bring you to youtube.videoCategories.list service
  • part: id,snippet
  • regionCode: es (2 letter code of your country)
  • Authorize and execute

And see the JSON response below. Note that categories with the attribute assignable equal to false cannot be used.

Using shoogle:

 

 

DOWNLOAD FULL SOURCE CODE

Leave a Reply