Advertisement
nucular

ViReEn One by nucular

Feb 7th, 2013
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.06 KB | None | 0 0
  1. -- ###################################################################
  2. --                              VIREEN
  3. --                            by nucular
  4. local                       Version = "One"
  5. -- ###################################################################
  6. -- The new VIdeo REcorder and ENcoder for TPT!
  7. -- Licensed under Creative Commons Attribution 3.0 Unported (CC BY 3.0)
  8. -- Look at http://creativecommons.org/licenses/by/3.0/
  9. -- ####################################################################
  10.  
  11. --      Look at it as a successor of my mod of gamax92's Moviemaker.
  12. --      Please post wishes, your own code and bug reports there:
  13. local   Forum_Thread = "http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=15965"
  14. --
  15. --      The newest version can be found there:
  16. local   Download_Page = "http://pastebin.com/10kvD95u"
  17. --
  18. --
  19. -- THATS NEW IN VIREEN:
  20. -- - Completely rewritten and ported to the Interface API.
  21. -- - Many formats to support.
  22. -- - Appears now as a addition to the TPT-intern R keybind, so its as fast as C++ can! (And thats far more than LUA)
  23. -- - Flawless support of TPT++ and newer versions.
  24. -- - Programmed for the newest version of FFMPEG.
  25.  
  26. -- CHANGES:
  27. -- - FROM A BIT OF DUST TO ONE:
  28. --   - Everything.
  29.  
  30. -- KNOWN BUGS:
  31. -- - Doesn't record interface or HUD now due to TPTs recording function (unable to fix for now)
  32. -- - TPT freezes when converting / deleting many frames (already at TODO)
  33.  
  34. -- TODO:
  35. -- - Allow user to place the script in subdirectory of TPT
  36. -- - More converting options
  37. -- - Add debugging function if needed
  38. -- - Use asynchrous os.execute to avoid freezing TPT (i think thats impossible without extern libraries)
  39.  
  40.  
  41.  
  42. --set up default settings and vars
  43. local setting_compatibility = 1 --set to 1 if youre using a older version of ffmpeg
  44. local setting_fps = 25
  45. local setting_format = ".avi"
  46. local setting_comp = 3
  47. local var_framesrecorded = 0
  48. local tpt_records = false
  49. local window = Window:new(-1, -1, 200, 100)
  50. local help = Window:new(-1, -1, 200, 222)
  51.  
  52. --check platform (win or linux) from moviemaker
  53. if package.config:sub(1,1) == "\\" then
  54.    var_backslash = "\\"
  55. else
  56.    var_backslash = "/"
  57. end
  58.  
  59. --nice little func to open a website
  60. local function openwebsite(url)
  61.     if var_backslash == "\\" then
  62.         os.execute("explorer " .. url) --Windows
  63.     else
  64.         os.execute("x-www-browser " .. url) --Linux
  65.     end
  66. end
  67.  
  68. -- check if Videos folder exists, create it if not, show mesagebox if creating failed
  69. if not fs.exists("Videos") then
  70.    if not fs.makeDirectory("Videos") then
  71.       tpt.message_box("ViReEn - Creating folder failed","Please create folder 'Videos' in\nTPTs directory manually.")
  72.    end
  73. end
  74.  
  75. -- check if FFMPEG exists
  76. if not fs.exists("ffmpeg.exe") then
  77.     tpt.message_box("ViReEn - FFMPEG not found","Please download FFMPEG from this\nwebsite and copy it in TPTs folder.")
  78.     if var_backslash == "\\" then
  79.         openwebsite("http://www.ffmpeg.org/download.html#WindowsBuilds") --open windows builds website
  80.     else
  81.         openwebsite("http://www.ffmpeg.org/download.html#LinuxBuilds") --open linux builds website
  82.     end
  83. end
  84.  
  85. --returns false if user pressed cancel on recording-message
  86. --returns true if user pressed continue
  87. local function pressed(mousex,mousey)
  88.     if mousex >= 189 and mousey >= 231 and mousex <= 362 and mousey <= 246 then
  89.         return false
  90.     elseif mousex >= 364 and mousey >= 231 and mousex <= 438 and mousey <= 246 then
  91.         return true
  92.     end
  93. end
  94.  
  95. --show introduction
  96. local function introduction()
  97.     tpt.log("Welcome to ViReEn.")
  98.     tpt.log("To start recording, press [R].")
  99. end
  100.  
  101.  
  102. --helper function from mniip's xstptirc
  103. local function textwidth(s)
  104.  return select(2,pcall(tpt.textwidth,s))
  105. end
  106.  
  107.  
  108.  
  109. --ADD A FEW GUI COMPONENTS
  110. local var_codecstring = ", .gif, .mp4, .mpg"
  111. local label_codec_info = Label:new(102,14,textwidth(var_codecstring),14,var_codecstring)
  112. window:addComponent(label_codec_info)
  113.  
  114. --Codec Button
  115. local button_codec = Button:new(70,14,30,14,setting_format,"Change output codec")
  116. window:addComponent(button_codec)
  117. --END
  118.  
  119.  
  120.  
  121. --save settings
  122. local function save_last()
  123.     local savestring = "FPS "..tostring(setting_fps).."\nCOD "..setting_format.."\nCOM "..tostring(setting_comp).."\nINF "..label_codec_info:text()
  124.     local f = io.open("vireen.pref","w")
  125.     if f then
  126.         f:write(savestring)
  127.         f:close()
  128.     end
  129. end
  130.  
  131. -- load settings based on code from cracker64's awesome Autorun Script Manager
  132. local function load_last()
  133.     local f = io.open("vireen.pref","r")
  134.     if f then
  135.         local lines = {}
  136.         local line = f:read("*l")
  137.         while line do
  138.             table.insert(lines,line)
  139.             line = f:read("*l")
  140.         end
  141.         f:close()
  142.         for i=1, #lines do
  143.             local tok=lines[i]:sub(1,3)
  144.             local str=lines[i]:sub(5)
  145.             if tok=="FPS" then
  146.                 setting_fps = tonumber(str)
  147.             elseif tok=="COD" then
  148.                 setting_format = str
  149.                 button_codec:text(str)
  150.             elseif tok=="COM" then
  151.                 setting_comp = tonumber(str)
  152.             elseif tok=="INF" then
  153.                 label_codec_info:text(str)
  154.             end
  155.         end
  156.     else
  157.         introduction()
  158.         save_last()
  159.     end
  160. end
  161. load_last()
  162.  
  163. --hack for getting selected button.
  164. --by mniip
  165. local checkloop
  166. checkloop=function()
  167.         local x,y = tpt.mousex,tpt.mousey
  168.         tpt_records = pressed(x,y)
  169.         tpt.unregister_step(checkloop)
  170. end
  171.  
  172. --check if user presses the R key
  173. local function checkR(key, numkey, modifier, event)
  174.     --tpt.log("key pressed")
  175.     if key == "r" and event == 1 then
  176.         if tpt_records == false then
  177.             tpt.register_step(checkloop)
  178.         else
  179.             tpt_records = false
  180.             interface.showWindow(window)
  181.         end
  182.     end
  183. end
  184.  
  185. --convert video
  186. local function convert()
  187.     if setting_compatibility == 1 then
  188.         os.execute("ffmpeg -r " .. tostring(setting_fps) .. " -qscale " .. setting_comp .. " -i frame_%06d.ppm Videos" .. var_backslash .. "tptvideo" .. tostring(os.time()).. setting_format)
  189.     else
  190.         os.execute("ffmpeg -r " .. tostring(setting_fps) .. " -q:s " .. setting_comp .. " -i frame_%06d.ppm Videos" .. var_backslash .. "tptvideo" .. tostring(os.time()).. setting_format)
  191.     end
  192. end
  193.  
  194. --UI DESIGN BEGINS HERE
  195.  
  196. --Help screen
  197. local helptext =    "ViReEn " .. Version .. "\n" ..
  198.                     "by\n\n" ..
  199.                     "Press R to start recording.\n" ..
  200.                     "In your TPT folder will appear a bunch\n" ..
  201.                     "of .ppm files. These are the recorded\n" ..
  202.                     "frames. After you pressed R again, the\n" ..
  203.                     "ViReEn Interface will pop up. Set the\n" ..
  204.                     "settings and press 'Save Video'.\n" ..
  205.                     "You can delete the raw frames by\n" ..
  206.                     "pressing 'Delete *.ppm'."
  207. local label_help = Label:new(5,5,185,130,helptext)
  208. help:addComponent(label_help)
  209. local button_forum = Button:new(5,150,select(1,window:size())-10,14,"Open Forum Thread","Open ViReEn's forum thread in browser")
  210. button_forum:action(
  211.     function(sender)
  212.         openwebsite(Forum_Thread)
  213.     end
  214. )
  215. help:addComponent(button_forum)
  216. local button_update = Button:new(5,166,select(1,window:size())-10,14,"Open Download Page","Open ViReEn's downloading page in browser")
  217. button_update:action(
  218.     function(sender)
  219.         openwebsite(Download_Page)
  220.     end
  221. )
  222. help:addComponent(button_update)
  223. local button_profile = Button:new(20,16,40,14,"nucular","Open nucular's user profile")
  224. button_profile:action(
  225.     function(sender)
  226.         openwebsite("http://www.tpt.io/@nucular")
  227.     end
  228. )
  229. help:addComponent(button_profile)
  230. local label_discl = Label:new(3,140,185,130,"Licensed under Creative Commons\nAttribution 3.0 Unported (CC BY 3.0)")
  231. help:addComponent(label_discl)
  232. local button_exithlp = Button:new(select(1,window:size())-16,2,14,14,"X","Close Help")
  233. button_exithlp:action(
  234.     function(sender)
  235.         save_last()
  236.         interface.closeWindow(help)
  237.         --tpt.log("closed interface")
  238.     end
  239. )
  240. help:addComponent(button_exithlp)
  241.  
  242.  
  243.  
  244.  
  245. --Exit Button
  246. local button_exit = Button:new(select(1,window:size())-16,2,14,14,"X","Close ViReEn")
  247. button_exit:action(
  248.     function(sender)
  249.         save_last()
  250.         interface.closeWindow(window)
  251.         --tpt.log("closed interface")
  252.     end
  253. )
  254. window:addComponent(button_exit)
  255.  
  256. --Help Button
  257. local button_help = Button:new(select(1,window:size())-31,2,14,14,"?","Show help")
  258. button_help:action(
  259.     function(sender)
  260.         interface.showWindow(help)
  261.     end
  262. )
  263. window:addComponent(button_help)
  264.  
  265. --Various Labels
  266. local label_codec = Label:new(5,14,textwidth("Codec:"),14,"Codec:")
  267. window:addComponent(label_codec)
  268. local label_fps = Label:new(5,30,textwidth("Frame Rate:"),14,"Frame Rate:")
  269. window:addComponent(label_fps)
  270. local label_comp = Label:new(5,46,textwidth("Compression:"),14,"Compression:")
  271. window:addComponent(label_comp)
  272. local label_info = Label:new(5,select(2,window:size())-16,textwidth("ViReEn " .. tostring(Version) .. " by nucular"),14,"ViReEn " .. tostring(Version) .. " by nucular")
  273. window:addComponent(label_info)
  274. local label_fps_info = Label:new(70,30,textwidth(tostring(setting_fps)),14,tostring(setting_fps))
  275. window:addComponent(label_fps_info)
  276. local label_comp_info = Label:new(70,46,textwidth(tostring(setting_comp)),14,tostring(setting_comp))
  277. window:addComponent(label_comp_info)
  278.  
  279. --FPS Slider
  280. local slider_fps = Slider:new(90,30,select(1,window:size())-104,14,50)
  281. slider_fps:value(setting_fps)
  282. slider_fps:onValueChanged(
  283.     function(sender, value)
  284.         setting_fps = slider_fps:value()+10
  285.         label_fps_info:text(tostring(setting_fps))
  286.         label_fps_info:size(textwidth(tostring(setting_fps)),14)
  287.     end
  288. )
  289. window:addComponent(slider_fps)
  290.  
  291. --Compression Slider
  292. local slider_comp = Slider:new(90,46,select(1,window:size())-104,14,30)
  293. slider_comp:value(setting_comp)
  294. slider_comp:onValueChanged(
  295.     function(sender, value)
  296.         setting_comp = slider_comp:value()+1
  297.         label_comp_info:text(tostring(setting_comp))
  298.         label_comp_info:size(textwidth(tostring(setting_comp)),14)
  299.     end
  300. )
  301. window:addComponent(slider_comp)
  302.  
  303. --Convert Button
  304. local button_convert = Button:new(select(1,window:size())-82,select(2,window:size())-16,80,14,"Save Video","Convert raw frames and save video")
  305. button_convert:action(convert)
  306. window:addComponent(button_convert)
  307.  
  308. --Delete Button
  309. local button_delete = Button:new(select(1,window:size())-82,select(2,window:size())-31,80,14,"Clear *.ppm","Delete raw frames")
  310. button_delete:action(
  311.     function(sender)
  312.         if var_backslash == "\\" then
  313.             os.execute("del frame_*.ppm")
  314.          else
  315.             os.execute("rm frame_*.ppm")
  316.          end
  317.     end
  318. )
  319. window:addComponent(button_delete)
  320.  
  321. --Do button_codec and label_codec_info text change
  322. local function button_codec_change()
  323.     if setting_format == ".avi" then
  324.         setting_format = ".gif"
  325.         label_codec_info:text(", .mp4, .mpg, .avi")
  326.     elseif setting_format == ".gif" then
  327.         setting_format = ".mp4"
  328.         label_codec_info:text(", .mpg, .avi, .gif")
  329.     elseif setting_format == ".mp4" then
  330.         setting_format = ".mpg"
  331.         label_codec_info:text(", .avi, .gif, .mp4")
  332.     elseif setting_format == ".mpg" then
  333.         setting_format = ".avi"
  334.         label_codec_info:text(", .gif, .mp4, .mpg")
  335.     end
  336.     button_codec:text(setting_format)
  337.     label_codec_info:size(textwidth(label_codec_info:text()),14)
  338. end
  339. button_codec:action(
  340.     function(sender)
  341.         button_codec_change()
  342.     end
  343. )
  344.  
  345. tpt.register_keypress(checkR)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement