AhFei

AhFei

简洁的写作需要勇气

yt-dlp Download YouTube Videos - A Convenient Command-Line Tool

yt-dlp is a fork of youtube-dl, GitHub: yt-dlp/yt-dlp: A youtube-dl fork with additional features and fixes (github.com), which supports downloading videos from many websites, including YouTube, Twitter, Twitch, Bilibili, and more.

Unless otherwise specified, execute the commands in the order they appear in the article's code blocks, one by one, to achieve the goal.
Applicable systems: Debian-based distributions, including Ubuntu and Armbian; other distributions can generally adapt the commands slightly.

Estimated time to complete: 12 minutes

I might have made some mistakes, please let me know if I’ve gotten anything wrong!

Due to Google's restrictions, youtube-dl has very slow download speeds, and the last time it was used, it could no longer download from YouTube. The GitHub for youtube-dl: ytdl-org/youtube-dl: Command-line program to download videos from YouTube.com and other video sites (github.com)

Below is a demonstration of its integration with a web notepad for convenient daily use.

image


Overview (with /home/vfly2 as the home directory)

yt-dlp is a single-file program that also requires yt-dlp.conf to save configuration files, as well as FFmpeg to merge audio and video.

This article also involves a script file, a video link file, and an nfo conversion file. The final file locations are as follows:

/usr/local/bin/
├── ffmpeg
├── ffprobe
├── nfo.sh
├── yt-dlp
└── yt-dlp.conf

/home/vfly2
├── yt-dlp.sh
└── ytdlplist

Only these 7 files, clear at a glance.


Installation and Setup#

Install prerequisite tools

sudo apt install -y wget curl

Ensure Python version is 3.7+

python --version

Install yt-dlp#

Copy and run the command to install

sudo curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/yt-dlp && sudo chmod a+rx /usr/local/bin/yt-dlp

Update

sudo yt-dlp -U

Install dependencies FFmpeg and ffprobe#

Due to bugs in the original FFmpeg used for yt-dlp, yt-dlp created this GitHub: yt-dlp/FFmpeg-Builds: FFmpeg Builds for yt-dlp (github.com)

Copy and run the command to install

