and_cesbo

Astra. Directory to UDP stream

May 1st, 2016
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.40 KB | None | 0 0
  1. -- Directory to UDP stream
  2. --
  3. -- Usage:
  4. -- 1. Install Astra
  5. -- 2. Save script into /etc/astra/file-stream.lua
  6. -- 3. Launch it: astra /etc/astra/file-stream.lua -i /mnt/storage -o udp://127.0.0.1:10000
  7. --
  8.  
  9. input = nil
  10. output = "udp://127.0.0.1:10000"
  11.  
  12. pls = nil
  13. pls_skip = 0
  14.  
  15. channel_data = {}
  16.  
  17. function reload_pls()
  18.     pls = {}
  19.  
  20.     for x in utils.readdir(input.filename) do
  21.         if x:find("%.ts$") then
  22.             table.insert(pls, x)
  23.         end
  24.     end
  25.     if #pls == 0 then
  26.         log.error("Directory is empty")
  27.         astra.exit()
  28.     end
  29.  
  30.     table.sort(pls)
  31.  
  32.     local last_file_name = nil
  33.     local f = io.open(input.filename .. "/.last", "r")
  34.     if f then
  35.         last_file_name = f:read("*all")
  36.         f:close()
  37.     end
  38.     if not last_file_name or utils.stat(input.filename .. "/" .. last_file_name).type ~= "file" then
  39.         last_file_name = pls[1]
  40.     end
  41.  
  42.     for i,x in ipairs(pls) do
  43.         if x == last_file_name then
  44.             pls_skip = i
  45.             break
  46.         end
  47.     end
  48.     if pls_skip == 0 then
  49.         pls_skip = 1
  50.     end
  51. end
  52.  
  53. function play()
  54.     local s = pls[pls_skip]
  55.     local f = io.open(input.filename .. "/.last", "w")
  56.     f:write(s)
  57.     f:close()
  58.  
  59.     channel_data.input = file_input({
  60.         name = s,
  61.         filename = input.filename .. "/" .. s,
  62.         lock = input.filename .. "/.lock",
  63.         callback = function()
  64.             reload_pls()
  65.             pls_skip = pls_skip + 1
  66.             if pls_skip > #pls then
  67.                 pls_skip = 1
  68.             end
  69.             os.remove(input.filename .. "/.lock")
  70.             play()
  71.         end,
  72.     })
  73.     channel_data.tail:set_upstream(channel_data.input:stream())
  74. end
  75.  
  76. options_usage = [[
  77.     -i              path to the source directory
  78.     -o              output address. default: udp://127.0.0.1:10000
  79. ]]
  80.  
  81. options = {
  82.     ["-i"] = function(idx)
  83.         input = "file://" .. argv[idx + 1]
  84.         return 1
  85.     end,
  86.     ["-o"] = function(idx)
  87.         output = argv[idx + 1]
  88.         return 1
  89.     end,
  90. }
  91.  
  92. function main()
  93.     input = parse_url(input)
  94.     if input.filename:find("/$") then
  95.         input.filename = input.filename:sub(1, -2)
  96.     end
  97.     output = parse_url(output)
  98.  
  99.     reload_pls()
  100.  
  101.     channel_data.tail = transmit({})
  102.     output.upstream = channel_data.tail:stream()
  103.     channel_data.output = udp_output(output)
  104.  
  105.     play()
  106. end
Add Comment
Please, Sign In to add comment