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

4 Most Popular PHP Frameworks for Web Development in 2020

PHP is one of the most used server side scripting languages for web development and backend development. According to recent survey, 60-80% web developers prefer PHP frameworks for web development services due to several benefits. PHP is faster, robust, scalable language which guarantees safer web development and reduce workload too.
Read More