and_cesbo icon

Astra. Directory to UDP stream

and_cesbo | PRO | 05/01/16 09:07:49 AM UTC | 0 ⭐ | 731 👁️ | Never ⏰ | []
Lua |

2.4 KB

|

None

|

0 👍

/

0 👎

-- Directory to UDP stream
--
-- Usage:
-- 1. Install Astra
-- 2. Save script into /etc/astra/file-stream.lua
-- 3. Launch it: astra /etc/astra/file-stream.lua -i /mnt/storage -o udp://127.0.0.1:10000
--
 
input = nil
output = "udp://127.0.0.1:10000"
 
pls = nil
pls_skip = 0
 
channel_data = {}
 
function reload_pls()
    pls = {}
 
    for x in utils.readdir(input.filename) do
        if x:find("%.ts$") then
            table.insert(pls, x)
        end
    end
    if #pls == 0 then
        log.error("Directory is empty")
        astra.exit()
    end
 
    table.sort(pls)
 
    local last_file_name = nil
    local f = io.open(input.filename .. "/.last", "r")
    if f then
        last_file_name = f:read("*all")
        f:close()
    end
    if not last_file_name or utils.stat(input.filename .. "/" .. last_file_name).type ~= "file" then
        last_file_name = pls[1]
    end
 
    for i,x in ipairs(pls) do
        if x == last_file_name then
            pls_skip = i
            break
        end
    end
    if pls_skip == 0 then
        pls_skip = 1
    end
end
 
function play()
    local s = pls[pls_skip]
    local f = io.open(input.filename .. "/.last", "w")
    f:write(s)
    f:close()
 
    channel_data.input = file_input({
        name = s,
        filename = input.filename .. "/" .. s,
        lock = input.filename .. "/.lock",
        callback = function()
            reload_pls()
            pls_skip = pls_skip + 1
            if pls_skip > #pls then
                pls_skip = 1
            end
            os.remove(input.filename .. "/.lock")
            play()
        end,
    })
    channel_data.tail:set_upstream(channel_data.input:stream())
end
 
options_usage = [[
    -i              path to the source directory
    -o              output address. default: udp://127.0.0.1:10000
]]
 
options = {
    ["-i"] = function(idx)
        input = "file://" .. argv[idx + 1]
        return 1
    end,
    ["-o"] = function(idx)
        output = argv[idx + 1]
        return 1
    end,
}
 
function main()
    input = parse_url(input)
    if input.filename:find("/$") then
        input.filename = input.filename:sub(1, -2)
    end
    output = parse_url(output)
 
    reload_pls()
 
    channel_data.tail = transmit({})
    output.upstream = channel_data.tail:stream()
    channel_data.output = udp_output(output)
 
    play()
end

Comments