Class: Subtitle
- Inherits:
-
Object
- Object
- Subtitle
- Defined in:
- lib/subtitle.rb
Overview
Facade that wraps all the complexities surrounding which translation engine to use or which caption instances to be instantiated.
Constant Summary collapse
- TYPE_MAP =
{"scc" => AllFather::TYPE_SCC, "srt" => AllFather::TYPE_SRT, "vtt" => AllFather::TYPE_VTT, "ttml" => AllFather::TYPE_TTML, "dfxp" => AllFather::TYPE_DFXP}
Instance Method Summary collapse
- #detect_language(options = nil) ⇒ Object
-
#initialize(file, options = nil) ⇒ Subtitle
constructor
A new instance of Subtitle.
- #transform(types, options = nil, target_lang = nil, src_lang = nil) ⇒ Object
- #translate(dest_lang, src_lang = nil, outfile = nil, options = nil) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(file, options = nil) ⇒ Subtitle
Returns a new instance of Subtitle.
19 20 21 22 23 24 |
# File 'lib/subtitle.rb', line 19 def initialize(file, = nil) # Infer the caption handler from the extension @cc_file = file raise "Input caption not provided. Please provide the same in :cc_file option" if @cc_file.nil? initialize_handler() unless .nil? end |
Instance Method Details
#detect_language(options = nil) ⇒ Object
26 27 28 29 |
# File 'lib/subtitle.rb', line 26 def detect_language( = nil) initialize_handler() if @handler.nil? @handler.infer_languages end |
#transform(types, options = nil, target_lang = nil, src_lang = nil) ⇒ Object
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 |
# File 'lib/subtitle.rb', line 44 def transform(types, = nil, target_lang = nil, src_lang = nil) # A quick validation & translation to expected arguments vals = [] invalid_vals = [] types.each do |type| type_val = TYPE_MAP[type] if type_val.nil? invalid_vals << type next end vals << type_val end unless invalid_vals.empty? raise "Invalid types #{invalid_vals} provided" end # Translator not required if target_lang is nil if @handler.nil? if target_lang.nil? && src_lang.nil? @handler = (, nil) else initialize_handler() end end output_dir = [:outfile] @handler.transform_to(vals, src_lang, target_lang, output_dir) end |
#translate(dest_lang, src_lang = nil, outfile = nil, options = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/subtitle.rb', line 31 def translate(dest_lang, src_lang = nil, outfile = nil, = nil) initialize_handler() if @handler.nil? if outfile.nil? outfile = "#{@cc_file}_#{dest_lang}" end if src_lang.nil? src_lang = detect_language[0] rescue nil raise "Could not detect Source Language!!" if src_lang.nil? end @handler.translate(src_lang, dest_lang, outfile) outfile end |
#type ⇒ Object
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 |
# File 'lib/subtitle.rb', line 71 def type type = nil ccfile = File.open(@cc_file, 'r:UTF-8', &:read) ccfile.each_line do | line | if line =~ /^(\d\d:)\d\d:\d\d[,]\d\d\d.*-->.*(\d\d:)\d\d:\d\d[,]\d\d\d/ type = "srt" elsif line =~ /^((\d\d:)+\d\d[.,]\d\d\d)\s-->\s((\d\d:)+\d\d[.,]\d\d\d)|(^WEBVTT$)/ type = "vtt" elsif line =~ /(^\d\d:\d\d:\d\d:\d\d\t(([0-9a-fA-F]{4})\s)*)+|(^Scenarist_SCC V(\d.\d)$)/ type = "scc" end end unless type doc = File.open(@cc_file) { |f| Nokogiri::XML(f) } namespace = doc.namespaces["xmlns"] if doc.errors.empty? if doc.xpath('/*').first.name == 'tt' && !doc.css('/tt/head').nil? && !doc.css('/tt/body').nil? if namespace =~ /\/ttaf1/ type = "dfxp" elsif namespace =~ /\/ttml/ type = "ttml" end end end end type end |