4

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?

0

2 Answers 2

3

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
4
  • Using -I 1:1 will fail when the first video in the playlist is unavailable.
    – kwyntes
    Commented Feb 13 at 12:12
  • Not too sure there's any good solution for that except cleaning your playlists. Perhaps a script checking for command success and looping numbers until valid one is found.
    – Destroy666
    Commented Feb 13 at 14:11
  • You can find it in the JSON response, see my answer.
    – kwyntes
    Commented Feb 14 at 16:18
  • For the ones searching for the title of each video/item, yt-dlp --skip-download --print title URL
    – Pablo A
    Commented Mar 15 at 0:50
2

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)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .