Kemarin temanku di kantor, Shirly, memberitahu kalau Prison Break season 4 episode 9 sudah keluar. Aku pun menyalin avi dan srt via usb. Sampai di rumah langsung aku pasang usb ke dvd player LGku, dan tekan tombol Play.
Alangkah terkejut diriku ternyata subtitle yang muncul cuma sekali dan aku tekan tombol next tetap tidak muncul. Tadinya kupikir resolusi TV LCD Philips 32″ mesti aku ubah menjadi 4:3, Super Zoom, Widescreen, dsb. Tetap tidak bisa. Hemm… apa yang salah ya? Padahal di PC bisa keluar dengan sukses. Eh Shirly SMS kalau SRT (file subtitlenya) ternyata tidak berurutan timenya. Itu yang mungkin menyebabkan tidak keluar.
Nah dalam bayanganku aku berbicara dengan Michael Scoffield:
Me: Mas Scof, gimana neh. Aku dah pengen banget melihat petualangan kalian di episode 9 yang semakin menarik ini. Udah mulai ilfil nih sama Heroes.
Scoffield: Yah kamu gimana sih Wis. Kirain Inggris lo jago. Masak hari gene masih butuh subtitle segala?
Me: Ya kalau cuma Oh My God… Oh Yes.. Oh No… itu mah gue ngerti Mas. Tapi kadang kan ente ngomong masalah Scilla atau Bird Book cepet banget. Kadang gak jelas. Jadi biar ngerti banget apa yang elo-elo pade katakan, mending gue baca subtitlenya. Tapi SRT kampret ini nggak urut sama sekali. Jadi mau nonton di TV gak bisa lihat subtitlenya. Any idea, mas?
Scoffield: Ah.. gue kan lebih ke arsitek. Masak elo gak ada ide sih? Kan sering gue kasih contoh. Bahwa dibalik kesulitan, ada kemudahan. Banyak kan kejadian elo ngira gue bakalan ketangkep tapi gue selalu ada jalan keluar?
Me: Ya terang lah.. wong Paul Scheuring yang ngasih jalan keluar!
Scoffield: Teuteup. Intinya gunakan dong kemampuan yang elo miliki. Trus cari way outnya…
Me: Oke deh.. gue tidur dulu ya Mas. Besok kali gue ada pencerahan.
Nah pas bangun pagi, habis sholat subuh, ada ide. Hmm.. kalau aku bisa membaca file SRT dengan program, lalu menampung semua teksnya, dan diurutkan.. Maka aku bisa menuliskan hasil urutannya menjadi file SRT yang terurut! Segeralah aku membuka PC hitam kesayanganku, dan mengklik Borland Delphi Studio 2006. This should be quick and easy.
Mulailah aku mencari pola file yang tidak terurut itu:
1
00:00:40,330 –> 00:00:42,030
Move Scylla.
2
00:02:11,060 –> 00:02:12,400
The data on Scylla is too delicate.
3
00:36:31,500 –> 00:36:33,570
Alex.
4
00:11:19,130 –> 00:11:22,400
Alexander Mahone was forced to do things by the Company,
5
00:38:11,670 –> 00:38:13,470
Good afternoon to you, too, Andy.
Lihat kan, timenya gak berurutan? Kayaknya DVD Player gak punya kemampuan sorting by time kalau SRTnya ngaco.
Ya udah… mulai deh desain tampilannya:
Put some code..
Umm…. I think I’m gonna need a small class to contain the text and the time.
TSubtitle = class(TObject)
strict private
FTime: String;
FText: String;
procedure SetText(const Value: String);
procedure SetTime(const Value: String);
public
property Time: String read FTime write SetTime;
property Text: String read FText write SetText;
end;
Ok… I should let the user open a SRT, load it in a memo, load all the time and text inside a container, sort it by time, and finally store it in a new file! How’s that Mas Scoffield?
Scoffield: Hmm.. si Papi.. Finally you find the algorithm, eh?
Me: Thanx to you. Your movie can make me coding early in the morning just because I really want to see you in action with … subtitle in my LCD 32”.
Scoffield: Ok. Ok. No more talk. We don’t have much time. You have to finish the code, put the sorted SRT back in your usb, and watch me, right? And you have a meeting with your boss 9 AM sharp in the office rite? Better you code in rush and fast. Don’t buggy or you will be late!
Me: Beres Boss!
Ok.. here is the main algorithm:
procedure TformMain.btnSortClick(Sender: TObject);
function CompareNames(Item1, Item2: Pointer): Integer;
begin
Result := CompareText(TSubtitle(Item1).Time,
TSubtitle(Item2).Time);
end;
begin
btnSave.Enabled := False;
LoadSubtitles(mmOriginal.Lines);
FSubtitles.Sort(@CompareNames);
WriteSortedSubTitles(FSubtitles, mmSorted.Lines);
btnSave.Enabled := True;
end;
Ini bagian loading subtitles dari file ke Container:
procedure TformMain.LoadSubtitles(textList: TStrings);
var
i: integer;
subtitle: TSubtitle;
begin
i := 0;
progressBar.Position := 0;
progressBar.Properties.Max := textList.Count;
progressBar.Properties.Text := ‘Loading subtitles…’;
while i < textList.Count do
begin
if Pos(‘–>’, textList[i]) > 0 then
begin
subtitle := TSubtitle.Create;
subtitle.Time := textList[i];
if i + 1 < textList.Count then
subtitle.Text := textList[i + 1];
FSubtitles.Add(subtitle);
end;
Inc(i);
progressBar.Position := i;
if i Mod 10 = 0 then
Application.ProcessMessages;
end;
end;
To put the sorted into the memo is more likely a trivial job to do:
procedure TformMain.WriteSortedSubTitles(subtitles: TObjectList; destination: TStrings);
var
i: integer;
begin
mmSorted.Clear;
i := 0;
progressBar.Position := 0;
progressBar.Properties.Max := FSubtitles.Count;
progressBar.Properties.Text := ‘Sorting subtitles…’;
while i < FSubtitles.Count do
begin
destination.Add(VarToStr(i + 1));
destination.Add(TSubtitle(FSubtitles[i]).Time);
destination.Add(TSubtitle(FSubtitles[i]).Text);
destination.Add(”);
Inc(i);
progressBar.Position := progressBar.Position + 1;
if i Mod 10 = 0 then
Application.ProcessMessages;
end;
end;
The full listing of source code and the ex
ecutable file is attached in this blog. (Sorry no DFM, since I use component from DevExpress), but hey.. you can substitute the memo and the button with standard VCL. Mas Scoffield, copy aja ke laptopnya Mas. Hehehe…
Akhirnya… begitu dicolokin ke dvd player, muncullah teks dengan benar dan maknyus…
Spoiler : 2 orang mati di episode ini… Siapa dia? Nonton aja ya sendiri!