如何确保 yt-dlp 在继续 Lua 脚本之前完成所有任务(包括 --recode 选项)?

问题描述 投票:0回答:1

我在 Lua 脚本中使用

yt-dlp 
下载视频,然后将其重新编码为 MP4 格式。这是我的 Lua 代码的简化版本:

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

问题是,一旦

"End"
下载完成,但在
yt-dlp
操作完成之前,就会打印
--recode mp4
。看来
os.execute
不会等待重新编码过程完成,而只会等待初始下载。

如何确保 Lua 脚本等到下载和重新编码操作完全完成后才继续打印

"End"
?任何有关正确同步的建议将不胜感激!

我就这样绑了但是没有带来任何结果

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")

我也在互联网上搜索过类似的问题,但没有得到任何可以帮助的信息。

lua yt-dlp
1个回答
0
投票

目前,我正在尝试使用此解决方案,尽管我不确定这是最好的解决方案。

我每隔 10 秒检查一次文件大小是否发生变化。如果没有,那么我可以认为该过程已完成,因此 LUA 可以继续

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
© www.soinside.com 2019 - 2024. All rights reserved.