Skip to content

Instantly share code, notes, and snippets.

@qruf
Last active September 11, 2016 16:44
Show Gist options
  • Select an option

  • Save qruf/4eadd0e77fbaa4192ddd02c0bc788340 to your computer and use it in GitHub Desktop.

Select an option

Save qruf/4eadd0e77fbaa4192ddd02c0bc788340 to your computer and use it in GitHub Desktop.
dvd->crop->encode DAR calculator
#! /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 round = function(num)
return math.floor(num + 0.5)
end
local rational = function(dec, acc)
acc = acc or 0.0001
local num, den
for i = 1, 1024 do
den, num = i, round(dec * i)
if math.abs(num / den - dec) < acc then
break
end
end
return num, den
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
print(usage)
os.exit(ret or 0)
end
local region = flags.p and "pal" or "ntsc"
local aspect = 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][aspect]
local num, den = rational(dar)
io.write(num, ":", den, "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment