luayt-dlp

How to ensure yt-dlp completes all tasks (including --recode option) before continuing in Lua script?


I am using yt-dlp in a Lua script to download and then recode a video to MP4 format. Here’s a simplified version of my Lua code:

local command = "yt-dlp.exe --recode mp4 <URL>"
os.execute(command)
print("End")

The issue is that "End" gets printed as soon as yt-dlp finishes downloading, but before the --recode mp4 operation completes. It appears that os.execute doesn’t wait for the recoding process to finish, only the initial download.

How can I make sure that the Lua script waits until both the download and recode operations are fully completed before moving on to print "End"? Any suggestions for synchronizing this properly would be much appreciated!

I tied this way but didn't bring any result

var = "yt-dlp.exe --recode mp4 <URL>"
os.execute(var)

-- Wait for the recode process to finish
local recode_done = false
while not recode_done do
    -- Check if there are any processes with "ffmpeg" or other expected recoding process names
    local handle = io.popen("tasklist /FI \"IMAGENAME eq ffmpeg.exe\"")
    local result = handle:read("*a")
    handle:close()

    -- If "ffmpeg.exe" isn't found in the task list, we assume recoding is complete
    if not result:find("ffmpeg.exe") then
        recode_done = true
    end

    -- Wait briefly before checking again
    os.execute("timeout /t 1 >nul")
end

print("End")

I've also searched on the internet for similar problems but I didn't get anything that could help.


Solution

  • For the moment in time, I worked around with this solution although I'm not sure is the best one.

    I check with an interval of10 seconds if the file size is changed. If not, then I can consider the process finished thus LUA can continue

    ...
    ...
    ...
    local Destination = <PATH/FILENAME>
    
    function sleep(n)
      local t0 = clock()
      while clock() - t0 <= n do end
    end
    local zzz = 10
    
    function get_file_size(filename)
        local file = io.open(filename, "rb")
        if not file then return 0 end
        local size = file:seek("end")
        file:close()
        return size
    end
    
    local stable = false
    local last_size = get_file_size(Destination)
    while not stable do
        
        sleep(zzz)
        local new_size = get_file_size(Destination)
        
        if new_size > 0 and new_size == last_size then
            stable = true
        else
            last_size = new_size
        end
    end
    
    ...
    ...
    ...