Last active
June 13, 2018 16:40
-
-
Save jatkins/46f75e3e9b54dc4fcda80ee35b8a6054 to your computer and use it in GitHub Desktop.
UBNT Camera Grab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'uri' | |
require 'json' | |
require 'fileutils' | |
path = "/home/cam/" # Location of files | |
prefix = "cam1" # File name prefix | |
############################################################################################################ | |
year = Time.now.strftime("%Y") | |
month = Time.now.strftime("%m") | |
day = Time.now.strftime("%d") | |
hour = (Time.now - (60 * 60)).strftime("%H") | |
Dir.chdir "#{path}/#{year}/#{month}/#{day}/#{hour}" | |
files = Dir.glob("*.jpeg") | |
# Using a bash call was quicker for the need, should be updated to ruby API one day | |
`ffmpeg -framerate 15 -pattern_type glob -i '*.jpeg' -c:v libx264 -preset veryfast -r 15 /#{path}/#{year}/#{month}/#{day}/#{prefix}_#{year}#{month}#{day}-#{hour}.mp4 > /dev/null` | |
for file in files do | |
FileUtils.rm(file) | |
end | |
FileUtils.rmdir("#{path}/#{year}/#{month}/#{day}/#{hour}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "camera1", | |
"username": "something", | |
"password": "string", | |
"address": "1.1.1.1", | |
"interval" : 2 | |
}, | |
{ | |
"name": "camera2", | |
"username": "something", | |
"password": "string", | |
"address": "1.1.1.2", | |
"interval": 2 | |
}, | |
{ | |
"name": "camera3", | |
"username": "something", | |
"password": "string", | |
"address": "1.1.1.3", | |
"interval": 2 | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'fileutils' | |
require 'net/http' | |
require 'uri' | |
def create_directory(path="/home/recordings", camera) | |
FileUtils.mkdir_p(path) unless File.exists?(path) && File.directory?(path) | |
FileUtils.mkdir_p("#{path}/#{camera}") unless File.exists?("#{path}/#{camera}") && File.directory?("#{path}/#{camera}") | |
end | |
def save_image(time=Time.now, http_response, camera_name, path) | |
save_name = "#{camera_name}_#{time.strftime("%H%M%S")}.jpeg" | |
time_path = "#{time.strftime("%Y")}/#{time.strftime("%m")}/#{time.strftime("%d")}/#{time.strftime("%H")}" | |
save_path = "#{path}/#{camera_name}/#{time_path}" | |
FileUtils.mkdir_p(save_path) unless File.exists?(save_path) && File.directory?(save_path) | |
output = File.open("#{save_path}/#{save_name}", 'wb') | |
output.write(http_response.body) | |
output.close | |
end | |
def capture(address, username="ubnt", password="ubnt") | |
uri = URI.parse("http://#{address}/api/1.1/snapshot") | |
request = Net::HTTP::Post.new(uri) | |
request.content_type = "application/json" | |
request.body = JSON.dump({ | |
"username" => username, | |
"password" => password | |
}) | |
req_options = { | |
use_ssl: uri.scheme == "https", | |
} | |
begin | |
responce = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| | |
http.request(request) | |
end | |
rescue StandardError | |
false | |
end | |
return responce | |
end | |
def run(path="/home/recordings", camera) | |
loop do | |
time = Time.now | |
create_directory(path, camera["name"]) | |
http = capture(camera["address"], camera["username"], camera["password"]) | |
save_image(time, http, camera["name"], path) | |
sleep camera["interval"] | |
end | |
end | |
file = File.read("config.json") | |
cameras = JSON.parse(file) | |
threads = [] | |
cameras.each do |camera| | |
threads << Thread.new { run("./test", camera) } | |
end | |
loop do | |
sleep(1) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment