Recently I uploaded this video on YouTube whose title automatically updates with its current realtime views and likes, such that the title says (almost exactly) the number of views and likes the video has.
You can watch this video here.
This idea was inspired from Tom Scott, who made the same kind of video before me. You can watch his video here.
Since the time I put this video on my channel, few of my viewers have been asking me to explain in detail how I made this happen. I couldn't make a video on that, so I'm writing this blog post!
First off, the backbone to this project is the YouTube Data API v3.
Make sure you give it a little bit of read before continuing this post.
Basically, by using YouTube Data API, we can do a lot of tasks by writing code. Tasks like searching for a video, getting a video's public statistics (views, likes, comments) and metadata (title, description, tags), etc., can be easily automated.
In addition to this, you can do some admin tasks too, for example, upload videos to your channel, or edit your existing videos (like change its metadata, thumbnails, etc), provided that you have a valid authorisation that the channel belongs to you (which is obvious).
My goal is to automate the process of updating the title of a video (on my channel) to its realtime views and likes. To do this, there are two simple steps to follow intuitively,
For every time interval, query the particular video's realtime statistics (views and likes, in this case).
Update the title of the particular video such that it contains these views and likes which we extracted from the previous step.
So how to do this?
Querying a video's realtime information.
First, we will write code to query a particular video's current views and likes. In-order to extract this information using the API, we need to first register at Google API Console.
After creating an account, create a new project and name it whatever you want. The next step is to activate the YouTube Data API, you can do this simply by going to Library, and searching for "YouTube Data API" and then click "Enable"
Now come to the Credentials page, and create a new API Key.
We can now use this API key to send requests to the API server. Here is how you send a request asking for a video's statistics,
GET https://www.googleapis.com/youtube/v3/videos?part=snippet%2Cstatistics&id=Ks-_Mh1QhMc&key=[YOUR_API_KEY] HTTP/1.1
Accept: application/json
In the above HTTP request, you can put in your API Key in place of [YOUR_API_KEY].
Sending this HTTP request will give us a JSON response with two things, the first one is the video's metadata (title, description, tags, etc) and then the statistics of the video.
A sample JSON response looks like this,
As you can see from the JSON response, we can now easily retrieve things we need. In our case we need these things : video title, description, views, likes, and comments.
I've written a python script to do all this, and called it get_video_info.py
Updating the title of the video.
Now, this is a little tricky. We need to use Google's OAuth 2.0 to correctly give our app the authorisation it needs in-order to edit a video on YouTube (like updating its title, which we are trying to do in this case). A simple API key like in the previous step will not work, OAuth is necessary.
This is easy too, go to your credentials page on API console (make sure you're logged in with the google account that is associated with your YouTube channel), and create a OAuth Client ID. Before creating this, you need to configure your OAuth Consent screen.
Once you're done creating a OAuth client ID, download the client_secret.json file from your dashboard. This file contains the client ID for your app, which you will use to get authorisation from yourself in the future.
You now need to give authorisation of your YouTube channel to the app that you created. This is just like giving permission of your google account to any other google app (take TubeBuddy for example).
Once the authorisation is received by your app, your app can now send requests to the YouTube API's server making changes it wants on behalf of your channel.
But there is a catch....
When using Google's API services, you are given an allowed limit of requests you can make the API server in a day (it's called quota). As of now, the quota of every new user is 10,000 a day. If you want to extend this quota, it is a very large process, and it takes a lot of time and approvals from Google.
So, 10,000. Would this be enough for our app to work effeciently?
Let's calculate, you can use the YouTube Data API quota calculator here
The job that we require (updating a video's title) has a cost of 53 for every single request.
So let's say if we want our code to update our video's title for every one minute (in worst case), then the total requests we need to make in a day is 24*60 which is 1440 requests. The total cost of these 1440 requests would be 1440*53 = 76320 which is way beyond our allowed quota 10,000.
So, what to do? I didn't find any option other than creating multiple apps (remember, each app gets a quota of 10,000 so 8 apps would get a total quota of 80,000) and make them run consecutively (that is, app1, app2, app3, app4,.....app8 and then keep repeating).
This might seem to be against Google's policies (it indeed is, I got a mail from Google recently stating that they will stop my apps from running if I don't bind these multiple apps into one)
I wrote a python script to do all this, you can read it here.
So now comes the final step. Giving all my apps permissions to my YouTube channel.
After giving the permissions once, all the apps can now start working together. And here is what they follow,
For every one minute, query the video's stats, and metadata
Build a title with the realtime views and likes
If the updated title matches with the old title, it means the title is already in the updated state, so no need to update again.
If not, then update the title of the video with the newly built title.
So, in worst case, our apps must update the title every one minute.
I then made my code run on cloud, and it works perfectly. I just need to give my code a video ID, and the rest, it will do itself!
All going well, but just after 2 hours of deploying this, I got an email from Google.
It says that my apps will be suspended for violating the policies (the time I'm writing this post, the apps are yet not suspended). So yeah, the apps will probably stop anytime soon, and the title will no longer get updated.
I really wish the quota for an individual app be increased.
Wow, this is a really interesting thing, when I first saw such a thing on a YouTube video, I was stunned! I tried to do the same thing on my old video, but for some reason it didn't work out ...
Maybe you can clearly show how to do it in the video? There are several ways to record a screen with audio, maybe you could tell us about it and not only in more detail?