Class: SCC

Inherits:
Object
  • Object
show all
Includes:
AllFather, CommonUtils
Defined in:
lib/scc.rb

Overview

Library to handle SCC Files

Uses the translator available to do the necessary language operations as defined by the AllFather

Constant Summary collapse

SUPPORTED_TRANSFORMATIONS =
[TYPE_SRT, TYPE_VTT, TYPE_TTML, TYPE_DFXP]

Constants included from CommonUtils

CommonUtils::CREDITS, CommonUtils::SCC_DEFAULT_FRAME_RATE

Constants included from AllFather

AllFather::TYPE_DFXP, AllFather::TYPE_SCC, AllFather::TYPE_SRT, AllFather::TYPE_TTML, AllFather::TYPE_VTT, AllFather::VALID_FILES

Instance Method Summary collapse

Methods included from CommonUtils

#create_file, #extension_from_type, #new_cue, #scc_encode, #time_details, #write_cue

Methods included from AllFather

#callsign

Constructor Details

#initialize(cc_file) ⇒ SCC

Returns a new instance of SCC.



19
20
21
22
# File 'lib/scc.rb', line 19

def initialize(cc_file)
  @cc_file = cc_file
  raise "Invalid SCC file provided" unless is_valid?
end

Instance Method Details

#infer_languagesObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/scc.rb', line 37

def infer_languages
  lang = nil
  begin
    sample_text = get_text(@cc_file, 100)
    lang = @translator.infer_language(sample_text)
  rescue StandardError => e
    puts "Error while detecting the language due to #{e.message}"
  end
  [lang]
end

#is_valid?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
# File 'lib/scc.rb', line 24

def is_valid?
  # Do any SCC specific validations here
  if @cc_file =~ /^.*\.(scc)$/
    return true
  end
  return false
end

#set_translator(translator) ⇒ Object



32
33
34
35
# File 'lib/scc.rb', line 32

def set_translator(translator)
  super(translator)
  @translator = translator
end

#supported_transformationsObject



52
53
54
# File 'lib/scc.rb', line 52

def supported_transformations
  return SUPPORTED_TRANSFORMATIONS
end

#transform_to(types, src_lang, target_lang, output_dir) ⇒ Object



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
# File 'lib/scc.rb', line 56

def transform_to(types, src_lang, target_lang, output_dir)
  # Let's start off with some validations
  super(types, src_lang, target_lang, output_dir)

  # Suffix output dir with File seperator
  output_dir = "#{output_dir}#{File::Separator}" unless output_dir.end_with?(File::Separator)
  
  # Prepare the output files for each type
  file_map = {}
  types.each do |type|
    output_file = File.basename(@cc_file, File.extname(@cc_file)) + extension_from_type(type)
    out_file = "#{output_dir}#{output_file}"
    if create_file(TYPE_SCC, type, out_file, target_lang)
      file_map[type] = out_file
    else
      raise StandardError.new("Failed to create output file for type #{type}")
    end
  end

  # Read the file and prepare the cue model
  prev_cue_info = cur_cue_info = nil
  ccfile = File.open(@cc_file, 'r:UTF-8', &:read)
  cue_index = 1
  ccfile.each_line do | line |
    time_point = line.scan(/(^\d\d:\d\d:\d\d:\d\d\s)(.*)/)
    unless time_point.empty?
      scc_text_code = time_point[0][1].strip
      message = decode(scc_text_code)
      # Replace \u0000 with empty as this causes the ttml / dfxp outputs
      # to treat them as end and terminates the xml the moment this is encountered
      # https://github.com/sparklemotion/nokogiri/issues/1535
      message = message.gsub(/\u0000/, '')
      if prev_cue_info.nil?
        prev_cue_info = CueInfo.new(TYPE_SCC)
        prev_cue_info.index = cue_index
        prev_cue_info.message = message
        prev_cue_info.start = time_point[0][0].strip
      else
        cur_cue_info = CueInfo.new(TYPE_SCC)
        cur_cue_info.index = cue_index
        cur_cue_info.message = message
        cur_cue_info.start = time_point[0][0].strip
        # Set the previous cue info's end time to current cue's start time
        # TODO: Need to see if we need to reduce alteast 1 fps or 1s
        prev_cue_info.end = cur_cue_info.start
        prev_cue_info.start_time_units = time_details(prev_cue_info.start, TYPE_SCC)
        prev_cue_info.end_time_units = time_details(prev_cue_info.end, TYPE_SCC)
        write_cue(prev_cue_info, file_map)
        prev_cue_info = cur_cue_info
      end
      cue_index += 1
    end
  end
  # we need to set some end time, but don't know the same !!
  # for now setting the start time itself
  cur_cue_info.end = cur_cue_info.start 
  cur_cue_info.start_time_units = time_details(cur_cue_info.start, TYPE_SCC)
  cur_cue_info.end_time_units = time_details(cur_cue_info.end, TYPE_SCC)
  write_cue(cur_cue_info, file_map, true)
end

#translate(src_lang, dest_lang, out_file) ⇒ Object



48
49
50
# File 'lib/scc.rb', line 48

def translate(src_lang, dest_lang, out_file)
  raise "Not Implemented. Class #{self.class.name} doesn't implement translate yet !!"
end