How to code an executable YouTube video downloaded in python

Clément Sauvage
3 min readAug 16, 2020

--

The final project is available directly on GitHub

Introduction

Python is one of the simplest yet most powerful programming language for many reasons. One of it is that the community is extremely broad and very open source oriented. Thanks to that, you can probably find a library to accomplish in a few line most of the tasks you could think of with python, and being able to know how to search for these libraries might be one of the most helpful skills for software engineering.

If we want to code an executable YouTube video downloaded in python, the best thing to do first is not to code but to decompose the process we want to create and how it will be implemented. By doing so, we can think of libraries we should search for to write our final program.

Before we code

In the first place we want to find a library which download a video. The first that comes up, and the most popular one seems to be youtbe_dl. When reading the documentation we can find a ton of different options but here, for to stay as simple as possible, we simply want to download the video in mp4.

Then, when we would have a functioning python CLI script to download a video, we will need to convert it to a .exe file. One of the best option for this seems to be the cx_Freeze library.

Let’s program it !

To begin, we install both libraries in your main python distribution, or preferably in a virtual environment. (idna is an additional dependency needed in the process of compiling our script to the executable file)

pip install youtube-dl cx_Freeze idna

We can now start with the python script to download the videos (youtube-download.py):

from __future__ import unicode_literalsimport youtube_dlimport osimport timeprint("Initialisation...\n")VIDEO_URL = input("\nPlease, past the link of the video : \n")ydl_opts = {}with youtube_dl.YoutubeDL(ydl_opts) as ydl:    ydl.download([VIDEO_URL])print("Download complete")time.sleep(3)

It is straightforward. We first import the required dependencies, then we take as an input the link of the video, to finally download it with an initialized YoutubeDL object from the youtube_dl library.

Let’s then use cx_Freeze to compile our python script as an executable file. We just need to add another script in the same folder (setup.py):

from cx_Freeze import setup, Executablebase = Noneexecutables = [Executable("youtube-downloader.py", base=base)]packages = ["idna"]options = {    'build_exe': {    'packages':packages,    },}setup(    name = "youtube-downloader",    options = options,    version = "0.0.1",    description = 'Download youtube videos',    executables = executables)

We then run the following command:

python setup.py build

And, Voilà ! We can now find in the “build” folder our executable file !

The final project is available directly on GitHub, don’t forget to read the README.md to get the instructions.

Thanks for reading, I hope this article gave you a glimpse of how simply most of the small tasks can be executed with python and its infinite number of open source libraries!

--

--

Clément Sauvage
0 Followers

I am a software engineer who write about subjects which passionate me like technology, digital product and startups. More about me: https://clementsauvage.com