-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportloop.lua
94 lines (79 loc) · 2.59 KB
/
exportloop.lua
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
-- Takes in an mpv loop and passes it through ffmpeg to output an mp4
-- For now only mp4 is supported, but gif may be implemented later
local mp = require "mp"
local utils = require "mp.utils"
local defaults = {
videoEnc = "libx264",
audioEnc = "aac",
otherFlags = {'-movflags', '+faststart'}
}
local burnsubs = false
local function fileInDir(dirfiles, filename)
local foundFile = false
for k, file in pairs(dirfiles) do
if file == filename then
foundFile = true
break
end
end
return foundFile
end
local function getOutputName(filename)
local outprefix = (filename:match("([^/]+)%..+$") or "")
local outfilename = outprefix .. "_loop.mp4"
local dirfiles = utils.readdir(".", "files")
local foundFile = fileInDir(dirfiles, outfilename)
if not foundFile then
return outfilename
end
for i=1, 100 do
outfilename = outprefix .. "_loop" .. tostring(i) .. ".mp4"
if not fileInDir(dirfiles, outfilename) then
return outfilename
end
end
return false
end
local function export_loop(burnsubs)
local burnsubs = burnsubs or false
local starttime = mp.get_property("ab-loop-a")
local endtime = mp.get_property("ab-loop-b")
if (not starttime or starttime == 'no' or not endtime or endtime == 'no') then
print("Could not get loop ends. Please set loop boundaries first.")
return false
end
local duration = endtime - starttime
if not (starttime and endtime and duration > 0) then
print("Invalid loop, please make sure ab-loop-b > ab-loop-a")
return false
end
local filename = mp.get_property("filename")
local outfilename = getOutputName(filename)
-- If we are burning subtitles, it is necessary to place -ss after -i.
-- This is a big hit to efficiency, as it forces ffmpeg to read the file from the beginning, so we do not do this by default
local ffmpegArgs = {}
if burnsubs then
ffmpegArgs = {
'run', 'ffmpeg', '-i', filename, '-ss', starttime, '-t', duration, "-c:v", defaults.videoEnc, "-c:a", defaults.audioEnc,
"-filter_complex", "subtitles=\'" .. filename .. "\'"
}
else
ffmpegArgs = {
'run', 'ffmpeg', '-ss', starttime, '-i', filename, '-t', duration, "-c:v", defaults.videoEnc, "-c:a", defaults.audioEnc
}
end
for k,v in pairs(defaults.otherFlags) do
table.insert(ffmpegArgs, v)
end
table.insert(ffmpegArgs, outfilename)
print (unpack(ffmpegArgs))
mp.commandv(unpack(ffmpegArgs))
end
local function export_loop_burnsubs()
export_loop(true)
end
local function export_loop_nosubs()
export_loop(false)
end
mp.add_key_binding("G", "export_loop_burnsubs", export_loop_burnsubs)
mp.add_key_binding("g", "export_loop_nosubs", export_loop_nosubs)