Search code examples
bashyoutube-dl

Youtube-dl -o format: can I use conditions


I see so many questions on youtube-dl's -f flag. I want to know about the -o flag. I basically want to do:

-o '%(artist ? artist : "Unknown Artist")s/%(album ? album : "Unknown Album")s/%(track ? track : title)s.%(ext)s'

or if you come from some languages, this might make more sense:

-o '%(artist ?? "Unknown Artist")s/%(album ?? "Unknown Album")s/%(track ?? title)s.%(ext)s'

So how can I do this? The goal is to avoid youtube-dl from making default folders like NA, or to use a more appropriate field when applicable, or even pass in shell args to use as defaults when the tags don't exist.

EDIT Since it was requested, here is what prompted this:

I am trying to download this playlist. Let's look at these two videos in particular:

When I run

youtube-dl -o '%(artist)s/%(album)s/%(track)s.%(ext)s' <url>

the resulting output directories are

  • The Oh Hellos/Dear Wormwood/Prelude.f137.mp4
  • NA/NA/NA.f137.mp4

The first one is completely acceptable, but the second one is obviously not. I would like to be able to apply a conditional formatting so that the output of the first command stays the same, and the second command outputs either

  • The Oh Hellos/Dear Wormwood/Exeunt.f137.mp4 or
  • Unknown Artist/Unknown Album/The Oh Hellos - Exeunt.f137.mp4

Solution

  • You have three options.

    • Use --output-na-placeholder Unknown to make Unknown directories instead of NA directories.

    • Create symbolic links for NA to Unknown Artist/Unknown Album:
      mkdir "Unknown Artist"; ln -s "Unknown Artist" NA; cd "Unknown Artist"; mkdir "Unknown Album"; ln -s "Unknown Album" NA

    • Use yt-dlp for defaults:
      yt-dlp -o '%(artist|Unknown Artist)s/%(album|Unknown Album)s/%(track|Unknown Track)s.%(ext)s' <url>
      Tested: [download] Destination: Unknown Artist/Unknown Album/Unknown Track.mp4