Last active
August 29, 2015 14:05
-
-
Save parastuffs/d0ba2bb1b5b16ce5daf0 to your computer and use it in GitHub Desktop.
Get an .ass script as argument, extract the chapters and write them in a .txt file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
#use diagnostics; | |
my ($chapters_file, $subtitles_file, $style); | |
my $STYLE = "Chapitre"; | |
sub display_help { | |
my $s = "Usage: $0 [options]\nOptions:\n"; | |
$s = $s."-s script\t\tsubtitles script\n"; | |
$s = $s."-o chaptersFile\t\toutput file\n"; | |
$s = $s."-t style\t\tstyle used in script to mark the chapters\n"; | |
$s = $s."-h\t\t\tdisplay this help\n"; | |
$s = $s." | |
This script parses the given .ass subtitles file in order | |
to extract the time and name of the carefully set chapters in | |
the said script. A chapter is set by creating a new line at | |
the first frame of the chapter, writing its name in the | |
dialogue box and marking the line as \"Comment\". As for the | |
style, the default is \"Chapitre\", but can be fed to this | |
script using the '-t' modifier. | |
If the '-o' modifier is not set, the default output file name | |
is \"<input script base name>_chapters.txt\". | |
\n"; | |
$s = $s."https://gist.github.com/parastuffs/d0ba2bb1b5b16ce5daf0\n"; | |
$s = $s."\n"; | |
print $s; | |
exit; | |
} | |
sub parse_arguments { | |
my (@args) = @_; | |
while (defined($args[-1])) { | |
#Subtitles | |
if (defined($args[-2]) && $args[-2] eq "-s") { | |
$subtitles_file = pop(@args); | |
pop(@args); | |
} | |
#Chapters | |
elsif (defined($args[-2]) && $args[-2] eq "-o") { | |
$chapters_file = pop(@args); | |
pop(@args); | |
} | |
#Style | |
elsif (defined($args[-2]) && $args[-2] eq "-t") { | |
$style = pop(@args); | |
pop(@args); | |
} | |
#Help | |
elsif ($args[-1] eq "-h") { | |
pop(@args); | |
display_help; | |
} | |
else { | |
print "None of the expected arguments has occured.\nPlease | |
consult $0 -h to get help.\n"; | |
exit; | |
} | |
} | |
if(!defined($subtitles_file)) { | |
print "No input file given. I can't do nothing, old sport. '-h' to display the help.\n"; | |
exit; | |
} | |
#Default chapters | |
if(!defined($chapters_file)) { | |
$subtitles_file =~ m/(.*)\.[^\.]*$/; | |
$chapters_file = $1."_chapters.txt"; | |
} | |
#Default style | |
if(!defined($style)) { | |
$style = $STYLE; | |
} | |
} | |
sub parse_chapters { | |
open(INFILE, $subtitles_file); | |
open(OUTFILE, '>>'.$chapters_file); | |
my $i = 1; | |
while(<INFILE>) { | |
chomp; | |
if($_ =~ m/^Comment: [0-9]+,([0-9]:[0-9]{2}:[0-9]{2}\.[0-9]{2}).*(Chapitre),[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,(.*)/) { | |
print OUTFILE "CHAPTER".$i."=0$1\n"; | |
print OUTFILE "CHAPTER".$i."NAME=$3\n"; | |
++$i; | |
} | |
} | |
close(INFILE); | |
close(OUTFILE); | |
} | |
parse_arguments(@ARGV); | |
parse_chapters(); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment