Introduction

Uploading videos to YouTube manually can be a time-consuming process, especially if you have multiple videos to upload regularly. To simplify this task, I’ve created a Python script that automates the upload process using the YouTube Data API v3. This blog post will guide you through the setup and usage of this script, enabling you to streamline your video uploads.

Prerequisites

Before you start, make sure you have the following:

  1. Google Developer Console Project: You need to create a project and enable the YouTube Data API v3.
  2. OAuth 2.0 Credentials: Obtain your OAuth 2.0 Client ID and Client Secret from the Google Developer Console.
  3. Python Environment: Ensure you have Python installed on your machine.

Step-by-Step Guide

Step 1: Set Up Google Developer Console

  1. Navigate to the Developer Console: Go to Google Developer Console.
  2. Create a New Project: Click on “Select Project” at the top, then “New Project”. Give it a name and create it.
  3. Enable YouTube Data API v3: Go to “API & Services Dashboard” and click on “Enable APIs and Services”. Search for “YouTube Data API v3” and enable it.
  4. Set Up OAuth Consent Screen: Go to “OAuth consent screen” on the left, select “External”, and fill in the required details.
  5. Create OAuth 2.0 Credentials: Go to “Credentials”, click on “Create Credentials”, and select “OAuth 2.0 Client IDs”. Download the JSON file with your credentials and save it securely.

Step 2: Install Required Libraries

You need to install the necessary libraries using pip. Open your terminal and run the following command:

pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client

Step 3: Configure and Run the Script

  1. Download the Script: Download the script from my GitHub.
  2. Place the Credentials File: Ensure the client_secrets_file path in the script points to your downloaded credentials JSON file.
  3. Modify the Script: If necessary, modify the script to fit your video upload requirements.

Here is the main script you will use:

import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
import googleapiclient.http

# Scopes required to upload a video
SCOPES = ["https://www.googleapis.com/auth/youtube.upload"]
TOKEN_FILE = 'token.json'

def authenticate_youtube():
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

# Delete the token file to reset authentication
if os.path.exists(TOKEN_FILE):
os.remove(TOKEN_FILE)

# Load client secrets file
client_secrets_file = "path/to/your/credentials.json"

# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, SCOPES)
flow.redirect_uri = 'http://localhost:8080/'

credentials = flow.run_local_server(port=8080)

youtube = googleapiclient.discovery.build(
"youtube", "v3", credentials=credentials)

return youtube

def upload_video(youtube):
request_body = {
"snippet": {
"categoryId": "22", # Category ID for "People & Blogs"
"title": "Test Video",
"description": "This is a test description",
"tags": ["test", "api", "video"]
},
"status": {
"privacyStatus": "private"
}
}

media_file = "path/to/your/video.mp4"

# Upload the video
request = youtube.videos().insert(
part="snippet,status",
body=request_body,
media_body=googleapiclient.http.MediaFileUpload(media_file, chunksize=-1, resumable=True)
)

response = None
while response is None:
status, response = request.next_chunk()
if status:
print(f"Uploaded {int(status.progress() * 100)}%")

print(f"Video uploaded with ID: {response['id']}")

if __name__ == "__main__":
youtube = authenticate_youtube()
upload_video(youtube)

Step 4: Run the Script

  1. Execute the Script: Run the script from your terminal or command line:
upload_video.py
  1. Authenticate: The script will prompt you to authenticate with your Google account. Once authenticated, it will upload the specified video to your YouTube channel.

Troubleshooting

If you encounter issues, ensure that:

  • The credentials file is correctly placed and the path is correct.
  • You have enabled the YouTube Data API v3 in your Google Developer Console.
  • The necessary libraries are installed in your Python environment.

Conclusion

By following these steps, you can automate the process of uploading videos to YouTube, saving you valuable time and effort. Feel free to modify and expand upon the script to fit your specific needs. If you have any questions or run into issues, don’t hesitate to reach out.