Python Tool to Download YouTube Video in 4K HD

Created: Tue 18 Apr 2023 Updated: 6 months, 2 weeks ago

article-featured-image

This article is about my YouTube Downloader tool which is written in Python. It supports video Downloading up to 4K. After searching on the internet and GitHub also, I found out that most of the available tools do not even support 1080p video. If some tools do support it, they provide audio and video files separately, which we have to merge together. Not so cool right?

With this Python tool, you can Download video up to 4K definition without any audio or video related issue. Just paste the YouTube video link and you are all good. This is a command line-based tool.

Get YouTube 4k video downloader tool

The script is available on my GitHub page. Go to the link below to get the youtube-4k-dl-gdrive-sync tool.

You can either Download ZIP by clicking on 'Code' button OR you can use Git to clone the code repository into your system. To use Git, run the below command:

$
git clone https://github.com/pbxforce/youtube-4k-dl-gdrive-sync.git

$
cd ~/youtube-4k-dl-gdrive-sync

First command will fetch the code by cloning it from my GitHub repository using Git and second command will change the working directory to this cloned directory.

Setup virtual environment and required packages

All these commands must be run inside cloned directory. Inside this directory, you will find requirements.txt file. It contains list of all the required pip packages that we need for this Python tool to work. But first, i advise you to create new virtual environment specific to this project. To create a new virtual environment and activate it, Run the below command:

$
python3 -m venv venv

$
source venv/bin/activate

Here, we created new virtual environment by the name of venv and then activated it. Now we need to install all the required pip packages. For that, we will use install.sh which is a basic Bash script file. Run the below command:

(venv) $
bash install.sh

This command will install all the required pip packages in activated virtual environment. Now you are all set to test the Python script.

Executing Python script

After creating new virtual environement and installing all the required packages, it's time to initialize the script. Run the below command to execute the python script:

(venv) $
python3 pbxforce_yt.py --no-drive --keep-local

Now you need to understand the scope of this script. In the above command, i have passed two optional arguments. These arguments are used to let the script know that we don't want to interact with Google Drive and also we need to save the files in the system locally.

  • First, you will be required to paste your YouTube video link.
  • Then select preferred video quality from the list.
  • Your video will be saved in the current working directory under a new directory named protocolten

Now that is if you don't want to interact with google drive and just want to download the files in system locally. If you want to interact with Google Drive then there are some extra steps that you need to take first.

Sync with Google Drive

To download a YouTube video, upload it to Google Drive and get the direct download link, you will need to acquire Google OAuth credentials. Go to this link: Google Console to generate OAuth credentials. You also need to enable Google Drive API from Google Console Library.

After setting up Google OAuth credentials, you will get Client ID and Client secret. Now run the script without optional arguments as below:

$
python3 pbxforce_yt.py

Now it will configure Google Drive API that will be used to interact with google drive. It's a one-time setup. Follow the process:

  • First, you will be asked to paste your Client ID that we generated in the last step.
  • Then you will be required to paste your Client secret.
  • Your browser window will open. Log in with your Google account.
  • After login, you will see the message "Your authentication flow is completed" means everything worked well.

This process will generate a credentials.json file in your project directory. This file will be used to authenticate any request made to google drive. After authentication, the rest of the process is the same. Paste the video URL, type your preferred quality and it'll be uploaded to your google drive and a direct download link will be provided to you.

Sync with Google Drive and keep local files

Now the above google drive process will do not save any kind of file in your local system. Any video you downloaded from YouTube, it'll be directly uploaded to your google drive. If you want to keep the video file in your system as well as upload it to Google Drive, you can pass optional argument --keep-local while running the script. Run the below command for that:

$
python3 pbxforce_yt.py --keep-local

Using the script with this argument, video downloaded from YouTube will be uploaded to google drive and also saved in the system locally.

Python YouTube Downloader
protocolten-admin

Author: Harpreet Singh
Server Administrator

POST CATEGORY
  1. Programming
  2. Cloud
  3. Scripts
Suggested Posts:
LINUX post image
Install Latest Version of PHP on CentOS 8 and 7


CentOS is great. I have to admit that all those SELinux enforcement and other …

LINUX post image
How to deploy Django application with Nginx, MySQL, and Gunicorn on Ubuntu

Django is an open-source web framework based on Python. It can be very efficient …

LINUX post image
Configure UFW Firewall in Linux

Firewalls in Linux play an important role in controlling the incoming and outgoing network …

WINDOWS post image
Run any program as service in windows

Running a program as a service in Windows can be incredibly useful, allowing you to …

LINUX post image
Get Free valid SSL Certificate from Trusted CA

SSL plays a key role when it comes to your website security and encrypted …

Sign up or Login to post comment.

Sign up Login

Comments (0)