@@ -82,35 +82,17 @@ function check_output(ret, output_path)
8282end
8383
8484
85- function generate_thumbnails (from_keypress )
86- msg .debug (" Thumbnailer state:" )
87- msg .debug (utils .to_string (Thumbnailer .state ))
88-
89- if not Thumbnailer .state .available then
90- if from_keypress then
91- mp .osd_message (" Nothing to thumbnail" , 2 )
92- end
93- if Thumbnailer .state .is_remote then
94- msg .warn (" Not thumbnailing remote file" )
95- end
85+ function do_worker_job (state_json_string , frames_json_string )
86+ local thumb_state , err = utils .parse_json (state_json_string )
87+ if err then
88+ msg .error (" Failed to parse state JSON" )
9689 return
9790 end
9891
99- local thumbnail_count = Thumbnailer .state .thumbnail_count
100- local thumbnail_delta = Thumbnailer .state .thumbnail_delta
101- local thumbnail_size = Thumbnailer .state .thumbnail_size
102- local file_template = Thumbnailer .state .thumbnail_template
103- local file_duration = mp .get_property_native (" duration" )
104- local file_path = mp .get_property_native (" path" )
105-
106- msg .info ((" Generating %d thumbnails @ %dx%d" ):format (thumbnail_count , thumbnail_size .w , thumbnail_size .h ))
107-
108- -- Create directory for the thumbnails
109- local thumbnail_directory = split_path (file_template )
110- local l , err = utils .readdir (thumbnail_directory )
92+ local thumbnail_indexes , err = utils .parse_json (frames_json_string )
11193 if err then
112- msg .info ( " Creating " , thumbnail_directory )
113- create_directories ( thumbnail_directory )
94+ msg .error ( " Failed to parse thumbnail frame indexes " )
95+ return
11496 end
11597
11698 local thumbnail_func = create_thumbnail_mpv
@@ -122,7 +104,10 @@ function generate_thumbnails(from_keypress)
122104 end
123105 end
124106
125- if Thumbnailer .state .is_remote then
107+ local file_duration = mp .get_property_native (" duration" )
108+ local file_path = mp .get_property_native (" path" )
109+
110+ if thumb_state .is_remote then
126111 thumbnail_func = create_thumbnail_mpv
127112 if thumbnailer_options .remote_direct_stream then
128113 -- Use the direct stream (possibly) provided by ytdl
@@ -132,14 +117,16 @@ function generate_thumbnails(from_keypress)
132117 end
133118 end
134119
135- mp . commandv ( " script-message " , " mpv_thumbnail_script-enabled " )
120+ msg . debug (( " Generating %d thumbnails @ %dx%d " ): format ( # thumbnail_indexes , thumb_state . thumbnail_size . w , thumb_state . thumbnail_size . h ) )
136121
137122 local generate_thumbnail_for_index = function (thumbnail_index )
123+ local thumbnail_path = thumb_state .thumbnail_template :format (thumbnail_index )
124+ local timestamp = math.min (file_duration , thumbnail_index * thumb_state .thumbnail_delta )
138125
139126 mp .commandv (" script-message" , " mpv_thumbnail_script-progress" , tostring (thumbnail_index ))
140127
141128 -- The expected size (raw BGRA image)
142- local thumbnail_raw_size = (thumbnail_size .w * thumbnail_size .h * 4 )
129+ local thumbnail_raw_size = (thumb_state . thumbnail_size .w * thumb_state . thumbnail_size .h * 4 )
143130
144131 local need_thumbnail_generation = false
145132
@@ -158,7 +145,7 @@ function generate_thumbnails(from_keypress)
158145 end
159146
160147 if need_thumbnail_generation then
161- local ret = thumbnail_func (file_path , timestamp , thumbnail_size , thumbnail_path )
148+ local ret = thumbnail_func (file_path , timestamp , thumb_state . thumbnail_size , thumbnail_path )
162149 local success = check_output (ret , thumbnail_path )
163150
164151 if success == nil then
@@ -200,33 +187,35 @@ function generate_thumbnails(from_keypress)
200187 mp .commandv (" script-message" , " mpv_thumbnail_script-ready" , tostring (thumbnail_index ), thumbnail_path )
201188 end
202189
203- -- Keep track of which thumbnails we've checked during the passes (instead of proper math for no-overlap)
204- local generated_thumbnails = {}
205-
206- -- Do several passes over the thumbnails with increasing frequency
207- for res = 6 , 0 , - 1 do
208- local nth = (2 ^ res )
209-
210- for thumbnail_index = 0 , thumbnail_count - 1 , nth do
211- if not generated_thumbnails [thumbnail_index ] then
212- local bail = generate_thumbnail_for_index (thumbnail_index )
213- if bail then return end
214- generated_thumbnails [thumbnail_index ] = true
215- end
216- end
190+ for i , thumbnail_index in ipairs (thumbnail_indexes ) do
191+ local bail = generate_thumbnail_for_index (thumbnail_index )
192+ if bail then return end
217193 end
218194end
219195
196+ -- Set up listeners and keybinds
220197
221- function on_script_keypress ()
222- mp .osd_message (" Starting thumbnail generation" , 2 )
223- generate_thumbnails (true )
224- mp .osd_message (" All thumbnails generated" , 2 )
225- end
198+ -- Job listener
199+ mp .register_script_message (" mpv_thumbnail_script-job" , do_worker_job )
226200
227- -- Set up listeners and keybinds
228201
229- mp .register_script_message (" mpv_thumbnail_script-generate" , generate_thumbnails )
202+ -- Register this worker with the master script
203+ local register_timer = nil
204+ local register_timeout = mp .get_time () + 3
205+
206+ local register_function = function ()
207+ if mp .get_time () > register_timeout and register_timer then
208+ msg .error (" Thumbnail worker registering timed out" )
209+ register_timer :stop ()
210+ else
211+ msg .debug (" Announcing self to master..." )
212+ mp .commandv (" script-message" , " mpv_thumbnail_script-worker" , mp .get_script_name ())
213+ end
214+ end
215+
216+ register_timer = mp .add_periodic_timer (0.1 , register_function )
230217
231- local thumb_script_key = not thumbnailer_options .disable_keybinds and " T" or nil
232- mp .add_key_binding (thumb_script_key , " generate-thumbnails" , on_script_keypress )
218+ mp .register_script_message (" mpv_thumbnail_script-slaved" , function ()
219+ msg .debug (" Successfully registered with master" )
220+ register_timer :stop ()
221+ end )
0 commit comments