Repository navigation
Expand file tree
/
Copy pathVideo recorder.lua
More file actions
126 lines (111 loc) · 4.38 KB
/
Copy pathVideo recorder.lua
File metadata and controls
126 lines (111 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
local sim = require 'sim-1'
local simUI
local simSubprocess
function sysCall_info()
return {autoStart = false, menu = 'Exporters\nVideo recorder...'}
end
function sysCall_init()
simUI = require 'simUI'
capturing = false
numCapturedFrames = 0
outputDir = sim.getStringParam(sim.stringparam_importexportdir)
ui = simUI.create [[<ui title="Video Recorder" closeable="true" on-close="onUiClose">
<group flat="true" layout="hbox" content-margins="0,0,0,0">
<label text="Output dir:" />
<edit id="${editOutputBaseDir}" value="${outputDir}" />
<button id="${btnBrowseOutputDir}" text="Browse..." on-click="browse" />
</group>
<checkbox id="${chkMakeVideo}" text="Convert image sequence to video" on-change="updateUi" />
<group enabled="false" id="${grpVideoEncoderOptions}" flat="true" layout="vbox" content-margins="0,0,0,0">
<group flat="true" layout="hbox" content-margins="0,0,0,0">
<label text="Frames per second:" />
<edit id="${editFPS}" value="20" />
</group>
<group flat="true" layout="hbox" content-margins="0,0,0,0">
<label text="Bitrate: (kbps)" />
<edit id="${editBitrate}" value="1800" />
</group>
</group>
<group flat="true" layout="hbox" content-margins="0,0,0,0">
<button id="${btnStartCapture}" text="Start capture" on-click="startCapture" />
<button id="${btnStopCapture}" text="Stop capture" on-click="stopCapture" enabled="false" />
</group>
</ui>]]
end
function sysCall_cleanup()
end
function sysCall_addOnScriptSuspend()
return {cmd = 'cleanup'}
end
function sysCall_afterSimulation()
end
function sysCall_beforeInstanceSwitch()
end
function sysCall_sensing()
if capturing then capture() end
end
function sysCall_nonSimulation()
if leaveNow then return {cmd = 'cleanup'} end
if capturing then capture() end
end
function onUiClose()
leaveNow = true
end
function browse()
local fileNames = simUI.fileDialog(
simUI.filedialog_type.folder, 'Select output directory', outputDir, '', '', '',
true
)
if #fileNames == 0 then return end
outputDir = fileNames[1]
updateUi()
end
function startCapture()
if capturing then return end
capturing = true
filePrefix = string.format('%d', os.time())
numCapturedFrames = 0
updateUi()
end
function stopCapture()
if not capturing then return end
capturing = false
if numCapturedFrames > 0 then
if simUI.getCheckboxValue(ui, chkMakeVideo) > 0 then
local fps = tonumber(simUI.getEditValue(ui, editFPS))
local br = tonumber(simUI.getEditValue(ui, editBitrate))
local inf = string.format('%s/%s_%s.png', outputDir, filePrefix, '%08d')
local outf = string.format('%s/%s.mp4', outputDir, filePrefix)
local args = {
'-r', tostring(fps), '-i', inf, '-c:v', 'libx264', '-b:v', br .. 'k', outf,
}
sim.addLog(sim.verbosity_scriptinfos, 'Running: ffmpeg ' .. table.join(args, ' '))
simSubprocess = simSubprocess or require 'simSubprocess'
local exitCode, output = simSubprocess.exec(
'ffmpeg', args, '',
{useSearchPath = true, openNewConsole = false}
)
if exitCode ~= 0 then
simUI.msgBox(
simUI.msgbox_type.critical, simUI.msgbox_buttons.ok, 'Error',
'Failed to execute ffmpeg:\n\n' .. output
)
else
sim.addLog(sim.verbosity_scriptinfos, output)
end
end
end
updateUi()
end
function updateUi()
simUI.setEnabled(ui, btnStartCapture, not capturing)
simUI.setEnabled(ui, btnStopCapture, capturing)
simUI.setEditValue(ui, editOutputBaseDir, outputDir)
simUI.setEnabled(ui, grpVideoEncoderOptions, simUI.getCheckboxValue(ui, chkMakeVideo) > 0)
end
function capture()
local img, res = auxFunc('fetchframe', -1) -- sim.getScaledImage('\xff\x00\xff',{1,1},{1024,768},0)
local fileName = string.format('%s/%s_%08d.jpg', outputDir, filePrefix, numCapturedFrames)
numCapturedFrames = numCapturedFrames + 1
sim.saveImage(img, res, 0, fileName, -1)
end