-
Notifications
You must be signed in to change notification settings - Fork 2
/
sub2eaf.pl
executable file
·188 lines (152 loc) · 4.43 KB
/
sub2eaf.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/perl
# Convert movie subtitles from SRT format and MicroDVD .sub format to
# ELAN annotations (EAF file format).
# Please note there's more than one subtitle format that uses the .sub
# extension. If it's not MicroDVD format, it will certainly not work
# with this script.
# [email protected], 2010
use strict;
# --- Read input ---
if ($#ARGV < 0) {
die "Usage: perl sub2eaf.pl subtitlefile [framerate]";
}
my $infile=$ARGV[0];
open IF,$infile || die "Failed to open file";
my @lines=<IF>;
chomp @lines;
close IF;
my $framerate=25;
if ($#ARGV > 0) {
$framerate=$ARGV[1];
}
my $prefix=$infile;
$prefix =~ s/\.[^\.]*$//;
my $vid_url="file:https://$prefix.mp4";
my $wav_url="file:https://$prefix.wav";
# --- Start file (print header) ---
preamble();
# --- Process data and print list of timeslots ---
my @contents;
my @ids;
my $counter=1;
if ($infile =~ /\.sub/) {
process_subfile();
} elsif ($infile =~ /\.srt/) {
process_srtfile();
} else {
die "Unknown file extension.";
}
# --- Print list of annotations ---
print " <TIER TIER_ID=\"subtitle\" LINGUISTIC_TYPE_REF=\"utterance\" DEFAULT_LOCALE=\"en\">\n";
$counter=1;
while ($#contents>=0) {
my $content=shift(@contents);
my $id="a$counter";
my $start=shift(@ids);
my $end=shift(@ids);
print " <ANNOTATION>\n";
print " <ALIGNABLE_ANNOTATION ANNOTATION_ID=\"$id\" TIME_SLOT_REF1=\"$start\" TIME_SLOT_REF2=\"$end\">\n";
print " <ANNOTATION_VALUE>$content</ANNOTATION_VALUE>\n";
print " </ALIGNABLE_ANNOTATION>\n";
print " </ANNOTATION>\n";
$counter++;
}
print " </TIER>\n";
# --- End file ---
endmatter();
# ----- subroutines -----
# Convert frame count to milliseconds.
sub frame2ms {
my $fc=shift;
return int($fc * 1000/$framerate);
}
# Convert "hh:mm:ss,ms" to milliseconds.
sub hms2ms {
my $t=shift;
my @fields=split(/[:,]/,$t);
return strip0($fields[3])
+1000*strip0($fields[2])
+60*1000*strip0($fields[1])
+60*60*1000*strip0($fields[0]);
}
sub strip0 {
my ($in)=@_;
$in =~ s/^0+//;
return $in;
}
# Read in subtitles in MicroDVD .sub format.
sub process_subfile {
print " <TIME_ORDER>\n";
foreach my $line(@lines) {
# Take off CRLF endline. This is nasty and should do a proper
# check.
chop $line;
$line =~ /{(.*)}{(.*)}(.*)/;
my $start=$1;
my $end=$2;
my $content=$3;
$content =~ s/\|/ /g;
my $id="ts$counter";
print_timeslot(frame2ms($start));
print_timeslot(frame2ms($end));
push @contents, $content;
}
print " </TIME_ORDER>\n";
}
# Read in subtitles in SRT format.
sub process_srtfile {
print " <TIME_ORDER>\n";
my ($start, $end, $content);
foreach my $line(@lines) {
# Take off CRLF endline. This is nasty and should do a proper
# check.
chop $line;
if ($line eq "") {
# Empty line: one record is ready, save it.
print_timeslot(hms2ms($start));
print_timeslot(hms2ms($end));
push @contents, $content;
$start="";
$end="";
$content="";
} elsif ($start eq "") {
# Extract start and end times.
next unless $line =~ /(.*) --> (.*)/;
$start=$1;
$end=$2;
} else {
# Save the content. Remember it may span multiple lines.
if ($content eq "") {
$content=$line;
} else {
$content .= " ".$line;
}
}
}
print " </TIME_ORDER>\n";
}
# Print time slot and store the time slot id in @ids.
sub print_timeslot {
my ($time)=@_;
my $id="ts$counter";
print " <TIME_SLOT TIME_SLOT_ID=\"$id\" TIME_VALUE=\"",
int($time), "\"/>\n";
push @ids, $id;
$counter++;
}
sub preamble {
print <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<ANNOTATION_DOCUMENT xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://www.mpi.nl/tools/elan/EAFv2.5.xsd" DATE="2008-03-19T16:07:44+00:00" AUTHOR="" VERSION="2.5" FORMAT="2.5">
<HEADER MEDIA_FILE="" TIME_UNITS="milliseconds">
<MEDIA_DESCRIPTOR MEDIA_URL="$vid_url" MIME_TYPE="video/mpeg" RELATIVE_MEDIA_URL="$vid_url"/>
<!-- <MEDIA_DESCRIPTOR MEDIA_URL="$wav_url" MIME_TYPE="audio/x-wav" RELATIVE_MEDIA_URL="$wav_url" EXTRACTED_FROM="file:https://elan-example1.mpg"/> -->
<PROPERTY NAME="lastUsedAnnotationId">316</PROPERTY>
</HEADER>
EOF
}
sub endmatter {
print ' <LINGUISTIC_TYPE LINGUISTIC_TYPE_ID="utterance" TIME_ALIGNABLE="true" GRAPHIC_REFERENCES="false"/>', "\n";
print ' <LOCALE LANGUAGE_CODE="en" COUNTRY_CODE="US"/>', "\n";
print "</ANNOTATION_DOCUMENT>\n";
}