I'm trying to make my own video parody with custom subtitles. Creating your own custom subtitles file in a text editor with the SubRip or .SRT format is easy to do, it looks like this:
Where 00:00:20,000 is in hours:minutes:seconds,milliseconds format.
My question is really more about the manipulation needed to hack my text into .srt in a more elegant way...
Here's the text (scraped from a website) to parse:
downfall = Import["http://pastebin.com/raw/d6nmE5sT", "Text"]
And my code that parses it:
ss=StringSplit[downfall,"\n"];
ss1={StringTake[#,17],StringTake[#,18;;]}&/@ss
ss2=MapColumn[1,StringCases[#,t1___~~" - "~~t2___:>{t1,t2}][[1]]&,ss1];
f[t_]:=Module[{m,s,mi},{m,s,mi}=StringCases[t,m__~~":"~~s__~~"."~~mi__:>{ToExpression@m,ToExpression@s,ToExpression@mi}][[1]];
"00:"~~StringJoin[ToString/@PadLeft[IntegerDigits@m,2]]~~":"~~StringJoin[ToString/@PadLeft[IntegerDigits@s,2]]~~","~~ToString[mi]~~"0"
]
ss3=MapAt[f,ss2,{All,1,All}]
ss4=MapAt[StringRiffle[##," --> "]&,ss3,{All,1}]
ss5=StringJoin@Table[ToString[i]~~"\n"~~ss4[[i,1]]~~"\n"~~ss4[[i,2]]~~"\n\n",{i,1,Length@ss1}]
fn=Export[NotebookDirectory[]~~"python.txt",ss5,CharacterEncoding->"UTF-8"];
RenameFile[fn,NotebookDirectory[]~~"python.srt"];
So far it works, but is brittle, and there are a few specify problems I'd like to fix:
- I can't directly export the ".srt" because it's not a 'known' format to mathematica, hence the rename hack.
- My string parsing is a bit overcomplicated.
- Instead of opening another video editing app like VLC and then importing, I'd like to write the captions into the video file though mathematica (into the frames).
Thanks!
- The video I'm captioning is a clip from Downfall - here's the original video without captions)