Skip to content

Instantly share code, notes, and snippets.

@kav2k
Last active December 29, 2015 06:09
Show Gist options
  • Save kav2k/7626858 to your computer and use it in GitHub Desktop.
Save kav2k/7626858 to your computer and use it in GitHub Desktop.
Gamers 3 subtitle translation converter. Usage: 1) Download the worksheet as plain text (will result in a TSV, Tab-Separated Values file) 2) Feed the file, with the column name containing the required language, to the script, example: `perl g3sub.pl E < worksheet.tsv > subtitles.srt` will produce English subtitles
#!perl -w
#
# Gamers 3 Subtitle File processing
# (c) Alexander Kashev, 2013
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Usage: perl g3sub.pl (column name) < subtitles.tsv > subtitles.srt
# I.e. "perl g3sub.pl K < subtitles.tsv > subtitles.srt" will result in subtitles from column K
# TSV-file is obtained from downloading the Google Documents worksheet as "plain text"
#
$fps = 25; # Video stream FPS (required, since STL format is time AND frame based)
$offset = 0; # Subtitle offset in ms
$scale = 7206.5/7200.0; # For some reason there's a slight scaling with regards to subtitles, but less than the FPS difference
die "Missing column name argument\n" if (@ARGV < 1);
while(<STDIN>){
my @cols = split "\t";
next if ($. == 1); # Header row
my @data = ( @cols[0,2,col_by_name($ARGV[0])] );
output(($.-1), @data);
# last if ($. > 10); # Debug
}
sub col_by_name {
my($name) = @_;
die "Column name should be a single uppercase letter\n" unless ($name =~ /^[A-Z]$/);
return ord($name)-65;
}
# STL -> SRT conversion
sub output {
my ($num, $startTS, $endTS, $rawText) = @_;
my ($start, $startFrame, $end, $endFrame);
if($startTS =~ /(\d\d:\d\d\:\d\d):(\d\d)/){
($start, $startFrame) = ($1,$2);
}
if($endTS =~ /(\d\d:\d\d\:\d\d):(\d\d)/){
($end, $endFrame) = ($1,$2);
}
$text = preprocess($rawText);
print "$num\n";
print time_to_SRT( int( STL_to_time($start, $startFrame) * $scale + $offset ));
print " --> ";
print time_to_SRT( int( STL_to_time($end, $endFrame) * $scale + $offset ));
print "\n";
print $text;
print "\n\n";
}
sub STL_to_time {
my ($timestamp, $frame) = @_;
$duration = (1.0 / $fps) * 1000; # Frame duration in ms
if($timestamp =~ /(\d\d):(\d\d):(\d\d)/) {
return int($1*60*60*1000 + $2*60*1000 + $3*1000 + $frame*$duration + 0.5);
}
else {
die "Cannot parse STL timestamp: $timestamp:$frame\n";
}
}
sub time_to_SRT {
my ($time) = @_;
my ($hour,$minute,$second);
($hour, $time) = ( int($time / (60*60*1000)), $time % (60*60*1000) );
($minute, $time) = ( int($time / (60*1000)), $time % (60*1000) );
($second, $time) = ( int($time / 1000), $time % 1000 );
return sprintf("%02d:%02d:%02d,%03d", $hour, $minute, $second, $time);
}
sub preprocess {
my ($text) = @_;
$text =~ s/\s*\|\s*/\n/;
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment