Repository: yt-dlp
Issue: [LastFM] Extract album cover from page, not from YouTube video
Labels: site-enhancement

### DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE

- [X] I understand that I will be **blocked** if I *intentionally* remove or skip any mandatory\* field

### Checklist

- [X] I'm requesting a site-specific feature
- [X] I've verified that I'm running yt-dlp version **2023.07.06** ([update instructions](https://github.com/yt-dlp/yt-dlp#update)) or later (specify commit)
- [X] I've checked that all provided URLs are playable in a browser with the same IP and same login details
- [X] I've searched [known issues](https://github.com/yt-dlp/yt-dlp/issues/3766) and the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues **including closed ones**. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)
- [ ] I've read about [sharing account credentials](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#are-you-willing-to-share-account-details-if-needed) and I'm willing to share it if required

### Region

Hong Kong, China

### Example URLs

Song page: https://www.last.fm/music/ABBA/_/Lay+All+Your+Love+on+Me

Album page: https://www.last.fm/music/ABBA/Gold:+Greatest+Hits

Artist page: https://www.last.fm/music/ABBA

### Provide a description that is worded well enough to be understood

## Preface

LastFM differs from other music streaming platforms in that they don't host the files by themselves. Instead, the web page contains an embedded YouTube video player, which is controlled by JavaScript to play/pause/seek at user's request.

Currently, yt-dlp's LastFM extractor simply finds the YouTube link of said video from within the HTML document, then passes that to the YouTube extractor which does the rest of the work. This works wonders if you just want to download the music video, or have it audio only. However, as a result of calling the YouTube extractor, "thumbnail" related options will grab thumbnails from the YouTube video, not from the LastFM page.

## Issue

I want to download a song from LiveFM, embed the album cover into the audio file, and add media metadata, so I run:

```bash
yt-dlp -f m4a --embed-thumbnail --add-metadata https://www.last.fm/music/ABBA/_/Lay+All+Your+Love+on+Me
```

The result is pretty good, except that it used this image as the album cover:

![Lay All Your Love On Me  5mHzaIehRTE](https://github.com/yt-dlp/yt-dlp/assets/25703836/13112dc7-aac6-40ec-b4e0-29975239607c)

See the brown borders on the left and right hand side? This is because `--embed-thumbnail` grabbed the video thumbnail which has those borders. Ideally we should use the album cover shown on the LastFM page instead: it doesn't have such ugly borders.

## Solution

I suggest extracting the image at the following location on the LastFM page to use as the thumbnail:

![Screenshot 2023-07-16 at 09-52-06 Gimme! Gimme! Gimme! (A Man After Midnight) — ABBA Last fm](https://github.com/yt-dlp/yt-dlp/assets/25703836/81d8e643-f647-47ab-b13d-d5a02e9694c5)

Reasons:

- This image is present on all song, album and artist pages (see Example URLs)
- This image is guaranteed to be of the highest resolution
- This image always has a (near) 1:1 aspect ratio, perfect for album art

We can use CSS selector `.header-new-background-image` on the HTML document to get a `<div>` element. The image URL can then be extracted from the "style" attribute of this element.

I suppose I could have a go implementing this myself then send you a pull request, but I'm know little about the internals of yt-dlp so I'm sure someone can do a better job at coding this up. Thank you!

### Provide verbose output that clearly demonstrates the problem

- [X] Run **your** yt-dlp command with **-vU** flag added (`yt-dlp -vU <your command line>`)
- [ ] If using API, add `'verbose': True` to `YoutubeDL` params instead
- [X] Copy the WHOLE output (starting with `[debug] Command-line config`) and insert it below

### Complete Verbose Output

```shell
[debug] Command-line config: ['-vU', '-f', 'm4a', '--embed-thumbnail', '--add-metadata', 'https://www.last.fm/music/ABBA/_/Lay+All+Your+Love+on+Me']
[debug] Portable config "D:\scoop\apps\yt-dlp\current\yt-dlp.conf": []
[debug] Encodings: locale cp936, fs utf-8, pref cp936, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version stable@2023.07.06 [b532a3481] (win_exe)
[debug] Python 3.8.10 (CPython AMD64 64bit) - Windows-10-10.0.19045-SP0 (OpenSSL 1.1.1k  25 Mar 2021)
[debug] exe versions: ffmpeg 6.0-full_build-www.gyan.dev (setts), ffprobe 6.0-full_build-www.gyan.dev
[debug] Optional libraries: Cryptodome-3.18.0, brotli-1.0.9, certifi-2023.05.07, mutagen-1.46.0, sqlite3-2.6.0, websockets-11.0.3
[debug] Proxy map: {}
[debug] Loaded 1855 extractors
[debug] Fetching release info: https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest
Available version: stable@2023.07.06, Current version: stable@2023.07.06
Current Build Hash: 5ff3e702171a50175c34397494e2d18ce35d771c2110b1e59bd173ec2fb352aa
yt-dlp is up to date (stable@2023.07.06)
[LastFM] Extracting URL: https://www.last.fm/music/ABBA/_/Lay+All+Your+Love+on+Me
[LastFM] Lay+All+Your+Love+on+Me: Downloading webpage
[youtube] Extracting URL: https://www.youtube.com/watch?v=5mHzaIehRTE
[youtube] 5mHzaIehRTE: Downloading webpage
[youtube] 5mHzaIehRTE: Downloading ios player API JSON
[youtube] 5mHzaIehRTE: Downloading android player API JSON
[youtube] 5mHzaIehRTE: Downloading m3u8 information
[debug] Sort order given by extractor: quality, res, fps, hdr:12, source, vcodec:vp9.2, channels, acodec, lang, proto
[debug] Formats sorted by: hasvid, ie_pref, quality, res, fps, hdr:12(7), source, vcodec:vp9.2(10), channels, acodec, lang, proto, size, br, asr, vext, aext, hasaud, id
[info] 5mHzaIehRTE: Downloading 1 format(s): 140
[info] Downloading video thumbnail 41 ...
[info] Writing video thumbnail 41 to: Lay All Your Love On Me [5mHzaIehRTE].webp
[debug] Invoking http downloader on "https://rr4---sn-i3b7knzs.googlevideo.com/videoplayback?expire=1689526481&ei=ccyzZPqTH9SK1d8P0v2ZwAc&ip=219.76.157.143&id=o-AIXdRl06JWWOMegN6jeqgesPATwOKW9wALQj5q0RmIMH&itag=140&source=youtube&requiressl=yes&mh=gc&mm=31%2C29&mn=sn-i3b7knzs%2Csn-i3belnl6&ms=au%2Crdu&mv=m&mvi=4&pl=25&gcr=hk&initcwndbps=972500&vprv=1&svpuc=1&mime=audio%2Fmp4&gir=yes&clen=4444528&dur=274.466&lmt=1581247616229830&mt=1689504586&fvip=4&keepalive=yes&fexp=24007246&beids=24350018&c=IOS&txp=5531432&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cgcr%2Cvprv%2Csvpuc%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRAIgel3WnGcKoxMiwf8mWJbBRSDiEe3Gd4CFXdEGBaP4WnkCIAagMs5h7tgB0td5iIFJrBHWIE6Rj0b3-FjED6Edll2P&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIhAPoteFTzSHcYsIlVNVN5QUc8aXxmDOGrvP0JUa49zbSAAiBVZnP6TsGXDwP5WeC_arLjCxwurIfWFgKDVUr0HCRxlg%3D%3D"
[debug] File locking is not supported. Proceeding without locking
[download] Destination: Lay All Your Love On Me [5mHzaIehRTE].m4a
[download] 100% of    4.24MiB in 00:00:01 at 2.47MiB/s
[FixupM4a] Correcting container of "Lay All Your Love On Me [5mHzaIehRTE].m4a"
[debug] ffmpeg command line: ffmpeg -y -loglevel "repeat+info" -i "file:Lay All Your Love On Me [5mHzaIehRTE].m4a" -map 0 -dn -ignore_unknown -c copy -f mp4 -movflags "+faststart" "file:Lay All Your Love On Me [5mHzaIehRTE].temp.m4a"
[Metadata] Adding metadata to "Lay All Your Love On Me [5mHzaIehRTE].m4a"
[debug] ffmpeg command line: ffmpeg -y -loglevel "repeat+info" -i "file:Lay All Your Love On Me [5mHzaIehRTE].m4a" -map 0 -dn -ignore_unknown -vn -acodec copy -write_id3v1 1 -metadata "title=Lay All Your Love On Me" -metadata "date=20181212" -metadata "description=Provided to YouTube by Universal Music Group

Lay All Your Love On Me · ABBA

Super Trouper

℗ 1980 Polar Music International AB

Released on: 2001-01-01

Producer, Associated  Performer, Recording  Arranger, Keyboards: Benny Andersson
Producer, Associated  Performer, Recording  Arranger, Guitar: Björn Ulvaeus
Associated  Performer, Vocals: Agnetha Fältskog
Associated  Performer, Background  Vocalist: Anni-Frid Lyngstad
Associated  Performer, Guitar: Lasse Wellander
Associated  Performer, Drums: Ola Brunkert
Associated  Performer, Bass: Rutger Gunnarsson
Composer  Lyricist: Benny Andersson
Composer  Lyricist: Björn Ulvaeus

Auto-generated by YouTube." -metadata "synopsis=Provided to YouTube by Universal Music Group

Lay All Your Love On Me · ABBA

Super Trouper

℗ 1980 Polar Music International AB

Released on: 2001-01-01

Producer, Associated  Performer, Recording  Arranger, Keyboards: Benny Andersson
Producer, Associated  Performer, Recording  Arranger, Guitar: Björn Ulvaeus
Associated  Performer, Vocals: Agnetha Fältskog
Associated  Performer, Background  Vocalist: Anni-Frid Lyngstad
Associated  Performer, Guitar: Lasse Wellander
Associated  Performer, Drums: Ola Brunkert
Associated  Performer, Bass: Rutger Gunnarsson
Composer  Lyricist: Benny Andersson
Composer  Lyricist: Björn Ulvaeus

Auto-generated by YouTube." -metadata "purl=https://www.youtube.com/watch?v=5mHzaIehRTE" -metadata "comment=https://www.youtube.com/watch?v=5mHzaIehRTE" -metadata "artist=ABBA" -metadata "album=Super Trouper" -movflags "+faststart" "file:Lay All Your Love On Me [5mHzaIehRTE].temp.m4a"
[ThumbnailsConvertor] Converting thumbnail "Lay All Your Love On Me [5mHzaIehRTE].webp" to png
[debug] ffmpeg command line: ffmpeg -y -loglevel "repeat+info" -f image2 -pattern_type none -i "file:Lay All Your Love On Me [5mHzaIehRTE].webp" -update 1 -movflags "+faststart" "file:Lay All Your Love On Me [5mHzaIehRTE].png"
[EmbedThumbnail] mutagen: Adding thumbnail to "Lay All Your Love On Me [5mHzaIehRTE].m4a"
```