How to Download Video Using Python Youtube API

Download a video using Python

Can we download a video using Python? Yes, its easy to download a video on youtube using youtube-dl library. Basically we don’t really need API to download a video. All we need is just the videoId. Here is a sample code for downloading a video using python. In this article, we will also add a function where we can download all your latest liked video which of course requires an API request.

First you need to install youtube-dl library

pip install youtube-dl

Create a new python file and named it as

Then paste this code:

import sys, youtube_dl
YOUTUBE_DL_DIR = sys.path[0]+'/'

These are the libraries we need. I assigned a global variable named YOUTUBE_DL_DIR. This is the directory where this python file is saved. So all the youtube downloaded videos will be saved on the same directory.

Next is the download function, paste this at the bottom:

def download_video(videoId):
    yvideo = [''+videoId]
    ydl_opts = {'outtmpl': YOUTUBE_DL_DIR+'%(id)s %(title)s'}

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:

This will download the video and save it on our directory. The name format will be: %(id)s %(title)s. This means the filename will start with the videoId and next is the title. You can change this according to your likes. Below are some template samples.

  • %(id)s
  • %(title)s
  • %(channel)s
  • %(uploader)s — uploaders full name
  • %(ext)s — file extension

Finally, paste the main function. Please remember, the videoId here is just a sample. You can change it. You can find the videoid on the Url. Example:

  • The video is GaGACOtd5RY
if __name__ == "__main__":
    videoId = 't4y9kwUS-zA'

Save it and run:


It should download the youtube video.


Leave a Reply
You May Also Like
Read More

How IT Services Help Running Your Businesses?

There are various strategies you can integrate into your company that will assist you with remaining in front of the opposition while proceeding to push your business ahead. In this article, we'll explore how savvy organizations utilize their IT resources
Read More