Skip to content

Instantly share code, notes, and snippets.

@sidneys
Created June 7, 2018 20:57
Show Gist options
  • Save sidneys/38401b86a488aeafdec16b5e90e01322 to your computer and use it in GitHub Desktop.
Save sidneys/38401b86a488aeafdec16b5e90e01322 to your computer and use it in GitHub Desktop.
XML (EBU-TT) to SRT Subtitle Converter for MediathekView XML Subtitles
#!/usr/bin/perl
## XML (EBU-TT) to SRT Subtitle Converter for MediathekView XML Subtitles
## forked from mattfoo/ebu-tt_to_srt
## USAGE
## $> perl xml-to-srt.pl subtitle.xml
## $> Written SRT Subtitle: subtitle.xml.srt
use strict;
use warnings;
use utf8;
use File::Basename;
use XML::Simple;
use Data::Dumper;
use POSIX qw{strftime};
# force writing utf8 compatible output
my $src = shift @ARGV;
my $des = basename($src) . ".srt";
# open destination file for writing
open(DES,'>:encoding(UTF-8)',$des) or die $!;
my $input = XMLin($src);
my $lineCount = 1;
print("Reading XML Subtitle: $src\n");
foreach my $data (@{$input->{"tt:body"}->{"tt:div"}->{"tt:p"}}) {
# uncomment for debugging purpose..
# print Dumper($data);
print DES $lineCount . "\n";
(my $begin = $data->{begin}) =~ s/\./\,/;
(my $end = $data->{end}) =~ s/\./\,/;
# ebuttm:documentStartOfProgramme
# 10:00:00:00 or 20:00:00:00
$begin =~ s/^1|^2/0/;
$end =~ s/^1|^2/0/;
print DES $begin . " --> " . $end . "\n";
# check if content has more than one line, which is stored as array
if (ref $data->{"tt:span"} eq 'ARRAY') {
foreach my $contentLine (@{$data->{"tt:span"}}) {
print DES ${$contentLine}{content} . "\n";
}
} else {
print DES $data->{"tt:span"}->{content} . "\n" if ($data->{"tt:span"}->{content});
}
print DES "\n";
$lineCount++;
}
close(DES);
print("Written SRT Subtitle: $des\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment