Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
#!/usr/bin/env lua5.1 -- This file: -- http://anggtwu.net/LUA/Subtitles3.lua.html -- http://anggtwu.net/LUA/Subtitles3.lua -- (find-angg "LUA/Subtitles3.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- (defun e () (interactive) (find-angg "LUA/Subtitles3.lua")) -- See: (find-angg ".emacs.templates" "find-subtitles3-links") -- -- (find-anggfile "LUA/lua50init.lua" "cruft-jan2024.lua") -- (find-blogme3 "cruft-jan2024.lua") Path.addLUAtopath() require "GetOpt1" -- (find-angg "LUA/GetOpt1.lua") json = require "json" JsonSubs = Class { type = "JsonSubs", __index = { segtext_ = function (seg) return seg.utf8 end, segtext = function (j,seg) return seg.utf8 end, segstext = function (j,segs) return mapconcat(j.segtext_, segs) end, evtext = function (j,ev) return j:segstext(ev.segs) end, splittime = function (j,ms) local n = ms local chop = function (a) local b=math.fmod(n,a); n=(n-b)/a; return b end local ms,secs,mins,hours = chop(1000),chop(60),chop(60),n return hours,mins,secs,ms end, mmss = function (j,ms) local hours,mins,secs,ms = j:splittime(ms) if hours ~= 0 then return format("%d:%02d:%02d", hours,mins,secs) end return format("%02d:%02d", mins,secs) end, evmmss = function (j,ev) return j:mmss(ev.tStartMs) end, evtimetext = function (j,ev) local time = ev.tStartMs and j:evmmss(ev) local text = ev.segs and j:evtext(ev) return time,text end, alltimetexts = function (j,events) local timetexts = VTable {} for i,ev in ipairs(events) do local time,text = j:evtimetext(ev) if time and text and text:match("%S") then table.insert(timetexts, HTable {time, text}) end end return timetexts end, findtimetext = function (j,c,time,text) local q = function (s) return (s:gsub('["\\]', '\\%1')) end local fmt = '%% (find-%svideo "%s" "%s")' return format(fmt, c, time, q(text)) end, finds = function (j,events,c) local f = function (pair) return j:findtimetext(c, pair[1], pair[2]) end local timetexts = j:alltimetexts(events) return mapconcat(f, timetexts, "\n") end, }, } -- (find-angg "LUA/GetOpt1.lua" "GetOpt-tests") mygetopt = GetOpt { ["-c"] = function (g,c_) c = c_ g:rest(2) end, ["-json3"] = function (g,fname) bigstr = ee_readfile(fname) o = json.decode.decode(bigstr) g:rest(2) end, ["-print"] = function (g) jss = JsonSubs {} print(jss:finds(o.events, c)) g:rest(1) end, } mygetopt:run(...) --[[ * (eepitch-shell) * (eepitch-kill) * (eepitch-shell) ~/LUA/Subtitles3.lua -c 2024algpergs -json3 /tmp/subs/subs.json3 -print --]] --[==[ * (eepitch-shell) * (eepitch-kill) * (eepitch-shell) rm -Rv /tmp/subs/ mkdir /tmp/subs/ cd /tmp/subs/ URL="https://youtu.be/vBkxGIrv2Q0" yt-dlp --write-auto-sub --skip-download --sub-format=json3 --sub-lang=pt $URL cp -v *.pt.json3 subs.json3 --]==] --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Subtitles3.lua" bigstr = ee_readfile "/tmp/subs/subs.json3" jss = JsonSubs {} o = json.decode.decode(bigstr) = jss:finds(o.events, "2024lean4of0") PPPV(o.events[1]) PPPV(o.events[2]) PPPV(o.events[2].segs) PPPV(o.events[2].segs[1]) = jss:segtext (o.events[2].segs[1]) = jss:segstext(o.events[2].segs) = jss:evtext (o.events[2]) = jss:splittime(3600*1000) = jss:mmss (3600*1000) = jss:mmss ( 4*1000) PPPV(o.events[2]) = jss:evmmss (o.events[2]) = jss:evtimetext(o.events[1]) = jss:evtimetext(o.events[2]) = jss:evtimetext(o.events[3]) = jss:evtimetext(o.events[4]) = jss:evtimetext(o.events[5]) = #o.events PPPV(o.events[1]) PPPV(o.events[2]) PPPV(o.events[3]) PPPV(o.events[4]) PPPV(o.events[5]) % (find-cae0video "00:00" "Oi! O meu nome é Eduardo Ochs. Eu sou") = jss:alltimetexts(o.events) = o.events[2].tStartMs PPPV(o.events[3]) PPPV(o.events[4]) = jss:evtext(o.events[2]) = o.events[2].tStartMs --]==] -- Local Variables: -- coding: utf-8-unix -- End: