Skip to content

Instantly share code, notes, and snippets.

@siniradam
Created June 4, 2025 15:54
Show Gist options
  • Save siniradam/9855f39e7f445e4f3364cccb9421d6d3 to your computer and use it in GitHub Desktop.
Save siniradam/9855f39e7f445e4f3364cccb9421d6d3 to your computer and use it in GitHub Desktop.
Downloading youtube videos with single command

Youtube Video Downloader

Instead of everytime dealing with youtube downloader websites, actually you can download it yourself. With this bash script you just need to enter the id of a youtube video or playlist, and you can download it.

  1. You need to download yt-dlp
  2. I am using mac, so just copied yt-dlp binary to /usr/local/bin folder.
  3. Append the code below to your ~/.zprofile file. (Or what ever bash file that suits you)

Usage

For downloading a video copy the video id and execute it like this:

ytv dQw4w9WgXcQ

Or you can download just the audio

yta dQw4w9WgXcQ

You can use it for youtube playlists as well.

ytp PLv1AUcJnyG9vMwxGQUDdWUFYoBownaUnp

Anatomy of a youtube link

  • Video ID in a youtube link /watch?v=aDaOgu2CQtI
  • Playlist ID in a youtube playlist /playlist?list=PLv1AUcJnyG9vMwxGQUDdWUFYoBownaUnp

Explanation of parameters

Playlist function also creates a folder for each item in the playlist.

yt-dlp -o '%(playlist)s/%(playlist_index)s - %(title)s/%(title)s.%(ext)s' \
  • %(playlist)s Playlist name
  • %(playlist_index)s Position number in playlist.
  • %(title)s Video title
  • %(ext)s Extension of the video.

Additionally you can make some changes for the final file name. This line below for replacing words, extra spaces etc.

--replace-in-metadata 'title' '^\s+|\s+$' '' \

Save thumnails:

--write-thumbnail

Creates a URL link file.

--write-link

Download subtitles if available.

  --write-subs

See more about the parameters here.

Script

ytv(){
yt-dlp -o '%(title)s/%(title)s.%(ext)s' \
  --write-thumbnail \
  --write-subs \
  --write-link \
  --quiet \
  --write-description \
  --progress \
  --parse-metadata 'title:%(title)s' \
  --replace-in-metadata 'title' 'FULL' '' \
  --replace-in-metadata 'title' 'HD' '' \
  --replace-in-metadata 'title' '  +' ' ' \
  --replace-in-metadata 'title' '^\s+|\s+$' '' \
  --exec "if [ -f '%(title)s/%(title)s.%(thumbnails_ext)s' ]; then ext='%(thumbnails_ext)s'; mv '%(title)s/%(title)s.$ext' '%(title)s/backdrop.$ext'; fi" \
  "https://www.youtube.com/watch?v=$1"
}

# Download youtube videos as audio with yt-dlp
yta(){
yt-dlp -o '%(title)s/%(title)s.%(ext)s' \
  --write-thumbnail \
  --write-link \
  --quiet \
  --write-description \
  --extract-audio \
  --audio-format mp3 \
  --progress \
  --parse-metadata 'title:%(title)s' \
  "https://www.youtube.com/watch?v=$1"
}

# Download Youtube Playlist with yt-dlp
ytp(){
yt-dlp -o '%(playlist)s/%(playlist_index)s - %(title)s/%(title)s.%(ext)s' \
  --write-thumbnail \
  --write-subs \
  --write-link \
  --quiet \
  --write-description \
  --progress \
  --parse-metadata 'title:%(title)s' \
  --replace-in-metadata 'title' 'FULL' '' \
  --replace-in-metadata 'title' 'HD' '' \
  --replace-in-metadata 'title' ' \|$' '' \
  "https://www.youtube.com/playlist?list=$1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment