yt-dlp / yt-dlp

A feature-rich command-line audio/video downloader
https://discord.gg/H5MNcFW63r
The Unlicense
90.94k stars 7.07k forks source link

Output template: Support conditional statements #11583

Open hseg opened 6 days ago

hseg commented 6 days ago

DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE

Checklist

Provide a description that is worded well enough to be understood

I would like to format videos in playlists differently from videos out of playlists (specifically, I'm using %(playlist_title,upload_date>%Y-%m-%d&{} - |)s%(playlist_index&{} - |)s).

However, since the youtube:tab extractor presents videos from eg /@user/videos as a playlist, this means that unless I either manually list all the videos I want to download, or download channel updates and playlists separately, the above output template will have the wrong behaviour on channel tabs. A way to distinguish the two is desirable. For the remainder of the discussion, note that in this case playlist_id=channel_id.

Additional relevant context: On one of my devices, I'm using https://github.com/JunkFood02/Seal to wrap yt-dlp. While I have prodded them (https://github.com/JunkFood02/Seal/issues/1873) to support multiple output templates, at the moment they only support a single custom output template.

Four ways suggest themselves:

Provide verbose output that clearly demonstrates the problem

Complete Verbose Output

[debug] Command-line config: ['--ignore-config', '-vU', '--print', '%(playlist_title,upload_date>%Y-%m-%d&{} - |)s%(playlist_index|)s', 'https://www.youtube.com/@PracticalEngineering', '--max-downloads', '1']
[debug] Encodings: locale UTF-8, fs utf-8, pref UTF-8, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version stable@2024.11.18 from yt-dlp/yt-dlp [7ea278792]
[debug] Python 3.12.7 (CPython x86_64 64bit) - Linux-6.11.5-arch1-1-x86_64-with-glibc2.40 (OpenSSL 3.4.0 22 Oct 2024, glibc 2.40)
[debug] exe versions: ffmpeg 7.1 (setts), ffprobe 7.1, phantomjs broken, rtmpdump 2.4
[debug] Optional libraries: Cryptodome-3.20.0, brotli-1.1.0, certifi-2024.08.30, mutagen-1.47.0, requests-2.32.3, secretstorage-3.3.3, sqlite3-3.46.1, urllib3-1.26.20, websockets-12.0
[debug] Proxy map: {}
[debug] Request Handlers: urllib, requests
[debug] Loaded 1837 extractors
[debug] Fetching release info: https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest
Latest version: stable@2024.11.18 from yt-dlp/yt-dlp
yt-dlp is up to date (stable@2024.11.18 from yt-dlp/yt-dlp)
[youtube:tab] Extracting URL: https://www.youtube.com/@PracticalEngineering
[youtube:tab] @PracticalEngineering: Downloading webpage
[debug] [youtube:tab] Selected tab: 'videos' (videos), Requested tab: ''
[youtube:tab] Downloading all uploads of the channel. To download only the videos in a specific tab, pass the tab's URL
[download] Downloading playlist: Practical Engineering Australia - Videos
[youtube:tab] Playlist Practical Engineering Australia - Videos: Downloading 6 items of 6
[download] Downloading item 1 of 6
[youtube] Extracting URL: https://www.youtube.com/watch?v=HmLmp06cbnU
[youtube] HmLmp06cbnU: Downloading webpage
[youtube] HmLmp06cbnU: Downloading ios player API JSON
[youtube] HmLmp06cbnU: Downloading mweb player API JSON
[debug] Loading youtube-nsig.2d24ba15 from cache
[debug] [youtube] Decrypted nsig rvtzc3BXbPfKpM2A6 => MVujBRZCtmV6LA
[debug] Loading youtube-nsig.2d24ba15 from cache
[debug] [youtube] Decrypted nsig NvO8RpuawyiuEZinn => _eQFO4S5coqzFA
[youtube] HmLmp06cbnU: Downloading m3u8 information
[debug] Sort order given by extractor: quality, res, fps, hdr:12, source, vcodec, channels, acodec, lang, proto
[debug] Formats sorted by: hasvid, ie_pref, quality, res, fps, hdr:12(7), source, vcodec, channels, acodec, lang, proto, size, br, asr, vext, aext, hasaud, id
[debug] Default format spec: bestvideo*+bestaudio/best
[info] HmLmp06cbnU: Downloading 1 format(s): 136+251
Practical Engineering Australia - Videos - 1
[info] Maximum number of downloads reached, stopping due to --max-downloads
Aborting remaining downloads
bashonly commented 4 days ago

Out of the 4 suggestions, only conditional formatting in output templates would be both useful and feasible, but it would only be feasible after output template refactoring; see #8511

In the meantime, maybe you could hack a workaround with --parse-metadata like this:

--parse-meta "%(playlist_id)s %(playlist_index)s:PL[A-Za-z0-9_-]{32} (?P<playlist_index>[0-9]+)"
--parse-meta "%(playlist_id)s %(playlist_title)s:PL[A-Za-z0-9_-]{32} (?P<playlist_title>.+)"
-o "%(playlist_title,upload_date>%Y-%m-%d&{} - |)s%(playlist_index&{} - |)s.%(ext)s"
hseg commented 4 days ago

I was hoping you'd pick that option, but it didn't seem fair to push for it. Testing that command, though, it doesn't seem to work on parse failures, see the log below. I tried fixing this, by appending eg |.*(?P<playlist_title>), but re (unlike regex) doesn't support branch reset, and in particular can't use the same capture group across two alternatives. I tried using the "has matched" predicate, but ((?P<isPL>PL[A-Za-z0-9_-]{32})|[A-Za-z0-9_-]{24}) (?(isPL)(?P<playlist_index>[0-9]+)|.*) unexpectedly always parsed as a playlist. I'm a little lost, but then again it's late and I'm afk. Don't know if I'll have time to test this tomorrow.

The log:

[youtube:tab] Extracting URL: ***@***.***
[youtube:tab] @PracticalEngineeringChannel: Downloading webpage
[youtube:tab] Downloading all uploads of the channel. To download only the videos in a specific tab, pass the tab's URL
[youtube:tab] @PracticalEngineeringChannel/shorts: Downloading webpage
[youtube:tab] Downloading as multiple playlists, separated by tabs. To download as a single playlist instead, pass https://www.youtube.com/playlist?list=UUMOqf8ab-42UUQIdVoKwjlQ
[download] Downloading playlist: Practical Engineering
[youtube:tab] Playlist Practical Engineering: Downloading 2 items of 2
[download] Downloading item 1 of 2
[download] Downloading playlist: Practical Engineering - Videos
[youtube:tab] UCMOqf8ab-42UUQIdVoKwjlQ page 1: Downloading API JSON
[youtube:tab] UCMOqf8ab-42UUQIdVoKwjlQ page 2: Downloading API JSON
[youtube:tab] UCMOqf8ab-42UUQIdVoKwjlQ page 3: Downloading API JSON
[youtube:tab] UCMOqf8ab-42UUQIdVoKwjlQ page 4: Downloading API JSON
[youtube:tab] UCMOqf8ab-42UUQIdVoKwjlQ page 5: Downloading API JSON
[youtube:tab] UCMOqf8ab-42UUQIdVoKwjlQ page 6: Downloading API JSON
[youtube:tab] Playlist Practical Engineering - Videos: Downloading 189 items of 189
[download] Downloading item 1 of 189
[youtube] Extracting URL: https://www.youtube.com/watch?v=sH1PVVJuBtE
[youtube] sH1PVVJuBtE: Downloading webpage
[youtube] sH1PVVJuBtE: Downloading ios player API JSON
[youtube] sH1PVVJuBtE: Downloading mweb player API JSON
[youtube] sH1PVVJuBtE: Downloading player a8476471
[youtube] sH1PVVJuBtE: Downloading m3u8 information
[MetadataParser] Could not interpret '%(playlist_id)s %(playlist_index)s' as 'PL[A-Za-z0-9_-]{32} (?P<playlist_index>[0-9]+)'
[MetadataParser] Could not interpret '%(playlist_id)s %(playlist_title)s' as 'PL[A-Za-z0-9_-]{32} (?P<playlist_title>.+)'
[info] sH1PVVJuBtE: Downloading 1 format(s): 401+251
[download] Destination: /storage/emulated/0/Download/Seal/Practical_Engineering - Practical_Engineering_-_Videos1Which_Power_Plant_Does_My_Electricity_Come_From [sH1PVVJuBtE].f401.mp4
Canceled by user

El 22 de noviembre de 2024 00:03:49 GMT+02:00, bashonly @.***> escribió:

Out of the 4 suggestions, only conditional formatting in output templates would be both useful and feasible, but it would only be feasible after output template refactoring; see #8511

In the meantime, maybe you could hack a workaround with --parse-metadata like this:

--parse-meta "%(playlist_id)s %(playlist_index)s:PL[A-Za-z0-9_-]{32} (?P<playlist_index>[0-9]+)"
--parse-meta "%(playlist_id)s %(playlist_title)s:PL[A-Za-z0-9_-]{32} (?P<playlist_title>.+)"
-o "%(playlist_title,upload_date>%Y-%m-%d&{} - |)s%(playlist_index&{} - |)s.%(ext)s"

-- Reply to this email directly or view it on GitHub: https://github.com/yt-dlp/yt-dlp/issues/11583#issuecomment-2492429941 You are receiving this because you authored the thread.

Message ID: @.***>

hseg commented 4 days ago

But thanks for the help, regardless!