Skip to content

Instantly share code, notes, and snippets.

@ethicnology
Last active December 29, 2022 08:51
Show Gist options
  • Save ethicnology/fdaa7de3acbbed5d3044cf022a4fd54c to your computer and use it in GitHub Desktop.
Save ethicnology/fdaa7de3acbbed5d3044cf022a4fd54c to your computer and use it in GitHub Desktop.
yt-dlp download from batch-file with S01E01 like prefix based on the index of the URL in the file

yt-dlp download from batch-file with S01E01 like prefix based on the index of the URL in the file

Here is a brief description of what the Bash command I provided does:

  • It sets the variable $season to the season number.
  • It sets the variable $index to the current video index.
  • It uses a while loop to read each line of the file batch-url.txt and extract the URL from each line using awk.
  • If the line is not empty and does not start with a # character (i.e. if it is not a comment), it passes the URL to the yt-dlp command with the filename prefix "S" followed by the season number (contained in the $season variable), followed by "E" and the index of the current line, with a leading zero added if the index is less than 10 (e.g. "01" instead of "1").

This command allows you to download multiple videos from an input batch file by adding the season number and index of each video to the filename prefix.
Note: this command will only work if the yt-dlp command is installed on your system. If it is not, you will need to replace it with the command you wish to use to download the videos.
Additionally, this command assumes that each line of the input batch file contains only one URL. If you need to handle empty lines or comments (starting with #), you should add a condition to ignore them in the while loop.

bash script

season=01
index=1
while read line; do
    url=$(echo "$line" | awk '{print $1}')
    yt-dlp --output "S$(printf "%02d" $season)E$(printf "%02d" $index) %(title)s.%(ext)s" "$url"
    ((index++))
done < batch-url.txt

one-liner

season=1; index=1; while read line; do url=$(echo "$line" | awk '{print $1}'); [ -n "$url" ] && [ "${url:0:1}" != "#" ] && yt-dlp --output "S$(printf "%02d" $season)E$(printf "%02d" $index) %(title)s.%(ext)s" "$url"; ((index++)); done < batch-url.txt

with all subs (no default track) merged in an matroska (.mkv) container

yt-dlp --embed-subs --sub-langs all --convert-subs srt --ppa "EmbedSubtitle:-disposition:s:0 0" --merge mkv URL
season=1; index=1; while read line; do url=$(echo "$line" | awk '{print $1}'); [ -n "$url" ] && [ "${url:0:1}" != "#" ] && yt-dlp --embed-subs --sub-langs all --convert-subs srt --ppa "EmbedSubtitle:-disposition:s:0 0" --merge mkv --output "S$(printf "%02d" $season)E$(printf "%02d" $index) %(title)s.%(ext)s" "$url"; ((index++)); done < batch-url.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment