LGstudio
LGstudio

Reputation: 117

YouTube API v3 - get video title by url in Python

I'd like to know how can I simply get the title or other information about a video using Youtube API, in case the only thing I know is the url of the video (so basically the video ID).

What other info can I get about a video? eg: Length, Category, Uploader name, Country of origin, ... ???

Can somebody provide me a usable code snippet and the library to use for this data collecting?

Thanks for the help in advance.

Upvotes: 2

Views: 4581

Answers (3)

FriskySaga
FriskySaga

Reputation: 439

Try using this API: https://pypi.org/project/python-youtube/

To grab the title, you can do something like this:

from pyyoutube import Api
playlistVideoItems = api.get_playlist_items(playlist_id='PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw').items
print(playlistVideoItems[0].snippet.title)

Note that the above is somewhat untested code. I copied the relevant bits and pieces from what I currently have, but I did not test this exact set lines of code. And of course, I'm not actually using that playlist ID for my purposes.

In regards to what other types of information can be gathered, I would recommend reading the documentation or running in a debugger. As of this writing, I am trying to figure out how to obtain the video uploader name, but I don't really need this data although it would be nice to have. I will update this answer if I figure it out.

Upvotes: 0

josepainumkal
josepainumkal

Reputation: 1733

Try below code:

payload = {'id': search_result["id"]["videoId"], 'part': 'contentDetails,statistics,snippet', 'key': DEVELOPER_KEY}
l = requests.Session().get('https://www.googleapis.com/youtube/v3/videos', params=payload)    
resp_dict = json.loads(l.content)
print "Title: ",resp_dict['items'][0]['snippet']['title']

Upvotes: 1

HEADLESS_0NE
HEADLESS_0NE

Reputation: 3536

There are plenty of examples and documentation about what you can get using the YouTube API's Python bindings.

Here are Python code samples as provided by YouTube: https://developers.google.com/youtube/v3/code_samples/python#create_and_manage_youtube_video_caption_tracks

And the code samples can also be downloaded from their GitHub repository: https://github.com/youtube/api-samples/tree/master/python

Upvotes: 2

Related Questions