{"id":6599,"date":"2023-08-25T04:35:19","date_gmt":"2023-08-25T04:35:19","guid":{"rendered":"https:\/\/www.ardorsys.com\/blog\/?p=6599"},"modified":"2023-08-23T06:03:39","modified_gmt":"2023-08-23T06:03:39","slug":"how-to-download-video-using-python-youtube-api","status":"publish","type":"post","link":"https:\/\/www.ardorsys.com\/blog\/how-to-download-video-using-python-youtube-api\/","title":{"rendered":"How to Download Video Using Python Youtube API"},"content":{"rendered":"\t\t<div class=\"pk-inline-posts\">\n\t\t\t\n\t\t\t<div class=\"pk-inline-posts-container pk-inline-posts-template-list\"\n\t\t\t\tdata-columns=\"1\">\n\t\t\t\t\t\t<article id=\"post-7404\" class=\"post-7404 post type-post status-publish format-standard has-post-thumbnail category-artificial-intelligence category-machine-learning category-python tag-artificial-intelligence tag-machine-learning tag-python\">\n\t\t\t<div class=\"pk-post-outer\">\n\n\t\t\t\t\t\t\t\t<div class=\"pk-post-inner\">\n\t\t\t\t\t<div class=\"entry-thumbnail\">\n\t\t\t\t\t\t<div class=\"pk-overlay pk-overlay-ratio pk-ratio-landscape\">\n\t\t\t\t\t\t\t<div class=\"pk-overlay-background\">\n\t\t\t\t\t\t\t\t<a href=\"https:\/\/www.ardorsys.com\/blog\/python-the-secret-weapon-behind-machine-learnings-success-2025\/\" class=\"pk-overlay-link\">\n\t\t\t\t\t\t\t\t\t<img loading=\"lazy\" decoding=\"async\" width=\"320\" height=\"200\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAADIAQMAAACjyqroAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAB5JREFUaN7twQENAAAAwqD3T20PBxQAAAAAAAAA8GIgCAABZGnJPQAAAABJRU5ErkJggg==\" class=\"attachment-pk-thumbnail size-pk-thumbnail pk-lazyload wp-post-image\" alt=\"python Powering Machine Learning Revolution\" data-pk-sizes=\"auto\" data-ls-sizes=\"(max-width: 320px) 100vw, 320px\" data-pk-src=\"https:\/\/www.ardorsys.com\/blog\/wp-content\/uploads\/2025\/02\/python-banner-Machine-Learning-2025-320x200.webp\" data-pk-srcset=\"https:\/\/avfockc25ltq.cdn.shift8web.com\/blog\/wp-content\/uploads\/2025\/02\/python-banner-Machine-Learning-2025-320x200.webp 320w, https:\/\/avfockc25ltq.cdn.shift8web.com\/blog\/wp-content\/uploads\/2025\/02\/python-banner-Machine-Learning-2025-240x150.webp 240w, https:\/\/avfockc25ltq.cdn.shift8web.com\/blog\/wp-content\/uploads\/2025\/02\/python-banner-Machine-Learning-2025-800x500.webp 800w\" \/>\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\n\t\t\t\t<div class=\"pk-post-inner\">\n\t\t\t\t\t<header>\n\t\t\t\t\t\t<ul class=\"post-meta\"><li class=\"meta-category\"><a class=\"category-style\" href=\"https:\/\/www.ardorsys.com\/blog\/category\/artificial-intelligence\/\"><span style=\"background-color:#c150ce\" data-color=\"#c150ce\" data-color-dark=\"#555555\" class=\"char\" data-scheme=\"inverse\">A<\/span><span class=\"label\">Artificial Intelligence<\/span><\/a><\/li><\/ul><h3 class=\"pk-title\"><a href=\"https:\/\/www.ardorsys.com\/blog\/python-the-secret-weapon-behind-machine-learnings-success-2025\/\" rel=\"bookmark\">Python: The Secret Weapon Behind Machine Learning\u2019s Success \ud83d\ude80<\/a><\/h3><ul class=\"post-meta\"><li class=\"meta-author\"><span class=\"by\">by<\/span><span class=\"author\"><a class=\"url fn n\" href=\"https:\/\/www.ardorsys.com\/blog\/author\/admin\/\" title=\"View all posts by admin\">admin<\/a><\/span><\/li><li class=\"meta-date\">February 26, 2025<\/li><\/ul>\t\t\t\t\t<\/header><!-- .entry-header -->\n\t\t\t\t<\/div><!-- .post-inner -->\n\n\t\t\t<\/div><!-- .post-outer -->\n\t\t<\/article>\n\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\n<p>Can we download a video using Python? Yes, its easy to download a video on youtube using youtube-dl library. Basically we don\u2019t really need API to download a video. All we need is just the videoId. Here is a sample code for downloading a video using <a href=\"https:\/\/www.ardorsys.com\/python-development\/\">python<\/a>. 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.<\/p>\n<p>First you need to install youtube-dl library<\/p>\n<pre><code>pip install youtube-dl<\/code><\/pre>\n<p>Create a new python file and named it as\u00a0<strong>download_video.py<\/strong><\/p>\n<p>Then paste this code:<\/p>\n<pre><code>import sys, youtube_dl\r\nYOUTUBE_DL_DIR = sys.path[0]+'\/'<\/code><\/pre>\n<p>These are the libraries we need. I assigned a global variable named\u00a0<strong>YOUTUBE_DL_DIR<\/strong>. This is the directory where this python file is saved. So all the youtube downloaded videos will be saved on the same directory.<br \/>\n\t\t<div class=\"pk-inline-posts\">\n\t\t\t\n\t\t\t<div class=\"pk-inline-posts-container pk-inline-posts-template-list\"\n\t\t\t\tdata-columns=\"1\">\n\t\t\t\t\t\t<article id=\"post-7404\" class=\"post-7404 post type-post status-publish format-standard has-post-thumbnail category-artificial-intelligence category-machine-learning category-python tag-artificial-intelligence tag-machine-learning tag-python\">\n\t\t\t<div class=\"pk-post-outer\">\n\n\t\t\t\t\t\t\t\t<div class=\"pk-post-inner\">\n\t\t\t\t\t<div class=\"entry-thumbnail\">\n\t\t\t\t\t\t<div class=\"pk-overlay pk-overlay-ratio pk-ratio-landscape\">\n\t\t\t\t\t\t\t<div class=\"pk-overlay-background\">\n\t\t\t\t\t\t\t\t<a href=\"https:\/\/www.ardorsys.com\/blog\/python-the-secret-weapon-behind-machine-learnings-success-2025\/\" class=\"pk-overlay-link\">\n\t\t\t\t\t\t\t\t\t<img loading=\"lazy\" decoding=\"async\" width=\"320\" height=\"200\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAADIAQMAAACjyqroAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAB5JREFUaN7twQENAAAAwqD3T20PBxQAAAAAAAAA8GIgCAABZGnJPQAAAABJRU5ErkJggg==\" class=\"attachment-pk-thumbnail size-pk-thumbnail pk-lazyload wp-post-image\" alt=\"python Powering Machine Learning Revolution\" data-pk-sizes=\"auto\" data-ls-sizes=\"(max-width: 320px) 100vw, 320px\" data-pk-src=\"https:\/\/www.ardorsys.com\/blog\/wp-content\/uploads\/2025\/02\/python-banner-Machine-Learning-2025-320x200.webp\" data-pk-srcset=\"https:\/\/avfockc25ltq.cdn.shift8web.com\/blog\/wp-content\/uploads\/2025\/02\/python-banner-Machine-Learning-2025-320x200.webp 320w, https:\/\/avfockc25ltq.cdn.shift8web.com\/blog\/wp-content\/uploads\/2025\/02\/python-banner-Machine-Learning-2025-240x150.webp 240w, https:\/\/avfockc25ltq.cdn.shift8web.com\/blog\/wp-content\/uploads\/2025\/02\/python-banner-Machine-Learning-2025-800x500.webp 800w\" \/>\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\n\t\t\t\t<div class=\"pk-post-inner\">\n\t\t\t\t\t<header>\n\t\t\t\t\t\t<ul class=\"post-meta\"><li class=\"meta-category\"><a class=\"category-style\" href=\"https:\/\/www.ardorsys.com\/blog\/category\/artificial-intelligence\/\"><span style=\"background-color:#c150ce\" data-color=\"#c150ce\" data-color-dark=\"#555555\" class=\"char\" data-scheme=\"inverse\">A<\/span><span class=\"label\">Artificial Intelligence<\/span><\/a><\/li><\/ul><h3 class=\"pk-title\"><a href=\"https:\/\/www.ardorsys.com\/blog\/python-the-secret-weapon-behind-machine-learnings-success-2025\/\" rel=\"bookmark\">Python: The Secret Weapon Behind Machine Learning\u2019s Success \ud83d\ude80<\/a><\/h3><ul class=\"post-meta\"><li class=\"meta-author\"><span class=\"by\">by<\/span><span class=\"author\"><a class=\"url fn n\" href=\"https:\/\/www.ardorsys.com\/blog\/author\/admin\/\" title=\"View all posts by admin\">admin<\/a><\/span><\/li><li class=\"meta-date\">February 26, 2025<\/li><\/ul>\t\t\t\t\t<\/header><!-- .entry-header -->\n\t\t\t\t<\/div><!-- .post-inner -->\n\n\t\t\t<\/div><!-- .post-outer -->\n\t\t<\/article>\n\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\nNext is the download function, paste this at the bottom:<\/p>\n<pre><code>def download_video(videoId):\r\n    yvideo = ['https:\/\/www.youtube.com\/watch?v='+videoId]\r\n    ydl_opts = {'outtmpl': YOUTUBE_DL_DIR+'%(id)s %(title)s'}\r\n\r\n    with youtube_dl.YoutubeDL(ydl_opts) as ydl:\r\n        ydl.download(yvideo)<\/code><\/pre>\n<p>This will download the video and save it on our directory. The name format will be:\u00a0<strong>%(id)s\u00a0%(title)s<\/strong>. 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.<\/p>\n<ul>\n<li>%(id)s<\/li>\n<li>%(title)s<\/li>\n<li>%(channel)s<\/li>\n<li>%(uploader)s \u2014 uploaders full name<\/li>\n<li>%(ext)s \u2014 file extension<\/li>\n<\/ul>\n<p>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:<\/p>\n<ul>\n<li>https:\/\/www.youtube.com\/watch?v=GaGACOtd5RY<\/li>\n<li>The video is GaGACOtd5RY<\/li>\n<\/ul>\n<pre><code>if __name__ == \"__main__\":\r\n    videoId = 't4y9kwUS-zA'\r\n    download_video(videoId)<\/code><\/pre>\n<p>Save it and run:<\/p>\n<pre><code>python download_video.py<\/code><\/pre>\n<p>It should download the youtube video.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"Can we download a video in Python? Yes, its easy to download a video on youtube using youtube-dl library. Basically we don\u2019t 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.\n","protected":false},"author":1,"featured_media":6602,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149,44],"tags":[133],"class_list":{"0":"post-6599","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python","8":"category-web-development","9":"tag-python"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/posts\/6599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/comments?post=6599"}],"version-history":[{"count":1,"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/posts\/6599\/revisions"}],"predecessor-version":[{"id":7262,"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/posts\/6599\/revisions\/7262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/media\/6602"}],"wp:attachment":[{"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/media?parent=6599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/categories?post=6599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ardorsys.com\/blog\/wp-json\/wp\/v2\/tags?post=6599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}