Video::Subtitle::SRT - manipulate SRT subtitle files
use Video::Subtitle::SRT; my $subtitle = Video::Subtitle::SRT->new (\&callback); $subtitle->parse ($fh); sub callback { my $data = shift; # $data->{number} # $data->{start_time} # $data->{end_time} # $data->{text} }
Video::Subtitle::SRT is a callback based parser to parse SubRip subtitle files (.SRT). See the script adjust-srt in the distribution for how to use this module to create subtitle delay adjusting tools.
my $srt_wranger = Video::Subtitle::SRT->new (\& function);
The argument is the function to call back when parsing.
The callback function takes one argument, a hash reference. The hash has the following fields:
The start time of the subtitle, as a text string, in the SRT time format. This may be converted into a time in milliseconds from the start of the video using "srt_time_to_milliseconds".
The end time of the subtitle, as a text string, in the SRT time format. This may be converted into a time in milliseconds from the start of the video using "srt_time_to_milliseconds".
The text of the subtitle.
The number of the subtitle.
It does not need to return any value. It can die on error to halt parsing.
Parse a file handle,
open my $fh, "<", 'movie.srt' or die $!; $srt_wrangler->parse ($fh); close $fh or die $!;
or a file
$srt_wrangler->parse ('movie.srt');
Parse the subtitle file and call the callback specified in "new".
Convert an SRT time into a time in milliseconds.
Convert a time in milliseconds into an SRT time.
my $srt = make_subtitle (\%data);
Given a subtitle containing the fields {number => 1, text => 'Words', start_time => srt_time, end_time => srt_time}, make it into an SRT subtitle. The return value is the subtitle. If any of the fields is missing, this emits a fatal error.
{number => 1, text => 'Words', start_time => srt_time, end_time => srt_time}
$srt->add ($data);
Add the line to the subtitles.
$srt->write_file ();
Write the file of subtitles to STDOUT.
Video::Subtitle::SRT exports the following functions on request: "srt_time_to_milliseconds", "milliseconds_to_srt_time", and "make_subtitle". All the functions can be exported using the tag <code>:all</code>:
use Video::Subtitle::SRT ':all';
This explains the format.
The module's creator is Tatsuhiko Miyagawa <miyagawa@bulknews.net>. The module is currently maintained by Ben Bullock <bkb@cpan.org>.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Video::Subtitle::SRT, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Video::Subtitle::SRT
CPAN shell
perl -MCPAN -e shell install Video::Subtitle::SRT
For more information on module installation, please visit the detailed CPAN module installation guide.