| 1 | package Parse::SSA; | ||||||||||||||
| 2 | { | ||||||||||||||
| 3 | 1 | $Parse::SSA::VERSION = '1.0'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | |||||||||||||||
| 6 | =pod | ||||||||||||||
| 7 | =head NAME | ||||||||||||||
| 8 | Parse::SSA = ASS/SSA subtitle parser | ||||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||
| 11 | |||||||||||||||
| 12 | version 1.0 | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||
| 15 | |||||||||||||||
| 16 | # Simple parsing all texts subtitle | ||||||||||||||
| 17 | my $sub = Parse::SSA->new("./sub.ass"); | ||||||||||||||
| 18 | my $row = $sub->subrow(); | ||||||||||||||
| 19 | print($row->[5]->{'subtitle'}); | ||||||||||||||
| 20 | # subtitle text on 5 line. | ||||||||||||||
| 21 | |||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||
| 23 | |||||||||||||||
| 24 | This module can parse text, time, comments, names from SSA/ASS subtitle fromat. | ||||||||||||||
| 25 | |||||||||||||||
| 26 | =head1 AUTHOR | ||||||||||||||
| 27 | Konstantin Gromyko E<lt>disinterpreter@protonmail.ch<gt> | ||||||||||||||
| 28 | |||||||||||||||
| 29 | =head1 LICENSE | ||||||||||||||
| 30 | |||||||||||||||
| 31 | This library is free software; you can redistribute it and/or modify | ||||||||||||||
| 32 | it under the same terms as Perl itself. | ||||||||||||||
| 33 | |||||||||||||||
| 34 | =head1 SEE ALSO | ||||||||||||||
| 35 | |||||||||||||||
| 36 | L<https://en.wikipedia.org/wiki/SubStation_Alpha> L<https://www.matroska.org/technical/specs/subtitles/ssa.html> | ||||||||||||||
| 37 | |||||||||||||||
| 38 | |||||||||||||||
| 39 | =cut | ||||||||||||||
| 40 | #use `5.020_000`; | ||||||||||||||
| 41 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | 1 | my @lines; | |||||||||||||
| 44 | 1 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||
| 45 | 1 | my ($pkg, $filename) = @_; | |||||||||||||
| 46 | 1 | my $lineNum = 0; | |||||||||||||
| 47 | 1 | my $self = {}; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 1 | my @assfile = (); | |||||||||||||
| 50 | 1 | ▶ | open( ASSFILE, "<", $filename ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | or die 'Can not open ".ass" source file:' . $filename . ", please check it!!\n"; | ||||||||||||||
| 52 | 1 | ▶ | while (<ASSFILE>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 56 | chomp; | |||||||||||||
| 54 | 56 | ▶ | if (m/^Dialogue:/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 55 | 40 | $lineNum = extractLine( $lineNum, $_ ); | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | elsif (m/^(Title:|Original)/) { | ||||||||||||||
| 58 | # If a line begins with 'Title' or 'Original', it is the source for the subtitles | ||||||||||||||
| 59 | |||||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | } | ||||||||||||||
| 63 | 1 | $self->{'fname'} = $filename; | |||||||||||||
| 64 | 1 | $self->{'assall'} = \@lines; | |||||||||||||
| 65 | 1 | bless($self, $pkg); | |||||||||||||
| 66 | |||||||||||||||
| 67 | 1 | return $self; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | =head2 subrow | ||||||||||||||
| 71 | |||||||||||||||
| 72 | # Get all parsed data from subtitle | ||||||||||||||
| 73 | my $row = $sub->subrow(); | ||||||||||||||
| 74 | |||||||||||||||
| 75 | The C<subrow> method gets the data which are present in a Dialogue section: begin time, end time, comment flag, character name, subtitles. | ||||||||||||||
| 76 | =cut | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 1 | ▶ | sub subrow { | ||||||||||||
|
Subroutine
subrow: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 79 | 1 | my ($self) = @_; | |||||||||||||
| 80 | 1 | return $self->{'assall'}; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 40 | ▶ | sub extractLine { | ||||||||||||
|
Subroutine
extractLine: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||
| 84 | 40 | my ($lineNumber, $content) = @_; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 40 | my $begin; | |||||||||||||
| 87 | my $end; | ||||||||||||||
| 88 | 40 | my $subtitle; | |||||||||||||
| 89 | 40 | my $name; | |||||||||||||
| 90 | 40 | my $currentTime; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 40 | ▶ | if ( $content | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | =~ m/Dialogue: [^,]*,([^,]*),([^,]*),([^,]*),([^,]*),[^,]*,[^,]*,[^,]*,[^,]*,(.*)$/ | ||||||||||||||
| 94 | ) | ||||||||||||||
| 95 | { | ||||||||||||||
| 96 | 40 | $begin = $1; | |||||||||||||
| 97 | 40 | $end = $2; | |||||||||||||
| 98 | 40 | $name = $4; | |||||||||||||
| 99 | 40 | $subtitle = $5; | |||||||||||||
| 100 | |||||||||||||||
| 101 | 40 | my $isComment = $3; | |||||||||||||
| 102 | 40 | $begin =~ s/\./,/g; | |||||||||||||
| 103 | 40 | $end =~ s/\./,/g; | |||||||||||||
| 104 | 40 | ▶ | if ( $begin =~ m/^\d{1}:/ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 40 | $begin = "0" . $begin; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 40 | ▶ | if ( $end =~ m/^\d{1}:/ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | 40 | $end = "0" . $end; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | 40 | $subtitle =~ s/\r$//g; | |||||||||||||
| 112 | 40 | $subtitle =~ s/\\N/ /g; | |||||||||||||
| 113 | 40 | $subtitle =~ s/^m\s+\d+.+//g; | |||||||||||||
| 114 | 40 | $subtitle =~ s/{[^}]*}//g; | |||||||||||||
| 115 | |||||||||||||||
| 116 | 40 | ▶ | if ( $isComment eq 'comment' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | 0 | $subtitle = '(' . $subtitle . ')'; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | 40 | my %line = ( begin => $begin, end => $end, isComment => $isComment, name=>$name, subtitle => $subtitle ); | |||||||||||||
| 120 | 40 | push @lines, \%line; | |||||||||||||
| 121 | |||||||||||||||
| 122 | 40 | return $lineNumber; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 1 | 1; | |||||||||||||