Class: Subconv::CaptionFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/subconv/caption_filter.rb

Overview

Apply post-processing to captions Supported operations:

  • remove color nodes

  • remove flash nodes

  • convert XY viewport relative positions to simple top or bottom positions

  • merge multiple captions on screen at the same time into one caption

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CaptionFilter

Returns a new instance of CaptionFilter.



13
14
15
16
17
18
# File 'lib/subconv/caption_filter.rb', line 13

def initialize(options)
  @options = options
  @options[:filter_node_types] ||= []
  @options[:filter_node_types].push ColorNode if @options[:remove_color]
  @options[:filter_node_types].push FlashNode if @options[:remove_flash]
end

Instance Method Details

#process!(captions) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
70
71
# File 'lib/subconv/caption_filter.rb', line 20

def process!(captions)
  last_timespan = nil
  open_captions = {}
  last_top_position = nil

  captions.map! do |caption|
    is_same_timespan = last_timespan == caption.timespan

    unless is_same_timespan
      # Time changed -> do not compare with previous top position
      last_top_position = nil
      open_captions = {}
    end

    if @options[:xy_position_to_top_or_bottom]
      caption.position = if caption.position.y < 0.5
                           last_top_position = caption.position.y
                           :top
                         elsif !last_top_position.nil? && (caption.position.y - last_top_position) < 0.08
                           # Do not move lines to the bottom when they are continuing directly
                           # from a caption displayed at the top to avoid continuous captions
                           # being thorn in half
                           last_top_position = caption.position.y
                           :top
                         else
                           :bottom
                         end

      # x position is removed altogether and the caption is now center-aligned
      caption.align = :middle
    end

    # Captions are expected to be ordered by increasing timespan and y position (this is
    # guaranteed for the SCC reader)
    if @options[:merge_by_position] && is_same_timespan && open_captions.key?(caption.position)
      target_caption = open_captions[caption.position]
      target_caption.content.children.push TextNode.new("\n")
      target_caption.content.children.concat caption.content.children
      # Remove this caption since it has been merged
      next
    end

    last_timespan = caption.timespan

    open_captions[caption.position] = caption
    caption
  end
  # Remove nils resulting from removed captions
  captions.compact!
  # Do per-caption processing after merging etc.
  captions.each(&method(:process_caption!))
end

#process_caption!(caption) ⇒ Object



73
74
75
76
# File 'lib/subconv/caption_filter.rb', line 73

def process_caption!(caption)
  filter_nodes! caption.content
  merge_text_nodes! caption.content
end