@@ -25,6 +25,10 @@ local Thumbnailer = {
25
25
-- Extra options for the workers
26
26
worker_extra = {},
27
27
},
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 ,
28
32
workers = {}
29
33
}
30
34
@@ -253,6 +257,8 @@ function Thumbnailer:get_thumbnail_path(time_position)
253
257
end
254
258
255
259
function Thumbnailer :register_client ()
260
+ self .worker_register_timeout = mp .get_time () + 2
261
+
256
262
mp .register_script_message (" mpv_thumbnail_script-ready" , function (index , path )
257
263
self :on_thumb_ready (tonumber (index ), path )
258
264
end )
@@ -285,8 +291,8 @@ function Thumbnailer:register_client()
285
291
local thumb_script_key = not thumbnailer_options .disable_keybinds and " T" or nil
286
292
mp .add_key_binding (thumb_script_key , " generate-thumbnails" , function ()
287
293
if self .state .available then
288
- self :start_worker_jobs ()
289
294
mp .osd_message (" Started thumbnailer jobs" )
295
+ self :start_worker_jobs ()
290
296
else
291
297
mp .osd_message (" Thumbnailing unavailabe" )
292
298
end
@@ -353,8 +359,6 @@ function Thumbnailer:prepare_source_path()
353
359
end
354
360
355
361
function Thumbnailer :start_worker_jobs ()
356
- self .state .enabled = true
357
-
358
362
-- Create directory for the thumbnails, if needed
359
363
local l , err = utils .readdir (self .state .thumbnail_directory )
360
364
if err then
@@ -372,12 +376,33 @@ function Thumbnailer:start_worker_jobs()
372
376
373
377
local worker_count = # worker_list
374
378
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
+
375
385
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
379
400
380
401
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
+
381
406
msg .debug ( (" Splitting %d thumbnails amongst %d worker(s)" ):format (self .state .thumbnail_count , worker_count ) )
382
407
383
408
local frame_job_order = self :_create_thumbnail_job_order ()
0 commit comments