occivink / mpv-gallery-view

Gallery-view scripts for mpv
The Unlicense
195 stars 21 forks source link

[Request] How to batch generate thumbnails from command line? #18

Closed snowman closed 4 years ago

snowman commented 4 years ago

I have lots of videos and have to spending most of the time to manually generate them, and what you can do is sitting there and wait around, and the worst thing is when you have 1000 videos to generate thumbnails!! :(

occivink commented 4 years ago

I actually used to have this in the README, but removed it since I figured it might not be too useful. https://github.com/occivink/mpv-gallery-view/tree/96855e06091339ccea0fe7488eb2e78f7f58fc31#thumbnail-generation

snowman commented 3 years ago

Why needs container-fps property?

https://github.com/occivink/mpv-gallery-view/blob/92f251d2fec6ce2bb839d5c14c327ca9989212a8/scripts/contact-sheet.lua#L349

Why needing subtract duration with 1 / mp.get_property_number("container-fps", 30)?

The mpv container-fps documentation:

  container-fps
         Container FPS.

         This can easily contain bogus values.

         For videos that use modern container formats or video codecs,
         this will often be incorrect.

         (Renamed from fps.)

I can NOT get the same container-fps value in mpv as fps value in ffprobe:

## Get fps value with ffprobe
$ ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate foo.mp4
60/1

and in mpv, with mp.get_property_number("container-fps", 30) returns 59.999881744385

Question

Should we remove this line instead?

occivink commented 3 years ago

I don't remember exactly, but I think it's because the thumbnail was off by one frame. You can try removing that line and see if everything works as expected, but more importantly: what is the problem with it?

snowman commented 3 years ago

This issue is asking "How to batch generate thumbnails from the command line", and I want to generate contact-sheet (not playlist-view, which has only single thumbnail) of video.

And I take a look at the source code of how to get the contact-sheet thumbnail filenames.

Video file has multiple thumbnails at the different position, here is example code for illustration only:

duration = mp.get_property_number("duration")
duration = duration - (1 / mp.get_property_number("container-fps", 30))
opts.time_distance = "1%"
effective_time_distance = tonumber(string.sub(opts.time_distance, 1, -2)) / 100 * duration

while time < duration do
    times[#times + 1] = time
    time = time + effective_time_distance
end

gallery.items = times

To get the same duration from the command line, run

$ ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 foo.mp4

But I can't get the exact fps value same as container-fps from the command line, so can't get the exact same thumbnail filenames generated by mpv script contact-sheet.lua.

One possible solution to automate the generation of contact-sheet of video is to write mpv script to write the ffmpeg commands to shell script file, then execute them to generate thumbnails.

occivink commented 3 years ago

To be honest I'm not sure why this is the case, 1/fps is around 30milliseconds, so I don't see why such precision would matter here. Still, you can try removing it in the script and see if everything works. If it does let me know, but I don't think I added this specific piece of code without a reason (even if I don't remember it now).