Skip to content

Instantly share code, notes, and snippets.

@jasonbradley
jasonbradley / dont_run_on_top.sh
Created July 7, 2012 21:47
Check for PHP script already running
#!/bin/sh
if ps -ef | grep -v grep | grep my_script.php ; then
exit 0
else
php /var/www/my_script.php
exit 0
fi
@jasonbradley
jasonbradley / convert_to_h264.php
Created July 7, 2012 21:51
Convert video to h264
function convertVideoToH264($original_file_path, $converted_file)
{
//convert it
$cmd = "ffmpeg -i ".$original_file_path." -crf 20 -bufsize 20000k -maxrate 25000k -vcodec libx264 $converted_file";
shell_exec($cmd);
if (!is_file($converted_file))
{
echo "FFMPEG conversion failed!, Skipping...";
}
@jasonbradley
jasonbradley / gist:3069127
Created July 8, 2012 03:03
Get video file meta using ffmpeg
function getMeta($original_file_path)
{
ob_start();
passthru("ffmpeg -i ". $original_file_path . " 2>&1");
$meta_data = ob_get_contents();
ob_end_clean();
return $meta_data;
}
@jasonbradley
jasonbradley / util.lua
Created December 15, 2012 05:22
PHP print_r for Lua
Util.print_r = function(t, indent, done)
done = done or {}
indent = indent or ''
local nextIndent -- Storage for next indentation value
for key, value in pairs (t) do
if type (value) == "table" and not done [value] then
nextIndent = nextIndent or
(indent .. string.rep(' ',string.len(tostring (key))+2))
-- Shortcut conditional allocation
done [value] = true
@jasonbradley
jasonbradley / util.lua
Created December 15, 2012 05:23
PHP trim for Lua
Util.trim = function(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
@jasonbradley
jasonbradley / util.lua
Created December 15, 2012 05:24
Round function in Lua
Util.round = function(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
@jasonbradley
jasonbradley / util.lua
Created December 15, 2012 05:25
Format a number in Lua
Util.format_num = function(amount, decimal, prefix, neg_prefix)
local str_amount, formatted, famount, remain
decimal = decimal or 2 -- default 2 decimal places
neg_prefix = neg_prefix or "-" -- default negative sign
famount = math.abs(Util.round(amount,decimal))
famount = math.floor(famount)
remain = Util.round(math.abs(amount) - famount, decimal)
@jasonbradley
jasonbradley / jsonStorage.lua
Created December 15, 2012 05:28
Lua read/write data in JSON format
-- load the JSON library.
local Json = require("json")
local JsonStorage = {}
-- Function to save a table.  Since game settings need to be saved from session to session, we will
-- use the Documents Directory
JsonStorage.saveTable = function(t, filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local file = io.open(path, "w")
@jasonbradley
jasonbradley / gist:4357406
Created December 22, 2012 03:47
Lua convert HEX to RGB
function Util:hex2rgb(hex)
hex = hex:gsub("#","")
return tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6))
end
local widget = require "widget"
local scroller = widget.newScrollView{
width = 320,
height = 480,
scrollWidth = 700,
scrollHeight = 1000
}
-- event listener for button widget
local function onButtonEvent( event )