5

I want to get the playlist title of a youtube playlist and store it in a variable in a bash script.

I have already tried these things:

  • -e flag but it lists only the video titles
  • -o flag and --get-filename but again the same
  • the best try was to run yt-dlp and grep the first download line that contains the playlist name, but that seems too much for a basic job

Also note that I don't want to use: yt-dlp -o "%(playlist_title)s" because I want the name of the playlist only.

Is it possible to get the playlist title with yt-dlp? or I have to use a workaround like Pafy?

Destroy666
  • 12,350

2 Answers2

4

You need to simply use --print (or -O) instead of -o:

yt-dlp [the rest of your command] --skip-download --print playlist_title

See documentation:

-O, --print [WHEN:]TEMPLATE     Field name or output template to print to screen, optionally prefixed with when to print it, separated by a ":".

--skip-download also prevents the download and you might want to add --no-warnings to output only the playlist title.

A full example with a playlist that outputs only one playlist title (for 1st video - -I 1:1):

yt-dlp https://youtube.com/playlist?list=PLpeFO20OwBF7iEECy0biLfP34s0j-8wzk -I 1:1 --skip-download --no-warning --print playlist_title
Destroy666
  • 12,350
4

The proper way of doing this (which will not fail if the first video in the playlist is unavailable), is by reading the title property from the JSON response, which can be acquired by using --dump-single-json (shorthand -J) together with --flat-playlist.

To then parse the JSON data and get the title, you can use jq:

playlist_title=$(yt-dlp "https://youtube.com/playlist?list=$playlist_id" \
                        --flat-playlist                                  \
                        --dump-single-json                               \
                | jq -r .title)
kwyntes
  • 191
  • 10