Skip to content

Instantly share code, notes, and snippets.

@Yonaba
Created June 29, 2011 14:39
Show Gist options
  • Save Yonaba/1053962 to your computer and use it in GitHub Desktop.
Save Yonaba/1053962 to your computer and use it in GitHub Desktop.
Lua Subtitle Parser
-- A simple way to read .srt subtitles while streaming music!
-- This code uses LuaPlayer for PSP as framework.
local subs = {}
local _time = {}
local current_sub = 1
function get_time(tm)
local startTime,endTime
local t1={}
for word in string.gfind(tm,'%d%d:') do
local num = string.gsub(word,':','')
table.insert(t1,num)
end
for word in string.gfind(tm,'%d%d,%d%d%d') do
local num = string.gsub(word,',','.')
table.insert(t1,num)
end
local sec,ms = math.modf(t1[5])
startTime = (t1[1]*3600+t1[2]*60+sec)*1000+ms*1000
local sec,ms = math.modf(t1[6])
endTime = (t1[3]*3600+t1[4]*60+sec)*1000+ms*1000
return {st = startTime, nd = endTime}
end
function PrintSub(time)
screen:print(10,30,'current Time: '..time,Color.new(255,255,255))
screen:print(10,40,'_time['..current_sub..']: st '.._time[current_sub].st..' nd : '.._time[current_sub].nd,Color.new(255,255,255))
if _time[current_sub] then
if _time[current_sub].nd >= time then
nxt = false
screen:print(10,10,subs[current_sub],Color.new(255,255,255))
else
nxt = true
if nxt then current_sub = current_sub+1 nxt = false end
end
end
end
local mediapath = 'music'
Mp3.load(mediapath..'.mp3')
local file = io.open(mediapath..'.srt',"r")
repeat
local line = file:read('*l')
if not (line) then break end
if string.find(line,'%^?%d+$') and not string.find(line,'-->') then
local pos = file:seek()
local tm = file:read('*l')
print(tm)
table.insert(_time,get_time(tm))
local subt = ''
repeat
local sub =file:read('*l')
if sub and not string.find(sub,'%^?%d+$') then
subt = subt..' '..sub
else break
end
until(1==0)
print(subt)
table.insert(subs,subt)
file:seek("set",pos)
end
until not(true)
file:close()
local timer = Timer.new()
timer:start()
Mp3.play()
while true do
screen:clear()
local currentTime = timer:time()
PrintSub(currentTime)
screen.waitVblankStart()
screen.flip()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment