How to use youtube-dl from a python program?
It's not difficult and actually documented:
import youtube_dl
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'})
with ydl:
result = ydl.extract_info(
'http://www.youtube.com/watch?v=BaW_jenozKc',
download=False # We just want to extract the info
)
if 'entries' in result:
# Can be a playlist or a list of videos
video = result['entries'][0]
else:
# Just a video
video = result
print(video)
video_url = video['url']
print(video_url)
Python 3.6 - How to use the external downloader in Youtube-dl
opts = {
'format': "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
'external_downloader' : 'aria2c',
'external_downloader_args' :['--max-connection-per-server=16','--dir=/home/downloads'],
}
convert youtube-dl cmd command to python script
you can easily embed youtube-dl as:
import youtube_dl
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])
check here for more option.
youtube-dl python library documentation
If you download a version from github you can generate sphinx-docs which is useful for developement. Then using pythons help function usually gives some idea what the purpose of a function is
>>> import youtube_dl as yt
>>> help(yt)
Furthermore I find ipython a useful tool to explore code using the %edit
magic.
%edit yt.main
would bring you directly to the source of the main function.
How to Embed Youtube-dl in a Python Script
You need to install the module first.
You can do this by using the command pip install youtube_dl
in your terminal.
If pip in not installed, follow these guidelines: https://pip.pypa.io/en/stable/installing/
Get video from Youtube-DL is not working properly
from youtube_dl import YoutubeDL
link = "https://www.youtube.com/watch?v=Y9wBC3H4iH4"
with YoutubeDL({}) as ydl:
info = ydl.extract_info(link, download=False)
for i, format in enumerate(info['formats']):
print(f"[{i}] {format['format']}")
output:
[youtube] Y9wBC3H4iH4: Downloading webpage
[0] 249 - audio only (tiny)
[1] 250 - audio only (tiny)
[2] 251 - audio only (tiny)
[3] 140 - audio only (tiny)
[4] 160 - 256x144 (144p)
[5] 278 - 256x144 (144p)
[6] 242 - 426x240 (240p)
[7] 133 - 426x240 (240p)
[8] 243 - 640x360 (360p)
[9] 134 - 640x360 (360p)
[10] 244 - 854x480 (480p)
[11] 135 - 854x480 (480p)
[12] 247 - 1280x720 (720p)
[13] 136 - 1280x720 (720p)
[14] 248 - 1920x1080 (1080p)
[15] 137 - 1920x1080 (1080p)
[16] 18 - 640x360 (360p)
[17] 22 - 1280x720 (720p)
It literally says audio only
for some of the formats! Select one of the non-audio-only formats, and you won't get audio only. Note that which formats are available very much depends on which video you're trying to download.
Download only audio from youtube video using youtube-dl in python script
Read on in the developer instructions for an amended example:
from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
This will download an audio file if possible/supported. If the file is not mp3 already, the downloaded file be converted to mp3 using ffmpeg or avconv. For more information, refer to the format
and postprocessors
documentation entries in a current version of youtube-dl.
Related Topics
How to Force Python to Be 32-Bit on Snow Leopard and Other 32-Bit/64-Bit Questions
Display Fullscreen Mode on Tkinter
How to Force Django to Ignore Any Caches and Reload Data
Is There a Generator Version of 'String.Split()' in Python
Generate All Permutations of a List Without Adjacent Equal Elements
Is It Feasible to Compile Python to MAChine Code
How to Append Data to a JSON File
Override Python's 'In' Operator
Time Complexity of Python Set Operations
How to Use Selenium with Python
2D List Has Weird Behavor When Trying to Modify a Single Value
How to Find Out If Python Is Compiled with Ucs-2 or Ucs-4