Date created: Sunday, September 8, 2024 2:58:51 PM. Last modified: Sunday, October 27, 2024 4:49:45 PM

'yt-dlp' - Notes

Docker

Dockerfile

ARG ARCH="arm64"
FROM ubuntu:22.04

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -y --no-install-recommends install \
ca-certificates ffmpeg curl wget python3 mp3gain && \
ln /usr/bin/python3 /usr/bin/python

RUN apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/yt-dlp && \ chmod a+rx /usr/local/bin/yt-dlp
COPY ./mp3.sh /bin/mp3.sh
RUN chmod a+rx /bin/mp3.sh
WORKDIR /opt/yt-dlp/ ENTRYPOINT ["/usr/local/bin/yt-dlp"]

 

docker-compose.yml:

services:
  yt-dlp:
    build:
      context: .
      dockerfile: Dockerfile
    network_mode: host # This is to work around the odroid kernel problem of running more than 3/4 containers even when no ports are exposed
    volumes:
      - /data/:/opt/yt-dlp/:rw
    restart: never

 

mp3.sh:

#!/usr/bin/env bash

set -eu

# First download the file, with "safe" filenames
/usr/local/bin/yt-dlp -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 --windows-filenames $@

# Now get the filename - assume last arg is the URL
# This is always the original filename, if we converted a video to mp3 for example,
# this ends in .webm and not .mp3:
videoname=$(/usr/local/bin/yt-dlp --print filename --windows-filenames "${@: -1}")
mp3name="${videoname%.*}.mp3"

echo ""
echo "mp3 name is: ${mp3name}"
mp3gain -c "${mp3name}"

 

Downloading Audio

docker compose run --rm --entrypoint mp3.sh yt-dlp https://www.youtube.com/watch?v=xxxx

 

Downloading Video

docker compose run --rm yt-dlp https://www.youtube.com/watch?v=xxxx

 

Downloading a Channel

If downloading all videos on a channel by specifying the channel URL (e.g. https://www.youtube.com/@UKNOFconf) everything is downloaded is downloaded into a single folder, yt-dlp doesn't recognise that videos are part of playlists and can't put them into sub-folders based on the playlist they are in. If downloading all playlists from a channel (e.g. https://www.youtube.com/@UKNOFconf/playlists) yt-dlp can put the videos into per-playlist sub-folders.

One can download using the --download-archive option which writes the IDs of all successfully downloaded videos to a file. With this option one can first run yt-dlp with the playlists URL for the channel to download all videos which are in playlists and have them organised by playlist. After one can run the command again with the channel base URL, and only the remaining videos not in playlists will be downloaded due to the --download-archive option.

--format-sort can be used to specify how the best video/audio is chosen. The default for best video is the highest resolution, but there may be multiple videos at the same resolution, below it is specified to look by resolution (heigh) and then by bit rate within all videos at that highest resolution.

"bestvideo*" will download the highest quality video, even if it doesn't have audio, in this case the highest quality audio will then be downloaded and merged with the video.

docker compose run --rm yt-dlp \
--format-sort height,tbr \ --format "bestvideo*+bestaudio" \ --output "%(uploader)s/%(playlist_title)s/%(upload_date)s_%(title)s_%(id)s_%(format_id)s.%(ext)s" \ --restrict-filenames \ --ignore-errors \ --write-thumbnail \
--write-annotations \
--write-subs \ --retries 4 \
--download-archive archive.txt \ --continue \
https://www.youtube.com/@UKNOFconf/playlists

 


Previous page: 'youtube-dl' Notes
Next page: 'yq' - Notes