Class: FLV::Edit::Processor::MetaDataMaker

Inherits:
Base
  • Object
show all
Includes:
Dispatcher
Defined in:
lib/flvedit/processor/meta_data_maker.rb

Overview

MetaDataMaker is a Processor class (see Base) that computes the metadata for its sources. The metadata for the current source is accessible with #meta_data It is used by Update.

Defined Under Namespace

Classes: Info

Constant Summary collapse

CHUNK_LENGTH_SIZE =

todo: calc instead?

4
TAG_HEADER_SIZE =

todo: calc instead?

11
TOTAL_EXTRA_SIZE_PER_TAG =
CHUNK_LENGTH_SIZE + TAG_HEADER_SIZE

Constants included from Dispatcher

Dispatcher::ALL_EVENTS, Dispatcher::EVENT_TRIGGER, Dispatcher::EVENT_TRIGGER_LIST, Dispatcher::MAIN_EVENTS

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods included from Dispatcher

#absorb, #dispatch_instead, #each, included, #initialize, #stop

Methods inherited from Base

#clone, #each, #each_source, #initialize, #process_all

Instance Method Details

#meta_dataObject



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
116
117
# File 'lib/flvedit/processor/meta_data_maker.rb', line 57

def 
  frame_sequence_in_ms = @video_interval_stats.index(@video_interval_stats.values.max)
  last_ts = @info.values.map{|h| h.last}.compact.map(&:timestamp).map(&:in_seconds).max
  duration = last_ts + frame_sequence_in_ms/1000.0

  meta = @info[Video].last.body.dimensions if @info[Video].last
  meta ||= {:width => @previous_meta[:width], :height => @previous_meta[:height]} if @previous_meta
  meta ||= {}
  [Video, Audio].each do |k|
    h = @info[k]
    type = k.name.gsub!("FLV::","").downcase

    meta[:"#{type}size"] = h.bytes + h.tag_count * TAG_HEADER_SIZE
    meta[:"#{type}datarate"] = h.bytes / duration * 8 / 1000 #kbits/s
    if meta[:"has#{type.capitalize}"] = h.first != nil
      meta[:"#{type}codecid"] = h.first.codec_id
    end
  end
        
  meta.merge!(
    :filesize               => total_size,
    :datasize               => total_size - @info[Header].bytes - CHUNK_LENGTH_SIZE*(1 + @info.values.sum(&:tag_count)),
    :lasttimestamp          => last_ts,
    :framerate              => frame_sequence_in_ms ? 1000.0/frame_sequence_in_ms : 0,
    :duration               => duration,

    :hasCuePoints           => !@cue_points.empty?,
    :cuePoints              => @cue_points.map(&:body),
    :hasKeyframes           => !@key_frames.empty?,
    :keyframes              => time_positions_to_hash(@key_frames),

    :hasMetadata            => true, #duh! Left for compatibility with FLVTool2, but I can't believe anyone would actually check on this...
    :metadatadate           => Time.now,
    :metadatacreator        => 'flvedit........................................................'
  )

  meta.merge!(
    :audiodelay             => @info[Video].first.timestamp.in_seconds,
    :canSeekToEnd           => @info[Video].last.frame_type == :keyframe,
    :lastkeyframetimestamp  => @key_frames.last.first || 0
  ) if meta[:hasVideo]
  
  meta.merge!(
    :stereo                 => @info[Audio].first.channel == :stereo,
    :audiosamplerate        => @info[Audio].first.rate,
    :audiosamplesize        => @info[Audio].first.sample_size
  ) if meta[:hasAudio]

  # Adjust all sizes for this new meta tag
  meta_size = Tag.new(0, meta).size
  # p "Size of meta: #{meta_size}"
  # p "Info: #{@info.values.map(&:bytes).inspect} #{@info.values.map(&:tag_count).inspect}"
  meta[:filesize] += meta_size
  meta[:datasize] += meta_size
  meta[:keyframes][:filepositions].map! {|ts| ts + meta_size} if meta[:hasKeyframes]
  meta[:cuePoints][:filepositions].map! {|ts| ts + meta_size} if meta[:hasCuePoints]

  meta.merge!(YAML.load_file(options[:update]).symbolize_keys!) if options[:update]

  meta
end

#on_cue_point(t) ⇒ Object



48
49
50
# File 'lib/flvedit/processor/meta_data_maker.rb', line 48

def on_cue_point(t)
  @cue_points << t
end

#on_header(header) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/flvedit/processor/meta_data_maker.rb', line 18

def on_header(header)
  @cue_points = []
  @key_frames = []
  @video_interval_stats = Hash.new(0)
  @info = Hash.new{|h, key| h[key] = Info.new(0,0,nil,nil)}
  @info[Header].bytes = header.size
  # Header is not a tag, so leave tag_count at 0
end

#on_keyframe(t) ⇒ Object



44
45
46
# File 'lib/flvedit/processor/meta_data_maker.rb', line 44

def on_keyframe(t)
  @key_frames << [t.timestamp.in_seconds, self.total_size]
end

#on_meta_data(t) ⇒ Object



35
36
37
38
# File 'lib/flvedit/processor/meta_data_maker.rb', line 35

def (t)
  @previous_meta = t
  absorb
end

#on_tag(tag) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/flvedit/processor/meta_data_maker.rb', line 27

def on_tag(tag)
  h = @info[tag.body.class]
  h.first ||= tag
  h.last = tag
  h.bytes += tag.body.size
  h.tag_count += 1
end

#on_video(t) ⇒ Object



40
41
42
# File 'lib/flvedit/processor/meta_data_maker.rb', line 40

def on_video(t)
  @video_interval_stats[t.timestamp.in_milliseconds - @info[Video].last.timestamp.in_milliseconds] += 1   if @info[Video].last
end

#time_positions_to_hash(time_position_pairs) ⇒ Object



52
53
54
55
# File 'lib/flvedit/processor/meta_data_maker.rb', line 52

def time_positions_to_hash(time_position_pairs)
  times, filepositions = time_position_pairs.transpose
  {:times => times || [], :filepositions => filepositions || []}
end

#total_sizeObject



14
15
16
# File 'lib/flvedit/processor/meta_data_maker.rb', line 14

def total_size
  @info.values.sum{|h| h.bytes + h.tag_count * TOTAL_EXTRA_SIZE_PER_TAG}
end