Skip to content

Commit ad8eb71

Browse files
committed
Move playlist workaround from worker to manager
Avoid race issues and keep the workers dumb.
1 parent afea932 commit ad8eb71

File tree

2 files changed

+60
-50
lines changed

2 files changed

+60
-50
lines changed

src/thumbnailer_server.lua

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -133,45 +133,10 @@ function do_worker_job(state_json_string, frames_json_string)
133133
end
134134

135135
local file_duration = mp.get_property_native("duration")
136-
local file_path = mp.get_property_native("path")
137-
local extra_worker_options = {}
136+
local file_path = thumb_state.worker_input_path
138137

139138
if thumb_state.is_remote then
140139
thumbnail_func = create_thumbnail_mpv
141-
if thumbnailer_options.remote_direct_stream then
142-
-- Use the direct stream (possibly) provided by ytdl
143-
-- This skips ytdl on the sub-calls, making the thumbnailing faster
144-
-- Works well on YouTube, rest not really tested
145-
file_path = mp.get_property_native("stream-path")
146-
end
147-
end
148-
149-
-- edl:// urls can get LONG. In which case, save the path (URL)
150-
-- to a temporary file and use that instead.
151-
local thumbnail_directory = split_path(thumb_state.thumbnail_template)
152-
local playlist_filename = join_paths(thumbnail_directory, "playlist" .. mp.get_script_name() .. ".txt")
153-
local edl_playlist_used = false
154-
155-
if #file_path > 8000 then
156-
-- Path is too long for a playlist - just pass the original URL to
157-
-- workers and allow ytdl
158-
extra_worker_options.enable_ytdl = true
159-
file_path = mp.get_property_native("path")
160-
msg.warn("Falling back to original URL and ytdl due to LONG source path. This will be slow.")
161-
162-
elseif #file_path > 1024 then
163-
local playlist_file = io.open(playlist_filename, "wb")
164-
if not playlist_file then
165-
msg.error(("Tried to write a playlist to %s but couldn't!"):format(playlist_file))
166-
return
167-
end
168-
169-
playlist_file:write(file_path .. "\n")
170-
playlist_file:close()
171-
172-
file_path = "--playlist=" .. playlist_filename
173-
edl_playlist_used = true
174-
msg.warn("Using playlist workaround due to long source path")
175140
end
176141

177142
local generate_thumbnail_for_index = function(thumbnail_index)
@@ -203,7 +168,7 @@ function do_worker_job(state_json_string, frames_json_string)
203168
end
204169

205170
if need_thumbnail_generation then
206-
local ret = thumbnail_func(file_path, timestamp, thumb_state.thumbnail_size, thumbnail_path, extra_worker_options)
171+
local ret = thumbnail_func(file_path, timestamp, thumb_state.thumbnail_size, thumbnail_path, thumb_state.worker_extra)
207172
local success = check_output(ret, thumbnail_path, thumbnail_func == create_thumbnail_mpv)
208173

209174
if success == nil then
@@ -255,11 +220,6 @@ function do_worker_job(state_json_string, frames_json_string)
255220
thumb_state.thumbnail_size.w,
256221
thumb_state.thumbnail_size.h,
257222
file_path))
258-
259-
-- Remove playlist file if it was used as a workaround
260-
if edl_playlist_used then
261-
os.remove(playlist_filename)
262-
end
263223
end
264224

265225
-- Set up listeners and keybinds

src/thumbnailer_shared.lua

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ local Thumbnailer = {
1919
-- ready: 1
2020
-- in progress: 0
2121
-- not ready: -1
22-
thumbnails = {}
22+
thumbnails = {},
23+
24+
worker_input_path = nil,
25+
-- Extra options for the workers
26+
worker_extra = {},
2327
},
2428
workers = {}
2529
}
@@ -30,6 +34,7 @@ function Thumbnailer:clear_state()
3034
self.state.available = false
3135
self.state.finished_thumbnails = 0
3236
self.state.thumbnails = {}
37+
self.state.worker_extra = {}
3338
end
3439

3540

@@ -72,7 +77,7 @@ function Thumbnailer:update_state()
7277
self.state.thumbnails[i] = -1
7378
end
7479

75-
self.state.thumbnail_template = self:get_thumbnail_template()
80+
self.state.thumbnail_template, self.state.thumbnail_directory = self:get_thumbnail_template()
7681
self.state.thumbnail_size = self:get_thumbnail_size()
7782

7883
self.state.ready = true
@@ -117,8 +122,10 @@ function Thumbnailer:get_thumbnail_template()
117122
end
118123

119124
local file_key = ("%s-%d"):format(filename, filesize)
120-
local file_template = join_paths(self.cache_directory, file_key, "%06d.bgra")
121-
return file_template
125+
126+
local thumbnail_directory = join_paths(self.cache_directory, file_key)
127+
local file_template = join_paths(thumbnail_directory, "%06d.bgra")
128+
return file_template, thumbnail_directory
122129
end
123130

124131

@@ -297,15 +304,58 @@ function Thumbnailer:_create_thumbnail_job_order()
297304
return work_frames
298305
end
299306

307+
function Thumbnailer:prepare_source_path()
308+
local file_path = mp.get_property_native("path")
309+
310+
if self.state.is_remote and thumbnailer_options.remote_direct_stream then
311+
-- Use the direct stream (possibly) provided by ytdl
312+
-- This skips ytdl on the sub-calls, making the thumbnailing faster
313+
-- Works well on YouTube, rest not really tested
314+
file_path = mp.get_property_native("stream-path")
315+
316+
-- edl:// urls can get LONG. In which case, save the path (URL)
317+
-- to a temporary file and use that instead.
318+
local playlist_filename = join_paths(self.state.thumbnail_directory, "playlist.txt")
319+
320+
if #file_path > 8000 then
321+
-- Path is too long for a playlist - just pass the original URL to
322+
-- workers and allow ytdl
323+
self.state.worker_extra.enable_ytdl = true
324+
file_path = mp.get_property_native("path")
325+
msg.warn("Falling back to original URL and ytdl due to LONG source path. This will be slow.")
326+
327+
elseif #file_path > 1024 then
328+
local playlist_file = io.open(playlist_filename, "wb")
329+
if not playlist_file then
330+
msg.error(("Tried to write a playlist to %s but couldn't!"):format(playlist_file))
331+
return false
332+
end
333+
334+
playlist_file:write(file_path .. "\n")
335+
playlist_file:close()
336+
337+
file_path = "--playlist=" .. playlist_filename
338+
msg.warn("Using playlist workaround due to long source path")
339+
end
340+
end
341+
342+
self.state.worker_input_path = file_path
343+
return true
344+
end
345+
300346
function Thumbnailer:start_worker_jobs()
301347
self.state.enabled = true
302348

303349
-- Create directory for the thumbnails, if needed
304-
local thumbnail_directory = split_path(self.state.thumbnail_template)
305-
local l, err = utils.readdir(thumbnail_directory)
350+
local l, err = utils.readdir(self.state.thumbnail_directory)
306351
if err then
307-
msg.info("Creating", thumbnail_directory)
308-
create_directories(thumbnail_directory)
352+
msg.debug("Creating thumbnail directory", self.state.thumbnail_directory)
353+
create_directories(self.state.thumbnail_directory)
354+
end
355+
356+
-- Try to prepare the source path for workers, and bail if unable to do so
357+
if not self:prepare_source_path() then
358+
return
309359
end
310360

311361
local worker_list = {}

0 commit comments

Comments
 (0)