wget https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz && \
tar -Jxv -f ffmpeg-master-latest-linux64-gpl.tar.xz && \
sudo mv ./ffmpeg-master-latest-linux64-gpl/bin/* /usr/local/bin/

# Clean up
rm -r ffmpeg-master-latest-linux64-gpl
rm ffmpeg-master-latest-linux64-gpl.tar.xz

Check ffmpeg version to ensure successful installation

ffmpeg -version

This can also be used for BBDown.

Configuration#

For more configuration options, check the project homepage: https://github.com/yt-dlp/yt-dlp#configuration

sudo vim /usr/local/bin/yt-dlp.conf

Note that the configuration file cannot contain Chinese characters, or it will throw an error.

# filename
-o '%(title)s.%(ext)s'

# thumbnail to jpg
--write-thumbnail
--convert-thumbnails jpg

# proxy
#--proxy 127.0.0.1:7890

# other
--write-description
--write-subs
--compat-options no-live-chat
#--remux-video mkv

# Execute a command
# %(filepath,_filename|)q is appended to the end of the command.
# --exec 'bash /usr/local/bin/nfo.sh'

Do not set the download directory here; set it in the script

--paths /home/vfly2/youtube

NFO Conversion Script#

If you want to use it with Emby, you can refer to the following section. Otherwise, you can skip it.

In the configuration file, there is an option --write-description, which saves the video description (description file), but since it is plain text, Emby cannot recognize it. The following is to execute a script using the --exec option after downloading to convert the description file into an nfo file that Emby can recognize.


First, uncomment --exec 'bash /usr/local/bin/nfo.sh' in the configuration file. --exec will pass the video path and name to the script.

Then create the conversion script

sudo touch /usr/local/bin/nfo.sh && sudo chmod a+x /usr/local/bin/nfo.sh

Edit

sudo vim /usr/local/bin/nfo.sh
#!/bin/bash

# Generate temporary files
tee nfo1.temp <<-'EOF'
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<movie>
  <plot><![CDATA[
EOF

tee nfo2.temp <<-'EOF'

]]></plot>
</movie>
EOF

# Generate an nfo file recognizable by Emby and add line breaks
file=$1
filename=${file%.*}
nfo=${filename}.nfo
description=${filename}.description

touch "${nfo}";
cat nfo1.temp > "${nfo}";
# Use `sed` command to add `<br/>` line break tags at the end of each line in the read `${description}` file
sed 's#$#&<br/>#g' "${description}" >> "${nfo}";
cat nfo2.temp >> "${nfo}";

# Delete temporary files
rm nfo1.temp nfo2.temp

This way, the script will be called after the download is complete to perform the conversion.

Usage#

For YouTube, the default download format is WebM, which Emby can recognize.

yt-dlp https://www.youtube.com/watch?v=gIbfYsQfNWs

Using a proxy

yt-dlp --proxy http://127.0.0.1:10809 https://www.youtube.com/watch?v=gIbfYsQfNWs

yt-dlp --proxy socks5://127.0.0.1:10808/ https://www.youtube.com/watch?v=gIbfYsQfNWs

Batch Download#

The previous content may not clearly show the advantages of command-line tools over GUI tools, which are speed, convenience, and automation. Below is a small script to leverage its advantages.

Batch Download Script#

First, create a script file:

filename="yt-dlp.sh"
touch $filename && chmod +x $filename && vim $filename

Edit the content:

vim yt-dlp.sh

You need to modify the value of the URLLIST variable, which is the absolute path of the file where the video URLs will be saved.

#!/bin/bash
# download multiple platforms (youtube, ) videos according to the file containing urls 

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# If a proxy is needed
# export http_proxy="http://127.0.0.1:1080"
# export https_proxy="http://127.0.0.1:1080"
# export no_proxy=127.0.0.1,localhost

# Path of the file containing video URLs
URLLIST="/home/vfly2/ytdlplist"

# Get the content of a specified line from the file, variable 1 is the file path, variable 2 is the line number
function grepline(){
    url=$(cat ${1} | head -n ${2} | tail -n 1)
    echo ${url}
}

LINE=1
URL=$(grepline ${URLLIST} ${LINE})   # Get the first line
ENDLINE=$(cat ${URLLIST} | tail -n 1)

# Get one line at a time until the line content is EOF
until [ "${URL}" == "${ENDLINE}" ]
do

/usr/local/bin/yt-dlp ${URL}
# /usr/local/bin/yt-dlp ${URL} --paths /home/vfly2/youtube   # You can specify the download directory here
LINE=$((${LINE}+1))   # Increment by 1
URL=$(grepline ${URLLIST} ${LINE})   # Get the next line, exit the loop if it reaches the last line

done

/usr/local/bin/yt-dlp ${ENDLINE}   # Download the last line URL

Save the video links in ytdlplist:

vim /home/vfly2/ytdlplist

One link per line

https://www.youtube.com/watch?v=gIbfYsQfNWs
https://www.youtube.com/watch?v=laIvjmdM0Ww

Execute the script (default downloads to the current directory)

bash -ex ./yt-dlp.sh

Integrating with Web Notepad#

If you only need to download one or two videos each time, connecting to the terminal, editing files, and executing scripts can be cumbersome.

This blog previously introduced a web notepad: Sharing my minimalist web notepad used for two years - AhFei's Curse (vfly2.com), which supports using curl to fetch content.

You can save links anytime through the web notepad, then use curl to fetch the links, and automatically execute the script to download videos at intervals, saving you from manual operations.

The real-time aspect is a bit lacking; AhFei has changed the download directory to Emby's media library for viewing through Emby. This way, high-quality videos are saved directly, and unnecessary ones can be deleted after watching, without worrying about videos being taken down or needing to separately download and save them.


Similar to the script flow above:

filename="yt-dlp.sh"
touch $filename && chmod +x $filename && vim $filename
vim yt-dlp.sh

In addition to URLLIST, a variable WEBLIST has been added, which is the URL of the web notepad. Here, we use the demonstration set up by AhFei: https://forward.vfly.app/ytdlplist, where links are copied one per line.

Readers can directly use this public instance or set up their own: Sharing my minimalist web notepad used for two years - AhFei's Curse (vfly2.com)

#!/bin/bash
# download multiple platforms (youtube, ) videos

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# export http_proxy="http://127.0.0.1:1080"
# export https_proxy="http://127.0.0.1:1080"
# export no_proxy=127.0.0.1,localhost

# Path of the file containing video URLs
URLLIST="/home/vfly2/ytdlplist"
WEBLIST="https://forward.vfly.app/ytdlplist"

# Download video links from the webpage
curl -k $WEBLIST > $URLLIST

# Get the content of a specified line from the file, variable 1 is the file path, variable 2 is the line number
function grepline(){
    url=$(cat ${1} | head -n ${2} | tail -n 1)
    echo ${url}
}

LINE=1
URL=$(grepline ${URLLIST} ${LINE})   # Get the first line
ENDLINE=$(cat ${URLLIST} | tail -n 1)

# Get one line at a time until the line content is EOF
until [ "${URL}" == "${ENDLINE}" ]
do

# echo ${URL}
/usr/local/bin/yt-dlp ${URL}
# /usr/local/bin/yt-dlp ${URL} --paths /home/vfly2/youtube   # You can specify the download directory here
LINE=$((${LINE}+1))   # Increment by 1
URL=$(grepline ${URLLIST} ${LINE})   # Get the next line, exit the loop if it reaches the last line

done

/usr/local/bin/yt-dlp ${ENDLINE}   # Download the last line URL
# Send a completion flag to the web notepad
echo -e "\nabove have done, but may skip" >> $URLLIST
curl -k --data-urlencode "text@${URLLIST}" $WEBLIST

This way, every time you SSH, you only need to run the command below without needing to edit files and copy links.

bash -ex ./yt-dlp.sh

Original link: https://blog.vfly2.com/2023/10/download-youtube-videos-with-yt-dlp/

Copyright statement: All articles on this blog are original works by AhFei, unless otherwise stated, and are licensed under CC BY-NC-SA 4.0. Please indicate the source when reprinting AhFei's Curse (blog.vfly2.com) .

Stay updated ٩(•̤̀ᵕ•̤́๑)ᵒᵏᵎᵎᵎᵎ with clear and repeatable practical skills. Feel free to subscribe via RSS and leave comments for corrections.

You can join the Telegram group https://t.me/vfly2 to discuss any issues encountered during the article's steps.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.