Chocobozzz / PeerTube

ActivityPub-federated video streaming platform using P2P directly in your web browser
https://joinpeertube.org/
GNU Affero General Public License v3.0
13.07k stars 1.51k forks source link

Implement sharpening for thumbnails & previews #6232

Open veikk0 opened 8 months ago

veikk0 commented 8 months ago

Describe the problem to be solved

As far as I've been able to determine, PeerTube doesn't currently sharpen the thumbnails or previews it generates.

The issues of thumbnail and preview quality have previously been raised in #5618 and #5464, and while I agree that thumbnail resolution should be increased somewhat, purely increasing the resolution isn't the only way to augment the perceived quality of thumbnails.

For websites operating at large enough scale, sharpening is a fairly common way of counteracting the reduction of detail that occurs as a consequence of downscaling images to thumbnail sizes.

Describe the solution you would like

FFmpeg includes multiple sharpening filters. unsharp is probably the most widely used.

Below are thumbnails generated with FFmpeg at 336x188 (the thumbnail resolution Youtube serves me), with sharpening and without, as well as a thumbnail generated by PeerTube currently (280x157). The FFmpeg filters used were scale=336:188,unsharp=3:3:0.6.

normal

unsharp

peertube-frontpage

(source image)

To compare the fidelity between sharpened/not-sharpened images, I recommend opening them in tabs and switching between them.

In terms of file sizes, not-sharpened image is 22.5 kb, the sharpened image is 25.1 kb, and the lower-resolution Peertube-generated thumbnail is 23.7 kb. However, the -q:v parameter can be used to change the quality and thus the file size. Even at these fairly similar file sizes, the resolution increase and sharpening haven't drastically increased the file size at -q:v 5, while the perceived quality has increased noticeably.

Chocobozzz commented 8 months ago

Hi,

Thanks for this interesting issue. Do you have resources on what value would be best (depending on image size and ffmpeg CPU) for -q:v or unsharp?

FYI ffmpeg doesn't scale the generated image, we use jimp (and so jpeg-js) for that purpose.

veikk0 commented 8 months ago

From my testing, -q:v 5 or 4 are a good setting for both thumbnails and the previews. In theory, the bigger the image the less noticeable the artifacts are and thus you can use higher values, but since the previews get displayed at higher than their native resolution of 850x480, keeping artifacts to a minimum is probably a good idea.

In terms of CPU usage, the impact is so minuscule that I'd have to process a large batch of images on my 15W dual-core from 2015 to even measure it. It's basically an instant operation.

Chocobozzz commented 8 months ago

I added the q:v option in https://github.com/Chocobozzz/PeerTube/commit/fb2dc408585dbef574aa8dbacb23981a2ffba1b0

I'm a little bit more hesitant with the unsharp filter as the value seems like vodoo to me ^^ And from my tests I've noticed that sometimes it can decrease the thumbnail quality. But I'm not against adding an admin config so they can choose to enable/disable this filter if we implement it (it's just more work)./