Advertisement
Guest User

Youtube_Viewer.lua

a guest
Feb 23rd, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.26 KB | None | 0 0
  1. -- 00 00 - width
  2. -- 00 00 - height
  3.  
  4. -- 00 00 00 - palette 0
  5. -- 00 00 00 - palette 1
  6. -- 00 00 00 - palette 2
  7. -- 00 00 00 - palette 3
  8. -- 00 00 00 - palette 4
  9. -- 00 00 00 - palette 5
  10. -- 00 00 00 - palette 6
  11. -- 00 00 00 - palette 7
  12. -- 00 00 00 - palette 8
  13. -- 00 00 00 - palette 9
  14. -- 00 00 00 - palette 10
  15. -- 00 00 00 - palette 11
  16. -- 00 00 00 - palette 12
  17. -- 00 00 00 - palette 13
  18. -- 00 00 00 - palette 14
  19. -- 00 00 00 - palette 15
  20.  
  21. -- 00 - PIXELS width*height
  22.  
  23. -- JSON METADATA
  24.  
  25. args = {...}
  26. if #args == 0 then
  27.   printError("No url passed")
  28.   return
  29. end
  30.  
  31. mon = peripheral.find("monitor")
  32. tape_drive = peripheral.find("tape_drive")
  33. skip_audio = true
  34.  
  35. if tape_drive then
  36.   print("Continuing with audio")
  37.   skip_audio = false
  38. end
  39.  
  40. function epoch()
  41.   return os.epoch() / 100000
  42. end
  43.  
  44. local function _StringIOwrite(self, _v)
  45.   if #self.buffer > 0 then
  46.     _lb = self.buffer[#self.buffer]
  47.     _a = _v:sub(0, self.bufferlength - #_lb)
  48.     _lb = _lb .. _a
  49.     _v = _v:sub(#_a + 1, -1)
  50.     self.buffer[#self.buffer] = _lb
  51.   end
  52.   while #_v ~= 0 do
  53.     _lb = _v:sub(0, self.bufferlength)
  54.     _v = _v:sub(self.bufferlength + 1, -1)
  55.     table.insert(self.buffer, _lb)
  56.   end
  57.   self.size = self.size + #_v
  58.   return #_v
  59. end
  60.  
  61. local function _StringIOread(self, _c)
  62.   _c = _c or 1
  63.   local _r = ""
  64.   while _c > 0 do
  65.     if #self.buffer == 0 then
  66.       break
  67.     end
  68.     local _t = math.min(_c, #self.buffer[1])
  69.     _c = _c - _t
  70.     _r = _r .. self.buffer[1]:sub(1, _t)
  71.     self.buffer[1] = self.buffer[1]:sub(_t + 1, -1)
  72.     if #self.buffer[1] == 0 then
  73.       table.remove(self.buffer, 1)
  74.     end
  75.   end
  76.   return _r
  77. end
  78.  
  79. local function _StringIObread(self, _c)
  80.   _c = _c or 1
  81.   local _r = ""
  82.   while _c > 0 do
  83.     if #self.buffer == 0 then
  84.       break
  85.     end
  86.     local _t = math.min(_c, #self.buffer[1])
  87.     _c = _c - _t
  88.     _r = _r .. self.buffer[1]:sub(1, _t)
  89.     self.buffer[1] = self.buffer[1]:sub(_t + 1, -1)
  90.     if #self.buffer[1] == 0 then
  91.       table.remove(self.buffer, 1)
  92.     end
  93.   end
  94.   function splitbytes(str)
  95.     if #str > 0 then
  96.       return string.byte(str:sub(1,1)), splitbytes(str:sub(2))
  97.     end
  98.   end
  99.   return splitbytes(_r)
  100. end
  101.  
  102. function StringIO(_buffer)
  103.   _table = {
  104.     ['write'] = _StringIOwrite,
  105.     ['read'] = _StringIOread,
  106.     ['bread'] = _StringIObread,
  107.     ['bufferlength'] = 1024,
  108.     ['buffer'] = {},
  109.     ['index'] = 0,
  110.     ['size'] = 0
  111.   }
  112.   if _buffer then
  113.     _table:write(_buffer)
  114.   end
  115.   return _table
  116. end
  117.  
  118. function parseCCI(sio)
  119.  
  120.   width = bit.blshift(sio:bread(), 8) + sio:bread()
  121.   height = bit.blshift(sio:bread(), 8) + sio:bread()
  122.  
  123.   palette = {}
  124.   for i=0, 15 do
  125.       palette[2^i] = {
  126.           sio:bread()/255,
  127.           sio:bread()/255,
  128.           sio:bread()/255
  129.       }
  130.   end
  131.  
  132.   _buffer = StringIO()
  133.   _buffer.bufferlength = width
  134.  
  135.   _hex = {'00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '0a', '0b', '0c', '0d', '0e', '0f', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '1a', '1b', '1c', '1d', '1e', '1f', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '2a', '2b', '2c', '2d', '2e', '2f', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '3a', '3b', '3c', '3d', '3e', '3f', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '4a', '4b', '4c', '4d', '4e', '4f', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '5a', '5b', '5c', '5d', '5e', '5f', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '6a', '6b', '6c', '6d', '6e', '6f', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '7a', '7b', '7c', '7d', '7e', '7f', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '8a', '8b', '8c', '8d', '8e', '8f', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '9a', '9b', '9c', '9d', '9e', '9f', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'ba', 'bb', 'bc', 'bd', 'be', 'bf', 'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'ca', 'cb', 'cc', 'cd', 'ce', 'cf', 'd0', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'da', 'db', 'dc', 'dd', 'de', 'df', 'e0', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'e9', 'ea', 'eb', 'ec', 'ed', 'ee', 'ef', 'f0', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'fa', 'fb', 'fc', 'fd', 'fe', 'ff'}
  136.  
  137.   pxl = 0
  138.   for _=1, width*height do
  139.     pxl = pxl + 1
  140.     _v = sio:bread()
  141.     if _v then
  142.       -- _val = string.format("%x", _v)
  143.       _val = _hex[_v + 1]
  144.       if #_val == 1 then
  145.         _val = "0" .. _val
  146.       end
  147.       _buffer:write(_val)
  148.     else
  149.       break
  150.     end
  151.  
  152.     if pxl % 50000 == 0 then
  153.       sleep(0)
  154.     end
  155.   end
  156.  
  157.   blitmap = _buffer.buffer
  158.  
  159.   for i, k in pairs(palette) do
  160.     mon.setPaletteColour(i, k[1], k[2], k[3])
  161.   end
  162.  
  163.   for y, blitline in pairs(blitmap) do
  164.       mon.setCursorPos(1, y)
  165.       mon.blit((" "):rep(#blitline), blitline, blitline)
  166.   end
  167. end
  168.  
  169. url = "ws://63.141.246.170:15065/playback"
  170. w, h = mon.getSize()
  171. pt, pa = 0, 0
  172. ft, fa = 0, 0
  173.  
  174. print("Connecting")
  175. ws, error = http.websocket(url)
  176. print("Connected")
  177.  
  178. payload = {
  179.   ['url'] = args[1],
  180.   ['skip-audio'] = skip_audio,
  181.   ['width'] = w,
  182.   ['height'] = h
  183. }
  184.  
  185. _payload = json.encode(payload)
  186. print("Sent payload: " .. _payload)
  187. ws.send(_payload)
  188.  
  189. print("Waiting for audio")
  190. audioresp = StringIO()
  191. while true do
  192.   resp = ws.receive()
  193.   if resp == "aeof" then
  194.     print("Received eof")
  195.     break
  196.   end
  197.   audioresp:write(resp)
  198.   ws.send("achk")
  199. end
  200.  
  201. if skip_audio == false then
  202.   print("Seeking audio and writing")
  203.   tape_drive.stop()
  204.   tape_drive.seek(-tape_drive.getSize())
  205.   while true do
  206.     aresp = audioresp:read(audioresp.bufferlength*100)
  207.     if aresp == "" then
  208.       break
  209.     end
  210.     print(#aresp)
  211.     tape_drive.write(aresp)
  212.     sleep(0)
  213.   end
  214.   print("Finished dumping audio")
  215.   tape_drive.seek(-tape_drive.getSize())
  216. end
  217.  
  218. print("Waiting for video response")
  219.  
  220. if skip_audio == false then
  221.   print("Starting tape drive")
  222.   tape_drive.play()
  223.   -- sleep(1)
  224. end
  225.  
  226. ws.send("vst")
  227.  
  228. while true do
  229.   resp = ws.receive()
  230.   ws.send("vfr")
  231.   if resp == "eof" then
  232.     break
  233.   end
  234.   start = epoch()
  235.   parseCCI(StringIO(resp))
  236. end
  237.  
  238. if skip_audio == false then
  239.   tape_drive.stop()
  240. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement