[LIBRARY] srt.lua A simple srt caption parser
Posted: Mon Jan 08, 2024 2:30 am
This is my first contribution to the community and really there's not much to talk about this is a simple library for getting the current line of an .srt of a song or video in a given time.
Get a copy here:
Github: https://github.com/alejandro-alzate/srt-lua
or here:
Luarocks: https://luarocks.org/modules/alejandro-alzate/srt
Overview
There are three simple highlights to the library simple quick and dirty as well of three api methods to keep in mind:
The function srt.new(string) returns an table containing the given string, a clock and the text on the given time plus some other goodies.
A real world example would look like this:
local captions = srt.new(love.filesystem.read("captions.en.srt"))
The best way to interact with this library is with the method :setTime(time) that takes an integer time that is the time elapsed in seconds, this method also automatically calls internally :update(delta) that is only needed if you want to offset the playback of the captions. you can use the Source:tell to get the elapsed time and pass it to srt, a real world example would look like this:
captions:setTime(yourAudio:tell())
Last but not least important there's the :getText() method that just returns the text stored within the captions object this can seem useless since you can access directly to the text through captions.outputText but calling the method is recommended because it could get updates to add other functionality also i'm not gonna lie but it just looks good in my humble opinion.
Once again here could be a real world example of the library on use.
local text = captions:getText()
local x = (love.graphics.getWidth() / 2) - (love.graphics.getFont():getWidth(text) / 2 )
local y = love.graphics.getHeight() - love.graphics.getFont():getHeight() * 2
love.graphics.print(captions:getText(), x, y)
A complete example would look like this:
Well with nothing to be said anymore i log off, have a great day/night and enjoy coding with my library!
Get a copy here:
Github: https://github.com/alejandro-alzate/srt-lua
or here:
Luarocks: https://luarocks.org/modules/alejandro-alzate/srt
Overview
There are three simple highlights to the library simple quick and dirty as well of three api methods to keep in mind:
The function srt.new(string) returns an table containing the given string, a clock and the text on the given time plus some other goodies.
A real world example would look like this:
local captions = srt.new(love.filesystem.read("captions.en.srt"))
The best way to interact with this library is with the method :setTime(time) that takes an integer time that is the time elapsed in seconds, this method also automatically calls internally :update(delta) that is only needed if you want to offset the playback of the captions. you can use the Source:tell to get the elapsed time and pass it to srt, a real world example would look like this:
captions:setTime(yourAudio:tell())
Last but not least important there's the :getText() method that just returns the text stored within the captions object this can seem useless since you can access directly to the text through captions.outputText but calling the method is recommended because it could get updates to add other functionality also i'm not gonna lie but it just looks good in my humble opinion.
Once again here could be a real world example of the library on use.
local text = captions:getText()
local x = (love.graphics.getWidth() / 2) - (love.graphics.getFont():getWidth(text) / 2 )
local y = love.graphics.getHeight() - love.graphics.getFont():getHeight() * 2
love.graphics.print(captions:getText(), x, y)
A complete example would look like this:
Well with nothing to be said anymore i log off, have a great day/night and enjoy coding with my library!