Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #! /usr/bin/env lua
  2.  
  3. local usage = [[
  4. dvdaspect.lua [-pw] width height
  5. returns the correct display aspect ratio of a dvd after cropping
  6.  
  7. options:
  8. -p input is pal
  9. -w input is widescreen
  10. ]]
  11.  
  12. local die = function(ret)
  13. print(usage)
  14. os.exit(ret or 0)
  15. end
  16.  
  17. local rational = function(decimal, acc)
  18. acc = acc or 1E-3
  19. local sign, num, denum
  20. local sign = (decimal < 0) and -1 or 1
  21. decimal = math.abs(decimal)
  22. if decimal == math.floor(decimal) then
  23. num = decimal * sign
  24. denum = 1
  25. return num, denum
  26. end
  27. if decimal < 1E-19 then
  28. num = sign
  29. denum = 9999999999999999999
  30. elseif decimal > 1E+19 then
  31. num = 9999999999999999999 * sign
  32. denum = 1
  33. end
  34. local z = decimal
  35. local predenum = 0
  36. local sc
  37. denum = 1
  38. repeat
  39. z = 1 / (z - math.floor(z))
  40. sc = denum
  41. denum = denum * math.floor(z) + predenum
  42. predenum = sc
  43. num = math.floor(decimal * denum)
  44. until ((math.abs(decimal - (num / denum)) < acc) or (z == math.floor(z)))
  45. num = sign * num
  46. return num, denum
  47. end
  48.  
  49. local flags, args = {}, {}
  50. for i, v in ipairs(arg) do
  51. if v:match("^-") then
  52. for f in v:gmatch("%w") do
  53. flags[f] = true
  54. end
  55. else
  56. args[#args+1] = v
  57. end
  58. end
  59.  
  60. local w, h = tonumber(args[1]), tonumber(args[2])
  61. if not w or not h then
  62. die(1)
  63. end
  64.  
  65. local region = flags.p and "pal" or "ntsc"
  66. local screen = flags.w and "wide" or "full"
  67.  
  68. local pars = {
  69. ntsc = { wide = 40/33, full = 10/11 },
  70. pal = { wide = 16/11, full = 12/11 }
  71. }
  72.  
  73. local dar = (w / h) * pars[region][screen]
  74. local num, den = rational(dar)
  75. io.write(num, ":", den, "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement