Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env lua
- local usage = [[
- dvdaspect.lua [-pw] width height
- returns the correct display aspect ratio of a dvd after cropping
- options:
- -p input is pal
- -w input is widescreen
- ]]
- local die = function(ret)
- print(usage)
- os.exit(ret or 0)
- end
- local rational = function(decimal, acc)
- acc = acc or 1E-3
- local sign, num, denum
- local sign = (decimal < 0) and -1 or 1
- decimal = math.abs(decimal)
- if decimal == math.floor(decimal) then
- num = decimal * sign
- denum = 1
- return num, denum
- end
- if decimal < 1E-19 then
- num = sign
- denum = 9999999999999999999
- elseif decimal > 1E+19 then
- num = 9999999999999999999 * sign
- denum = 1
- end
- local z = decimal
- local predenum = 0
- local sc
- denum = 1
- repeat
- z = 1 / (z - math.floor(z))
- sc = denum
- denum = denum * math.floor(z) + predenum
- predenum = sc
- num = math.floor(decimal * denum)
- until ((math.abs(decimal - (num / denum)) < acc) or (z == math.floor(z)))
- num = sign * num
- return num, denum
- end
- local flags, args = {}, {}
- for i, v in ipairs(arg) do
- if v:match("^-") then
- for f in v:gmatch("%w") do
- flags[f] = true
- end
- else
- args[#args+1] = v
- end
- end
- local w, h = tonumber(args[1]), tonumber(args[2])
- if not w or not h then
- die(1)
- end
- local region = flags.p and "pal" or "ntsc"
- local screen = flags.w and "wide" or "full"
- local pars = {
- ntsc = { wide = 40/33, full = 10/11 },
- pal = { wide = 16/11, full = 12/11 }
- }
- local dar = (w / h) * pars[region][screen]
- local num, den = rational(dar)
- io.write(num, ":", den, "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement