nonogamer9

CC-CAM (Capture Cards H264 on ComputerCraft / flask)

Aug 1st, 2026 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.81 KB | Source Code | 0 0
  1. -- YOU NEED TO DOWNLOAD THIS SCRIPT TO RUN IT! https://pastebin.com/umb1p8mJ
  2. print("_______ _______       _______ _______ _______")
  3. print("|       |       ___   |       |_____| |  |  |")
  4. print("|_____  |_____      | |_____  |     | |  |  |")
  5. print("")
  6.  
  7. if not http then
  8.     error("HTTP API is disabled! Enable it in your ComputerCraft.cfg")
  9. end
  10.  
  11. print("Select Display Mode:")
  12. print("1. Craft-OS-PC GFX Mode (256 Colors)")
  13. print("2. Text Mode (51x19)")
  14. print("3. Mosaic Blocks (102x57)")
  15. write("Enter choice (1, 2, or 3): ")
  16.  
  17. local choice
  18. while true do
  19.     local event, p1 = os.pullEvent("char")
  20.     if p1 == "1" or p1 == "2" or p1 == "3" then
  21.         choice = tonumber(p1)
  22.         print(p1)
  23.         break
  24.     end
  25. end
  26.  
  27. local serverUrl = "http://localhost:5000/stream?mode=" .. choice
  28.  
  29. print("Connecting to capture card stream...")
  30. local response = http.get(serverUrl, nil, true)
  31. if not response then
  32.     error("Could not connect to the Flask server.")
  33. end
  34.  
  35. local originalMode = term.getGraphicsMode()
  36. local screenWidth, screenHeight = 51, 19
  37. local pixelWidth, pixelHeight, frameSize
  38.  
  39. if choice == 1 then
  40.     term.setGraphicsMode(2)
  41.  
  42.     local cubeSize = 5
  43.     for i = 16, 255 do
  44.         local index = i - 16
  45.         local rIndex = math.floor(index / (cubeSize * cubeSize))
  46.         local gIndex = math.floor((index % (cubeSize * cubeSize)) / cubeSize)
  47.         local bIndex = index % cubeSize
  48.         local r = math.floor(rIndex * 255 / (cubeSize - 1))
  49.         local g = math.floor(gIndex * 255 / (cubeSize - 1))
  50.         local b = math.floor(bIndex * 255 / (cubeSize - 1))
  51.         term.setPaletteColor(i, r / 255, g / 255, b / 255)
  52.     end
  53.  
  54.     pixelWidth = screenWidth * 6
  55.     pixelHeight = screenHeight * 9
  56.     frameSize = pixelWidth * pixelHeight
  57. else
  58.     term.setGraphicsMode(0)
  59.     term.clear()
  60.  
  61.     pixelWidth = screenWidth * 6
  62.     pixelHeight = screenHeight * 9
  63.     frameSize = pixelWidth * pixelHeight
  64. end
  65.  
  66. print("Streaming active. Press 'Q' to quit.")
  67. local running = true
  68.  
  69. parallel.waitForAny(
  70.     function()
  71.         while running do
  72.             local chunk = response.read(frameSize)
  73.             if not chunk or #chunk < frameSize then
  74.                 print("\nStream connection lost or lagging.")
  75.                 break
  76.             end
  77.  
  78.             if choice == 1 then
  79.                 -- FIXED & OPTIMIZED: Build a table of all row strings and push in one go
  80.                 term.setFrozen(true)
  81.                
  82.                 local rows = {}
  83.                 local idx = 1
  84.                 for y = 0, pixelHeight - 1 do
  85.                     rows[y + 1] = chunk:sub(idx, idx + pixelWidth - 1)
  86.                     idx = idx + pixelWidth
  87.                 end
  88.                
  89.                 term.drawPixels(0, 0, rows)
  90.                 term.setFrozen(false)
  91.                 sleep(0.001)
  92.                
  93.             elseif choice == 2 then
  94.                 local fullPixelWidth = screenWidth * 6
  95.                 for ty = 0, screenHeight - 1 do
  96.                     for tx = 0, screenWidth - 1 do
  97.                         local sampleX = tx * 6
  98.                         local sampleY = ty * 9
  99.                         local byteOffset = sampleY * fullPixelWidth + sampleX + 1
  100.                         local color = chunk:byte(byteOffset) or 0
  101.                        
  102.                         term.setCursorPos(tx + 1, ty + 1)
  103.                         term.setBackgroundColor(2 ^ color)
  104.                         term.write(" ")
  105.                     end
  106.                 end
  107.                 sleep(0.001)
  108.             else
  109.                 for ty = 0, screenHeight - 1 do
  110.                     for tx = 0, screenWidth - 1 do
  111.                         local sampleX = tx * 6
  112.                         local sampleY = ty * 9
  113.  
  114.                         local s0 = chunk:byte((sampleY + 1) * pixelWidth + (sampleX + 1) + 1) or 0
  115.                         local s1 = chunk:byte((sampleY + 1) * pixelWidth + (sampleX + 4) + 1) or 0
  116.                         local s2 = chunk:byte((sampleY + 4) * pixelWidth + (sampleX + 1) + 1) or 0
  117.                         local s3 = chunk:byte((sampleY + 4) * pixelWidth + (sampleX + 4) + 1) or 0
  118.                         local s4 = chunk:byte((sampleY + 7) * pixelWidth + (sampleX + 1) + 1) or 0
  119.                         local s5 = chunk:byte((sampleY + 7) * pixelWidth + (sampleX + 4) + 1) or 0
  120.  
  121.                         local counts = {}
  122.                         local function tally(c)
  123.                             counts[c] = (counts[c] or 0) + 1
  124.                         end
  125.                         tally(s0); tally(s1); tally(s2); tally(s3); tally(s4); tally(s5)
  126.  
  127.                         local fgColor, maxCount = 0, -1
  128.                         local bgColor = 0
  129.                         local secondMax = -1
  130.  
  131.                         for col, cnt in pairs(counts) do
  132.                             if cnt > maxCount then
  133.                                 secondMax = maxCount
  134.                                 bgColor = fgColor
  135.                                 maxCount = cnt
  136.                                 fgColor = col
  137.                             elseif cnt > secondMax then
  138.                                 secondMax = cnt
  139.                                 bgColor = col
  140.                             end
  141.                         end
  142.  
  143.                         if secondMax == -1 then
  144.                             bgColor = fgColor
  145.                         end
  146.  
  147.                         local bitmask = 0
  148.                         if s0 == fgColor then bitmask = bitmask + 1 end
  149.                         if s1 == fgColor then bitmask = bitmask + 2 end
  150.                         if s2 == fgColor then bitmask = bitmask + 4 end
  151.                         if s3 == fgColor then bitmask = bitmask + 8 end
  152.                         if s4 == fgColor then bitmask = bitmask + 16 end
  153.                         if s5 == fgColor then bitmask = bitmask + 32 end
  154.  
  155.                         local charCode = 128 + math.floor(bitmask / 2)
  156.  
  157.                         term.setCursorPos(tx + 1, ty + 1)
  158.                         term.setTextColor(2 ^ fgColor)
  159.                         term.setBackgroundColor(2 ^ bgColor)
  160.                        
  161.                         local success, err = pcall(function()
  162.                             term.write(string.char(charCode))
  163.                         end)
  164.                         if not success then
  165.                             term.write(" ")
  166.                         end
  167.                     end
  168.                 end
  169.                 sleep(0.001)
  170.             end
  171.         end
  172.     end,
  173.     function()
  174.         while running do
  175.             local event, p1 = os.pullEvent("key")
  176.             if event == "key" and p1 == keys.q then
  177.                 running = false
  178.             end
  179.         end
  180.     end
  181. )
  182.  
  183. response.close()
  184. term.setGraphicsMode(originalMode)
  185. term.clear()
  186. print("Stream closed successfully.")
Add Comment
Please, Sign In to add comment