aboutsummaryrefslogtreecommitdiff
path: root/lib/util.lua
blob: a1de8175a0200a63ac36af746ff0076b161f4f86 (plain) (blame)
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
#!/usr/bin/env lua

local table_merge = function (t1, t2)
	t1 = t1 or {}
	t2 = t2 or {}

	for k, v in pairs(t2) do
		t1[k] = v
	end

	return t1
end

local table_print = function (t)
	for k, v in pairs(t) do
		print( '|'.. k .. '=' .. v .. '|')
	end
end

local table_match_or_any = function (t, key)
	local str

	str= t[key]
	if not str then
		_, str = next(t, nil)
	end

	return str
end

local zip_ext_first = function (zip, out)
	local dir, rc, srt

	dir = os.tmpname()
	os.remove(dir)
	os.execute('mkdir -p ' .. dir)

	rc = os.execute('unzip -qq ' .. zip .. ' -d ' .. dir)
	srt = io.popen('find ' .. dir .. ' -type f -name *.srt'):read('*l')
	os.execute("mv '" .. srt .. "' '"  .. out .. "'")
	os.remove(dir)

	return rc
end

local string_rm_vid_ext = function (str)
	local extensions = {
		"mkv",
		"mp4",
		"webm",
		"flv",
		"gif",
		"gifv",
		"avi",
		"mpeg",
		"3gp"
	}

	for _, ext in ipairs(extensions) do
		str = str:gsub('.' .. ext, '')
	end

	return str
end

local error = function (str)
	str = 'error: ' .. str
	if mp then
		mp.msg.warn(str)
	else
		print(str)
	end
end

return {
	table_merge = table_merge,
	table_print = table_print,
	table_match_or_any = table_match_or_any,
	zip_ext_first = zip_ext_first,
	string_rm_vid_ext = string_rm_vid_ext,
	error = error,
}