This is a short guide aimed at beginners to explain what youtube-dl and ytdl-core are and how to use them effectively.
youtube-dl and ytdl-core are not user-friendly websites
or GUI applications for downloading YouTube videos!
The projects are meant for embedding in computer programs.
If you're looking for a YouTube downloading website, check out
safedl; it uses ytdl-core under the hood.
If you're looking for a CLI of each library, here's
youtube-dl's binary instructions,
and here's a binary wrapper for ytdl-core.
youtube-dl and ytdl-core are libraries, or a chunk of code
that can be used by another computer program.
ytdl-core is written in JS to be used in a Node.js program.
It's distributed on npm and
developed on GitHub.
youtube-dl is written in Python, to be used from another Python program.
It's distributed on pip
and developed on GitHub.
If you're new to programming, you should probably learn the basics of
Python
or JS
before continuing.
youtube-dl is older and larger than ytdl-core. It supports
many sites and makes a binary (CLI) of it under the same project.
ytdl-core is more exposed than youtube-dl; in other words,
you have much more control over what ytdl-core actually does.
You should use the one that best fits your project's needs, i.e. if
one has a feature you need or is in the language you'll code in.
Keep in mind that most languages can utilize binary files, so
if you code in another language you can still use the bins.
Quick note: Python 2 and Python 3 behave differently; I'll only teach usage in Python 3.
We've finally gotten past the boring part. Glad you're still here.
Start by installing the library in pip. You'll need to do this from
the command line.
How exactly this happens varies a bit by machine, but it's probably
pip install youtube_dl
. In some cases it might be pip3 install youtube_dl
.
Make sure you keep it updated!
Basic usage of youtube-dl in Python is as simple as it gets:
import youtube_dl
youtube_dl.YoutubeDL({}).download('videourl')
This code will download the video at 'videourl'
directly to disk.
Let's talk a bit about exactly what that code does.
The first line, import youtube_dl
, looks for the module you just
installed with pip
and makes it usable in the rest of the file.
You only need this line once in a file.
The second line's a bit complicated, so let's break it down.
youtube_dl.YoutubeDL
references the class called YoutubeDL
from
the import youtube_dl
statement you wrote before it.
({})
provides blank (default) options to start the class. This is required.
You can provide any other options by putting them inside the {}
, for example
{'quiet':'true'}
.
Finally, .download('videourl')
uses the YoutubeDL
class that's
before it to download 'videourl'
using the options in {}
.
If you're going to do multiple operations from the same instance of
the YoutubeDL
class, a with..as
statement can make it easier:
import youtube_dl
with youtube_dl.YoutubeDL({}) as ydl:
ydl.download('videourl')
ydl.download('videourl')
# etc...
TO learn more, check out youtube-dl's documentation to learn about all the things you can do from the Python module.
Start by installing ytdl-core into your project's JSON file:
npm i -S ytdl-core@latest
. Make sure you keep it updated!
Using ytdl-core in Node.js is also a cinch:
const ytdl = require('ytdl-core');
ytdl('videolink');
Let's break this one down as well.
The first line, const ytdl = require('ytdl-core');
, has the same
function as the import
statement from the Python program:
it takes the module from package.json and makes it available in
the file. Again, you only need this line once per file.
Then ytdl('videolink')
returns a Stream, a special type of data
in JS that streams from one source to another.
You can use pipes, listeners, or functions that take streams to
use this data in the next part of your program.
Another function that ytdl-core has is ytdl.getInfo('videolink')
.
It's async, so you'll have to wait for it to finish, for example with await
.
When it returns, you get an Object with all of the video metadata, formats, etc.
It's quite a helpful function, and makes it my downloading library of choice.
Check out ytdl-core's GitHub repo
for more detail and other functions that you can use with ytdl-core.
Please comment with ideas to improve this Gist.