po5 / thumbfast

High-performance on-the-fly thumbnailer script for mpv
Mozilla Public License 2.0
760 stars 34 forks source link

Render real thumbnail resolution #31

Closed christoph-heinrich closed 1 year ago

christoph-heinrich commented 1 year ago

The written out thumbnail is sometimes not the same resolution as we requested, as a result of that the file size does not match the expected file size and thus nothing gets rendered.

We can instead calculate the real thumbnail size and use that for rendering.

This probably introduces black borders again under some circumstances. Maybe we could detect that there only is a 1px black border at the bottom and nothing at the top and then cut that out by reducing the render resolution by 1px, but that's all we can do because of centering. That is outside of the scope of this PR though and I don't really know how to reliably detect that yet.

The written out file is sometimes (intermittently) much smaller then expected. Maybe that happens when thumbnail generation gets interrupted by seeking? But whatever the real reason may be, I've added a threshold of 5 for throwing out the calculated resolution if it changes too much from the original. In practice I've only ever observed a difference of 1 or 2 unless I was meddling with the filter parameters directly, so there should be plenty of breathing room to not throw out any correct results.

Might close #27, please test. ref #28 ref #29

natural-harmonia-gropius commented 1 year ago

Maybe we could just get them from subprocess in spwan(),use run(). I'll try tomorrow.

christoph-heinrich commented 1 year ago

Afaik we only have one way communication because of a limitation in windows.

christoph-heinrich commented 1 year ago

The algorithm can now handle cases where the width and height change at the same time. In my testing the loop runs at most 2 times when the thumbnail file is valid, so performance shouldn't be a problem. Even with an invalid thumbnail file it runs at most 4 times.

christoph-heinrich commented 1 year ago

It used to only redraw when there was a new image. That has been rectified now.

christoph-heinrich commented 1 year ago

It calculates the real resolution and informs the script about it now only when necessary.

christoph-heinrich commented 1 year ago

It was possible for a file size of 0 to be detected as a valid thumbnail with 0 width, which would result in an error in overlay-add. Now it checks the difference in size to make sure nothing wrong gets through.