@@ -25,6 +25,10 @@ local Thumbnailer = {
2525 -- Extra options for the workers
2626 worker_extra = {},
2727 },
28+ -- Set in register_client
29+ worker_register_timeout = nil ,
30+ -- A timer used to wait for more workers in case we have none
31+ worker_wait_timer = nil ,
2832 workers = {}
2933}
3034
@@ -253,6 +257,8 @@ function Thumbnailer:get_thumbnail_path(time_position)
253257end
254258
255259function Thumbnailer :register_client ()
260+ self .worker_register_timeout = mp .get_time () + 2
261+
256262 mp .register_script_message (" mpv_thumbnail_script-ready" , function (index , path )
257263 self :on_thumb_ready (tonumber (index ), path )
258264 end )
@@ -285,8 +291,8 @@ function Thumbnailer:register_client()
285291 local thumb_script_key = not thumbnailer_options .disable_keybinds and " T" or nil
286292 mp .add_key_binding (thumb_script_key , " generate-thumbnails" , function ()
287293 if self .state .available then
288- self :start_worker_jobs ()
289294 mp .osd_message (" Started thumbnailer jobs" )
295+ self :start_worker_jobs ()
290296 else
291297 mp .osd_message (" Thumbnailing unavailabe" )
292298 end
@@ -353,8 +359,6 @@ function Thumbnailer:prepare_source_path()
353359end
354360
355361function Thumbnailer :start_worker_jobs ()
356- self .state .enabled = true
357-
358362 -- Create directory for the thumbnails, if needed
359363 local l , err = utils .readdir (self .state .thumbnail_directory )
360364 if err then
@@ -372,12 +376,33 @@ function Thumbnailer:start_worker_jobs()
372376
373377 local worker_count = # worker_list
374378
379+ -- In case we have a worker timer created already, clear it
380+ -- (For example, if the video-dec-params change in quick succession or the user pressed T, etc)
381+ if self .worker_wait_timer then
382+ self .worker_wait_timer :stop ()
383+ end
384+
375385 if worker_count == 0 then
376- local err = " No thumbnail workers found. Make sure you are not missing a script!"
377- msg .error (err )
378- mp .osd_message (err , 3 )
386+ local now = mp .get_time ()
387+ if mp .get_time () > self .worker_register_timeout then
388+ -- Workers have had their time to register but we have none!
389+ local err = " No thumbnail workers found. Make sure you are not missing a script!"
390+ msg .error (err )
391+ mp .osd_message (err , 3 )
392+
393+ else
394+ -- We may be too early. Delay the work start a bit to try again.
395+ msg .warn (" No workers found. Waiting a bit more for them." )
396+ -- Wait at least half a second
397+ local wait_time = math.max (self .worker_register_timeout - now , 0.5 )
398+ self .worker_wait_timer = mp .add_timeout (wait_time , function () self :start_worker_jobs () end )
399+ end
379400
380401 else
402+ -- We have at least one worker. This may not be all of them, but they have had
403+ -- their time to register; we've done our best waiting for them.
404+ self .state .enabled = true
405+
381406 msg .debug ( (" Splitting %d thumbnails amongst %d worker(s)" ):format (self .state .thumbnail_count , worker_count ) )
382407
383408 local frame_job_order = self :_create_thumbnail_job_order ()
0 commit comments