mux: ts: don't set fmt.extra for subt
[vlc.git] / share / lua / modules / common.lua
blob8c49c713e5e79c09033ffd3952d3606fb1231d6f
1 --[[ This code is public domain (since it really isn't very interesting) ]]--
3 module("common",package.seeall)
5 -- Iterate over a table in the keys' alphabetical order
6 function pairs_sorted(t)
7 local s = {}
8 for k,_ in pairs(t) do table.insert(s,k) end
9 table.sort(s)
10 local i = 0
11 return function () i = i + 1; return s[i], t[s[i]] end
12 end
14 -- Return a function such as skip(foo)(a,b,c) = foo(b,c)
15 function skip(foo)
16 return function(discard,...) return foo(...) end
17 end
19 -- Return a function such as setarg(foo,a)(b,c) = foo(a,b,c)
20 function setarg(foo,a)
21 return function(...) return foo(a,...) end
22 end
24 -- Trigger a hotkey
25 function hotkey(arg)
26 local id = vlc.misc.action_id( arg )
27 if id ~= nil then
28 vlc.var.set( vlc.object.libvlc(), "key-action", id )
29 return true
30 else
31 return false
32 end
33 end
35 -- Take a video snapshot
36 function snapshot()
37 local vout = vlc.object.vout()
38 if not vout then return end
39 vlc.var.set(vout,"video-snapshot",nil)
40 end
42 -- Naive (non recursive) table copy
43 function table_copy(t)
44 c = {}
45 for i,v in pairs(t) do c[i]=v end
46 return c
47 end
49 -- tonumber() for decimals number, using a dot as decimal separator
50 -- regardless of the system locale
51 function us_tonumber(str)
52 local s, i, d = string.match(str, "^([+-]?)(%d*)%.?(%d*)$")
53 if not s or not i or not d then
54 return nil
55 end
57 if s == "-" then
58 s = -1
59 else
60 s = 1
61 end
62 if i == "" then
63 i = "0"
64 end
65 if d == nil or d == "" then
66 d = "0"
67 end
68 return s * (tonumber(i) + tonumber(d)/(10^string.len(d)))
69 end
71 -- tostring() for decimals number, using a dot as decimal separator
72 -- regardless of the system locale
73 function us_tostring(n)
74 s = tostring(n):gsub(",", ".", 1)
75 return s
76 end
78 -- strip leading and trailing spaces
79 function strip(str)
80 return string.gsub(str, "^%s*(.-)%s*$", "%1")
81 end
83 -- print a table (recursively)
84 function table_print(t,prefix)
85 local prefix = prefix or ""
86 if not t then
87 print(prefix.."/!\\ nil")
88 return
89 end
90 for a,b in pairs_sorted(t) do
91 print(prefix..tostring(a),b)
92 if type(b)==type({}) then
93 table_print(b,prefix.."\t")
94 end
95 end
96 end
98 -- print the list of callbacks registered in lua
99 -- useful for debug purposes
100 function print_callbacks()
101 print "callbacks:"
102 table_print(vlc.callbacks)
103 end
105 -- convert a duration (in seconds) to a string
106 function durationtostring(duration)
107 return string.format("%02d:%02d:%02d",
108 math.floor(duration/3600),
109 math.floor(duration/60)%60,
110 math.floor(duration%60))
113 -- realpath
114 function realpath(path)
115 return string.gsub(string.gsub(string.gsub(string.gsub(path,"/%.%./[^/]+","/"),"/[^/]+/%.%./","/"),"/%./","/"),"//","/")
118 -- parse the time from a string and return the seconds
119 -- time format: [+ or -][<int><H or h>:][<int><M or m or '>:][<int><nothing or S or s or ">]
120 function parsetime(timestring)
121 local seconds = 0
122 local hourspattern = "(%d+)[hH]"
123 local minutespattern = "(%d+)[mM']"
124 local secondspattern = "(%d+)[sS\"]?$"
126 local _, _, hoursmatch = string.find(timestring, hourspattern)
127 if hoursmatch ~= nil then
128 seconds = seconds + tonumber(hoursmatch) * 3600
130 local _, _, minutesmatch = string.find(timestring, minutespattern)
131 if minutesmatch ~= nil then
132 seconds = seconds + tonumber(minutesmatch) * 60
134 local _, _, secondsmatch = string.find(timestring, secondspattern)
135 if secondsmatch ~= nil then
136 seconds = seconds + tonumber(secondsmatch)
139 if string.sub(timestring,1,1) == "-" then
140 seconds = seconds * -1
143 return seconds
146 -- seek
147 function seek(value)
148 local input = vlc.object.input()
149 if input ~= nil and value ~= nil then
150 if string.sub(value,-1) == "%" then
151 local number = us_tonumber(string.sub(value,1,-2))
152 if number ~= nil then
153 local posPercent = number/100
154 if string.sub(value,1,1) == "+" or string.sub(value,1,1) == "-" then
155 vlc.var.set(input,"position",vlc.var.get(input,"position") + posPercent)
156 else
157 vlc.var.set(input,"position",posPercent)
160 else
161 local posTime = parsetime(value)
162 if string.sub(value,1,1) == "+" or string.sub(value,1,1) == "-" then
163 vlc.var.set(input,"time",vlc.var.get(input,"time") + (posTime * 1000000))
164 else
165 vlc.var.set(input,"time",posTime * 1000000)
171 function volume(value)
172 if type(value)=="string" and string.sub(value,1,1) == "+" or string.sub(value,1,1) == "-" then
173 vlc.volume.set(vlc.volume.get()+tonumber(value))
174 else
175 vlc.volume.set(tostring(value))