Skip to content

Instantly share code, notes, and snippets.

@meteozond
Last active May 22, 2023 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meteozond/b2c07c850fdcc7f41ec065756ccfb2e0 to your computer and use it in GitHub Desktop.
Save meteozond/b2c07c850fdcc7f41ec065756ccfb2e0 to your computer and use it in GitHub Desktop.
Getting snapshot from motion with nginx openresty content_by_lua
location /snapshot {
content_by_lua '
local host = "127.0.0.1"
local port = 8001
local sock = ngx.socket.tcp()
local ok, err = sock:connect(host, port)
bytes, err = sock:send("GET /\\r\\nHost: "..host..":"..port.."\\r\\n\\r\\n")
if not ok then
ngx.say("Request error ", err)
return
end
ngx.header.content_type = "text/html"
if not ok then
ngx.say("Connection error ", err)
return
end
local data = ""
while true do
local chunk, err = sock:receive(256)
if err then
ngx.say("Reading error ", string.len(data))
return
end
data = data .. chunk
local match = string.match(data, "\\r\\n[-][-]BoundaryString\\r\\n.-\\r\\n\\r\\n(.-)\\r\\n[-][-]BoundaryString\\r\\n")
if match then
ngx.header.content_type = "image/jpeg"
ngx.say(match)
return
end
end
';
}
@imbalind
Copy link

I know it's been 2 years since you published this, but I'm having issues running this. I get "Reading error 0", then when I changed line 21 to log the err variable I get "Reading error closed". I'm not familiar with nginx and Lua but if there's any quick thing I can test please let me know.

@meteozond
Copy link
Author

Hi @imbalind, sorry for delay, I've completely missed your message.
string.len(data) == 0 means that script haven't received any data. Looks like it is problem with your Motion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment