Word Count: 405
  • Post category:PCs
  • Post last modified:2025-01-09

Pre-requesite

  1. The yt-dlp library is installed on PC.
  2. The ffmpeg library is installed on PC.

Download a Youtube video

  1. Get the media available
    • Open a terminal at a suitable folder and issue the command below.
    • > yt-dlp -F <URL for the YT video>
    • for example: yt-dlp -F https://www.youtube.com/watch?v=pZyGYUMZAeg
    • This lists all video and audo files available for download. Each of these files has an ID.
  2. Select the audio and video media
    • > yt-dlp -f <video_ID>+<audio_id> <URL for the YT video>

Download a Youtube Playlist

  1. Get the playlist ID
    • Example for https://www.youtube.com/playlist?list=PLDzeHZWIZsTryvtXdMr6rPh4IDexB5NIA
    • the ID is PLDzeHZWIZsTryvtXdMr6rPh4IDexB5NIA
  2. Open a terminal at a suitable folder and issue the command with options below.
    • the option -f bestvideo*+bestaudio  is to ensure best possible quality video and audio format is selected.
    • -F or --list-formats will list all the available formats, from which we can select the custom format by specifying index (eg: -f '399+599')
    • -o %(playlist_index)02d-%(title)s.%(ext)s  is to save the videos with two-digit index prefix.
yt-dlp "https://www.youtube.com/playlist?list=PLRBClVey5BqwtEiRey4KILfmyDg6hOglw" --yes-playlist --format 'bestvideo*+bestaudio' --output "%(playlist_index)02d-%(title)s.%(ext)s"

Download a Youtube video as Song (MP3)

  • Get the video ID vid, and set audio format, audio quality, extract section from timestamp 5:01 to 16:43
    • > yt-dlp -x --embed-thumbnail --audio-format mp3 --audio-quality 320k --download-sections "*5:01-16:43" https://www.youtube.com/watch?v=vid
    • more above the –-download-sections REGEX parameter:
      • Download only chapters that match the regular expression.
      • A "*" prefix denotes time-range instead of chapter. Negative timestamps are calculated from the end.
      • The "*from-url" can be used to download between the “start_time” and “end_time” extracted from the URL. Needs ffmpeg.
      • This option can be used multiple times to download multiple sections, e.g. --download-sections 10:15-inf" --download-sections "intro"

Download multiple YouTube videos from URLs in a text file

  • The file links.txt contains the urls of the youtube videos, one video per line.
    • > yt-dlp --batch-file="links.txt" -f "bestvideo[height<=1080] [ext=mp4]+bestaudio[ext=m4a]/best[height<=1080][ext=mp4]/best" --embed-thumbnail --output "%(title)s.%(ext)s"

Reference

  1. Downloading YouTube Playlist in CLI using yt-dlp by Deepak Devanand
  2. The Easiest Way to Download Songs from YouTube Videos by Marin Todorov