-
Notifications
You must be signed in to change notification settings - Fork 7
feat: video thumbnail preview #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@houqp I was trying kiorg out, and thought it would be nice if I could see a preview (thumbnail) of videos, since I store most of my personal videos on my USB-stick and they have names that aren't very descriptive. If you have the time, could you share any thoughts on this change? There were a couple of improvements I was thinking of:
I'll work on adding tests in the meantime. |
thanks for the contribution, video support is something that I have wanted for myself, it's great that you picked it up.
We are already using the file_type crate for this use-case, see: Line 114 in 30bc8d3
However, I think parsing the file extension is good enough to keep it snappy and simple. If the user provided an incorrect file extension, it's better for them to fix it instead.
This is good idea, instead of going through the first 10%, which can be a lot depending on size of the video. It might be better to do a uniform sample of constant frames across the video to keep the overhead low. For example, extract 4 frames at 0, 25%, 50%, 75%. You can also take a look into key frame extraction, which is supposed to more likely occur during a scene change. |
@houqp I switched to a ffmpeg crate because video-rs doesn't support seeking to specific times within the video, and would therefore require a lot of processing to extract frames at 25%, 50%, 75% in large videos. Looks like all the tests are failing for At least some good news is the Any thoughts on what to do next? I can't find a crate that can handle seeking to specific parts of the video and doesn't require a local install of packages. Edit: Looking back, video-rs also requires ffmpeg to be installed on the machine. |
This comment was marked as outdated.
This comment was marked as outdated.
The result looks good, it's okay to have ffmpeg as a dependency, you can install it in the CI to make the pipelines pass. One thing to consider is that the built binary should not crash if ffmpeg is not installed. |
ce23d48
to
09c337d
Compare
The thumbnails are loading in a reasonable time (mostly <1s). Here's an example:
There is a bug where sometimes vertical videos are rotated the wrong way, I'll work on fixing that. Other than that, a lot of tuning can be done for the brightness, variance, and sharpness calculations to make the selection more accurate, but that will take a lot of time through manual testing. Would you want me to include that work in this PR? |
we can leave fine tuning into subsequent PRs, it's already better than what we have right now, so let's ship what we have. |
can probably do the score calculation in separate threads in parallel to reduce the load time as a future optimization |
Image rotation works, but takes 100-200ms. At least it's an infrequent operation. |
Tested with |
@houqp Ready for merge, let me know if you'd like any changes. |
@he-patrick one missing feature here is that the binary doesn't run on machines that don't have all the necessary libraries installed anymore. The video preview feature should be optional in the sense that when these libraries are present, it works out of the box, if not, it should still run, but display the current preview content or simply display a message to let the users know they need to install the system dependency to enable the preview. |
@houqp I think it's nice generating the placeholder thumbnail if ffmpeg is not available. Another option is to not generate the placeholder, but at least make the metadata table UI consistent with that of the images preview. WDYT? ![]() Also, thanks for your support on this PR 🙏 |
yes, that looks great to me. it would be good to still add a note at the end to let the user know that they need to install ffmpeg for full video preview 👍 |
Ok, this is a lot more complicated than I thought. I'll need to use something like the libloading crate to load ffmpeg at runtime. Including the ffmpeg crate in the dependencies breaks the application if ffmpeg isn't installed because the dynamic linker tries to resolve all the ffmpeg library symbols at startup time. Looks like this might take me some time to figure out, as I also have final exams in a few days 😅 |
no worries, take your time, let me know if you need suggestions on how to workaround this. i have a few ideas, but it's more fun if you figure this out by yourself ;) loading the dynamic library at runtime is the right first step. |
7f52fcb
to
c008e9f
Compare
Just came home from vacation and getting back on this. Sorry for the wait 🙏 |
Description:
This change introduces a thumbnail preview for videos. It uses ffmpeg-next to extract the raw RGB data of a frame at 0%, 25%, 50%, and 75% of a video. It then scores the frames based on their brightness, variance, and sharpness. If the extraction fails (i.e. the video file is corrupted, empty, etc.), then a placeholder image is generated.
Before changes
After changes
Example 1
Frame at 0%

brightness=0.0000, variance=0.1439, sharpness=0.0084, total=0.0503
Frame at 25%

brightness=0.0000, variance=0.0114, sharpness=0.0037, total=0.0050
Frame at 50%, Selected as thumbnail

brightness=0.2459, variance=0.2335, sharpness=0.1187, total=0.1974
Frame at 75%

brightness=0.0000, variance=0.1440, sharpness=0.0341, total=0.0588
Example 2
Frame at 0.0%

brightness=1.0000, variance=0.6925, sharpness=0.0568, total=0.5773
Frame at 25.0%

brightness=1.0000, variance=0.4763, sharpness=0.0870, total=0.5159
Frame at 50.0%

brightness=1.0000, variance=0.2753, sharpness=0.0505, total=0.4375
Frame at 75.0%

brightness=1.0000, variance=0.2424, sharpness=0.0042, total=0.4114