@@ -4,7 +4,7 @@ local Promise = require('jls.lang.Promise')
4
4
local StringBuffer = require (' jls.lang.StringBuffer' )
5
5
local File = require (' jls.io.File' )
6
6
local strings = require (' jls.util.strings' )
7
- local TableList = require (' jls.util.TableList ' )
7
+ local List = require (' jls.util.List ' )
8
8
local LocalDateTime = require (' jls.util.LocalDateTime' )
9
9
10
10
local function getExecutableName (name )
@@ -51,6 +51,7 @@ return class.create(function(ffmpeg)
51
51
self .ffprobePath = getExecutableName (' ffprobe' )
52
52
end
53
53
end
54
+ self .seekDelayMs = options .seekDelay or 0
54
55
end
55
56
56
57
function ffmpeg :check ()
@@ -93,24 +94,30 @@ return class.create(function(ffmpeg)
93
94
function ffmpeg :computeArguments (destFilename , destOptions , srcFilename , srcOptions , globalOptions )
94
95
local args = {self .ffmpegPath , ' -hide_banner' }
95
96
if globalOptions then
96
- TableList .concat (args , globalOptions )
97
+ List .concat (args , globalOptions )
97
98
end
98
99
if srcOptions then
99
- TableList .concat (args , srcOptions )
100
+ List .concat (args , srcOptions )
100
101
end
101
102
if srcFilename then
102
- TableList .concat (args , ' -i' , srcFilename )
103
+ if type (srcFilename ) == ' table' then
104
+ for _ , filename in ipairs (srcFilename ) do
105
+ List .concat (args , ' -i' , filename )
106
+ end
107
+ else
108
+ List .concat (args , ' -i' , srcFilename )
109
+ end
103
110
end
104
111
if destOptions then
105
- TableList .concat (args , destOptions )
112
+ List .concat (args , destOptions )
106
113
end
107
114
if destFilename then
108
- TableList .concat (args , ' -y' , destFilename )
115
+ List .concat (args , ' -y' , destFilename )
109
116
end
110
117
return args
111
118
end
112
119
113
- function ffmpeg :createCommand (part , filename , options , seekDelay )
120
+ function ffmpeg :createCommand (part , filename , options , parameters )
114
121
--[[
115
122
'-ss position (input/output)'
116
123
When used as an input option (before -i), seeks in this input file to position.
@@ -132,25 +139,25 @@ return class.create(function(ffmpeg)
132
139
local srcOptions = {}
133
140
local destOptions = {}
134
141
if part .from ~= nil then
135
- local delay = seekDelay or 0
142
+ local delay = ( parameters and parameters . seekDelay ) or self . seekDelayMs or 0
136
143
if (delay >= 0 ) and (delay < part .from ) then
137
- TableList .concat (srcOptions , ' -ss' , formatTime (part .from - delay ))
138
- TableList .concat (destOptions , ' -ss' , math.floor (delay ))
144
+ List .concat (srcOptions , ' -ss' , formatTime (part .from - delay ))
145
+ List .concat (destOptions , ' -ss' , math.floor (delay ))
139
146
elseif delay == - 1 then
140
- TableList .concat (srcOptions , ' -ss' , formatTime (part .from ))
147
+ List .concat (srcOptions , ' -ss' , formatTime (part .from ))
141
148
else
142
- TableList .concat (destOptions , ' -ss' , formatTime (part .from ))
149
+ List .concat (destOptions , ' -ss' , formatTime (part .from ))
143
150
end
144
151
end
145
152
if part .to ~= nil then
146
153
if part .from ~= nil then
147
- TableList .concat (destOptions , ' -t' , formatTime (part .to - part .from ))
148
- -- TableList .concat(destOptions, '-to', formatTime(part.to))
154
+ List .concat (destOptions , ' -t' , formatTime (part .to - part .from ))
155
+ -- List .concat(destOptions, '-to', formatTime(part.to))
149
156
else
150
- TableList .concat (destOptions , ' -t' , formatTime (part .to ))
157
+ List .concat (destOptions , ' -t' , formatTime (part .to ))
151
158
end
152
159
end
153
- TableList .concat (destOptions , options )
160
+ List .concat (destOptions , options )
154
161
local sourceFile = self .sources [part .sourceId ]
155
162
return self :computeArguments (filename , destOptions , sourceFile :getPath (), srcOptions )
156
163
end
@@ -159,17 +166,30 @@ return class.create(function(ffmpeg)
159
166
return File :new (self .cacheDir , filename )
160
167
end
161
168
162
- function ffmpeg :createCommands (filename , parts , destOptions , seekDelayMs )
169
+ function ffmpeg :deleteTempFiles ()
170
+ local tmpFiles = self .cacheDir :listFiles (function (file )
171
+ return file :isFile () and (file :getExtension () == ' tmp' )
172
+ end )
173
+ for _ , file in ipairs (tmpFiles ) do
174
+ file :delete ()
175
+ end
176
+ end
177
+
178
+ function ffmpeg :createCommands (destFilename , parts , destOptions , parameters )
163
179
local commands = {}
180
+ local filename = destFilename
181
+ if parameters .subtitles then
182
+ filename = self :createTempFile (' full.tmp' ):getPath ()
183
+ end
164
184
if # parts == 1 then
165
- table.insert (commands , self :createCommand (parts [1 ], filename , destOptions , seekDelayMs ))
185
+ table.insert (commands , self :createCommand (parts [1 ], filename , destOptions , parameters ))
166
186
elseif # parts > 1 then
167
187
local concatScript = StringBuffer :new ()
168
188
concatScript :append (' # fcut' )
169
189
for i , part in ipairs (parts ) do
170
190
local partName = ' part_' .. tostring (i ).. ' .tmp'
171
191
local outFilename = self :createTempFile (partName ):getPath ()
172
- table.insert (commands , self :createCommand (part , outFilename , destOptions , seekDelayMs ))
192
+ table.insert (commands , self :createCommand (part , outFilename , destOptions , parameters ))
173
193
local concatPartname = string.gsub (outFilename , ' [\\ +]' , ' /' )
174
194
-- local concatPartname = partName -- to be safe
175
195
concatScript :append (' \n file ' , concatPartname )
@@ -178,6 +198,9 @@ return class.create(function(ffmpeg)
178
198
concatFile :write (concatScript :toString ());
179
199
table.insert (commands , self :computeArguments (filename , {' -c' , ' copy' , ' -map' , ' 0' }, concatFile :getPath (), {' -f' , ' concat' , ' -safe' , ' 0' }))
180
200
end
201
+ if parameters .subtitles then
202
+ table.insert (commands , self :computeArguments (destFilename , {' -c' , ' copy' , ' -c:s' , ' mov_text' , ' -map' , ' 0' }, List .concat ({}, filename , parameters .subtitles )))
203
+ end
181
204
return commands
182
205
end
183
206
@@ -217,9 +240,9 @@ return class.create(function(ffmpeg)
217
240
local time = LocalDateTime :new ():plusSeconds (sec or 0 ):toTimeString ()
218
241
local args = {self .ffmpegPath , ' -hide_banner' , ' -v' , ' 0' , ' -ss' , time , ' -i' , sourceFile :getPath (), ' -f' , ' mjpeg' , ' -vcodec' , ' mjpeg' , ' -vframes' , ' 1' , ' -an' }
219
242
if width and height then
220
- TableList .concat (args , ' -s' , tostring (width ).. ' x' .. tostring (height ))
243
+ List .concat (args , ' -s' , tostring (width ).. ' x' .. tostring (height ))
221
244
end
222
- TableList .concat (args , ' -y' , file :getPath ())
245
+ List .concat (args , ' -y' , file :getPath ())
223
246
return args
224
247
end
225
248
0 commit comments