Uploading video on Youtube using Youtube API

Youtube API

Youtube Data API empowers your youtube channel with a bunch of features that you can integrate while uploading videos to youtube. Youtube has developed these powerful APIs to enable video makers, publishers and marketers to take publishing content on youtube to a whole new level. Especially for use cases involving media houses pushing multiple videos out every hour, these Apis are a win-win dish to fry.

You’d say the more, the merrier for content publishing but working with google Apis can get complex beyond a point. There’s a whole bunch of use cases and documentation which can be very confusing to understand and to implement in the first go. All in all, it’s a simple script with a sequence of steps that you need to execute to automate your Youtube uploads.

Procedure

First off, you need to create a project on google console dashboard. You can do that here https://console.developers.google.com/cloud-resource-manager Click on the ‘Create Project’ button and go through the procedure to set up your youtube upload project. Note: If you’re integrating for a youtube partner, you must create the project from the google account that has authorized access for the youtube partner account.

After setting up your project, you need to generate credentials for authentication and access to the API. In the left section, select API’s and Services > Credentials, where you’ll see the ‘Create Credentials’ option on the top of the page. Here, you need to create an OAuth Client ID and mention all the required details. If you’re looking to make requests from a browser or web server, you must specify the origins of these requests for authentication.

Next, you should be able to download your Client Secret JSON that you’ll require for the setup. Once you store this file in your application, you’re almost done with fetching all the details at this point. The client secret JSON consists of something called token_uri which is an API call to google to fetch the access token, which is the final piece of the puzzle you need to play with the Youtube API.

Access Token and refresh Tokens

You need your youtube account’s access token for validation using OAuth 2.0 verification protocols which is required for accessing google apis. Since the access token comes with an expiry, you need to store the OAuth-JSON on your end to re-generate access token using refresh token. More on this can be found at Using OAuth 2.0 to Access Google APIs | Google Identity Platform

Scopes

Every google API requires the requests to specify the scope of the request. If you’re simply integrating youtube uploads using this script, you only need access to the the following scope:

https://www.googleapis.com/auth/youtube

However, for youtube partner accounts adding monetization policies to the request require additional scopes added to the request.

https://www.googleapis.com/auth/youtubepartner
https://www.googleapis.com/auth/youtubepartner-content-owner-readonly

You can specify n number of scopes as a tuple in python:

SCOPES = (https://www.googleapis.com/auth/youtube, https://www.googleapis.com/auth/youtubepartner)

Furthermore, if you’re a youtube partner account holder or you’re working for a client who owns one of these, you can actually build a system to automate youtube uploads along with monetization policies pre-set to the video. Before getting to monetization and ad policies, you need to have a partner account with youtube to get access to the youtube partner API and content ID API. Now if it were so simple, every other person owning a gmail account would exploit these powerful features for their minor use case, abuse content publishing alike and make Twitter out of itself. However, there are a whole bunch of criteria you must fulfill before you can get yourself a youtube partner account. 
https://support.google.com/youtube/answer/72851?hl=en

Playing with the API

If you’re familiar with python, this part is going to be pretty straight-foward. You can access the entire script of Youtube API integration here https://github.com/meetshah15/youtube-partner First off, you’ll need access to the mp4 version of your video in the script. You can either download it to the same folder or specify the path to the video in the script. Once you have your video metadata, video, thumbnail (optional), and client-secret ready, you can start your implementation using the python script.

Parser = argparse.ArgumentParser(parents=[tools.argparser]) 
(youtube, youtube_partner) = get_authenticated_services(parser)

The get_authenticated_services function can be used in multiple ways. Here, you can see that it returns two objects, ‘youtube’ and ‘youtube_partner’ that are essentially required for uploading video to a partner account. If you’re simply uploading to your own account, you can play around with the function to only create the ‘youtube’ object. Youtube API enables you to add all the properties to your video within the API that you’d generally set from the youtube website. You can specify these properties using the ‘argparser’ library in python.

options = parse_options(video, video_title, video_description, keywords, channel_id)

Now youtube says that your video’s title, description and thumbnail are really the key to your video’s virality. Keywords are just something additional to help the viewer recognise the content. In case you own more than one channel of a particular account or a partner account, you’d have to specify the channel to which the video must be uploaded using channel ID. You can find your channel ID on youtube > Settings > Advanced Settings (https://www.youtube.com/account_advanced).

Once you’ve successfully created your Youtube object and set the metadata on the video, you can move on to upload your video! However, it’s possible that the youtube thumbnail does not show after you have uploaded the video. To tackle this, you can set your own custom thumbnail using the youtube thumbnail API. To use this, you must have an image asset of the thumbnail you wish to upload. If not, you can generate your own thumbnail at a specified duration of the video using the moviepy library and FFmpeg.

video_clip = VideoFileClip(video)
purge_list.append(video_clip)
image_path = pwd + '/' + video_id + '.jpg'
video_clip.save_frame(image_path, 4)

The piece of code above will take a screenshot of your video at the 4th second. Make sure you kill the ffmpeg process after you’re done uploading your image if you’re running the script from a server, since the process is known to occupy a huge chunk of CPU memory.

kill_ffmpeg_process(purge_list)

With this setup under your sleeve, you can start playing around with a whole lot of features that Youtube API has to offer. You can create new playlists, categorise your content, schedule your uploads based on the best time of the day and have superficial control over your youtube channel.Continuation article on Youtube Partner API is on it’s way. Follow us on linkedin to get updates on the new articles Linked in

 1,395 total views,  1 views today

How did you like the article?
+1
3
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0