Upload Videos To YouTube using PythonV3 Library

Hi readers, Today’s topic is a bit different than always. Today we are going to see how can we upload local videos to YouTube using a Python library.

I’ll try to make it short and simple. So, that it would be easy for you to implement.

Grab YouTube Api Keys

Youtube data api v3 screenshot

Before starting it out, you have to grab the API Keys so that you can perform actions using API. In Today’s date, YouTube provides you with 10,000 tokens each day for API requests. The reason is to prevent misuse of the API. You can read about Api Quota Limit here on the Youtube Data API – Quota Calculator.

For Videos Upload it will cost s 1600 tokens each.

youtube tokens limit

You can also read about Python Quickstart.

You have to go to the API Console and create a new project if you don’t have one.

Generate Keys

  1. Log into https://console.cloud.google.com
  2. Create a new Project
  3. Search for “YouTube Data API V3”
  4. Click Credentials
  5. Click Create Credentials
  6. Select that you will call API from “Web Server”
  7. Select “Public Data” if you want to not get private data and “User Data” if you do
  8. Download or copy your API key from the Credentials tab

Now you’ll see the credentials tab on the sidebar, visit the page. On Top, you’ll see a button Create Credentials. Just click and you’ll see something as I mentioned in the image below.

create credentials  for api

Click on Generate API Key. Then generate OAuth Client ID please make sure when you are selecting the Application Type select Desktop. The reason Web Applications have not worked for me.

select application type

Now, Hopefully, you’ll have a client_secret___**.json file that you can download from the download button.

download client secret json file

Install Library

Of course, we are gonna use a library for this. Because I have used ChatGpt for some use cases but eventually I found this awesome library that worked very well. Without any extra clutter, I got the results quickly.

We gonna use Simple YouTube API

#Installation
pip install simple-youtube-api

You can visit the library, it won’t take much time for you to understand how to use the Library.

file structure

Ignore the .venv folder. Because I have created the virtual environment because it wasn’t working on my machine ubuntu. But the necessary files are your video/videos program.py which is the main file. and the client_secret.json file that you have download from the google API console.

Program.py

I’m using the same code that has been mentioned in the library itself. Nothing new but you can comment few actions that you don’t want. For example: If you do have thumbnail you can uncomment the thumbnail setter method and just pass the correct path.

The below code has been implemented by myself so don’t worry it will be working perfectly.

from simple_youtube_api.Channel import Channel
from simple_youtube_api.LocalVideo import LocalVideo

# loggin into the channel
channel = Channel()
channel.login("client_secret.json", "credentials.storage")

# setting up the video that is going to be uploaded
video = LocalVideo(file_path="video.webm")

# setting snippet
video.set_title("My Filemanager Built Using Laravel Livewire")
video.set_description("I have built a filemanager using laravel livewire")
video.set_tags(["this", "tag"])
video.set_category("gaming")
video.set_default_language("en-US")

# setting status
video.set_embeddable(True)
# if we don't want to make video license free then we can keep it to standard. 
# video.set_license("creativeCommon") Creative Common means anybody can use the video on their channel.
video.set_privacy_status("private")
video.set_public_stats_viewable(True)

# setting thumbnail
# video.set_thumbnail_path("test_thumb.png") // you can set the thumbnail
# video.set_playlist("PLDjcYN-DQyqTeSzCg-54m4stTVyQaJrGi")

# uploading video and printing the results
video = channel.upload_video(video)
print(video.id)
print(video)

# liking video
video.like() 
# this will cost you an extra token so if you want you can do like as well.

# let me resolve this isssue 

Authentication

When you run this file, It will open a new window and will ask for your permission to use your channel for uploading videos, etc. After authentication, you’ll be able to upload videos. It will create a credentials.storage file in the root location of your project.

Result:

result after upload

Recommended: Download Youtube Video Using YT-dlp Library

Source Code: https://github.com/robinkashyap-01/youtube-video-upload-py

Conclusion

I think this way you’ll be ready to go. This implementation is pretty much simple. If you wanna extend this script don’t hesitate to ask Chat Gpt. Thanks for reading. I’m done for the day.

Ask any question in the comment box I’ll try to respond as soon as I’ll get the notification.

Leave a Comment