Kawaii Ninja

How I Created a Python Script to Download YouTube Videos for Offline Viewing


Have you ever wanted to watch your favorite YouTube videos offline? I certainly did! That’s why I decided to create a Python script that automates the process of downloading videos from my subscribed channels. In this blog post, I’ll share my journey of creating this script and how it has made my offline viewing experience much more convenient.

Getting Started:

To begin, I needed a way to access my subscribed channels information. I used NewPipe, an open-source Android app that allows exporting subscription data as a JSON file. This file contains all the necessary details about the channels I’m subscribed to.

Writing the Code:

I started by importing the required modules in Python, such as subprocess, datetime, os, and json. These modules provided the necessary functionalities for executing shell commands, working with dates, interacting with the operating system, and handling JSON data.

Next, I defined the output directory where I wanted to save the downloaded videos. I chose a folder called “Output”, but you can customize it to your liking. Additionally, I specified the path to the NewPipe subscription export JSON file, which held the channel information.

The core functionality of the script revolved around a function called download_videos(). This function took the channel data as input and extracted the channel URL and name. It then created a specific output directory for each channel if it didn’t already exist.

To download the videos, I utilized the yt-dlp command-line tool. With this tool, I constructed a command string that included various options like selecting video formats, writing subtitles, removing sponsor blocks, and more. The command was dynamically generated based on the channel’s URL and the output directory.

Once the download command was ready, I executed it using subprocess.Popen(). This allowed me to initiate multiple downloads. I kept track of the ongoing download processes in a list called download_processes.

To ensure that all downloads completed before exiting the script, I used a loop to wait for each process in the download_processes list to finish. Once all the videos were downloaded, the script printed a message indicating the completion of the process.


Creating this Python script to download YouTube videos from my subscribed channels has been a game-changer for my offline viewing experience. It saves me time and effort by automating the downloading process. Now, I can enjoy my favorite videos even when I don’t have an internet connection.

If you’re interested in creating a similar script, make sure to check out the yt-dlp tool and customize the code according to your preferences. Remember to respect the content creators rights and use the script responsibly. Happy offline viewing!

#Python #YouTube