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 to Hire Right WordPress Developer?

if you are looking web Development Company in India, you’re in luck. It can still be a challenge for you to find the best WordPress developer in a software development company but Ardorsys Technologies brings you with the multiple choices of hiring the right and professional web developer.
Read More

The Beginner’s Checklist: How to Secure Your Website

Do you have your website secured? No? Then, you are inviting hackers or unwanted threats to steal your data. Here is the complete checklist for site security that will help you to learn about security threats and to get your website secured from them...