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

Inherits:
Base
  • Object
show all
Defined in:
lib/flv/edit/processor/meta_data_maker.rb

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 inherited from Base

Base::ALL_EVENTS, Base::EVENT_TRIGGER, Base::EVENT_TRIGGER_LIST, Base::MAIN_EVENTS

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#absorb, #dispatch_chunk, #dispatch_chunks, #dispatch_instead, #each, #has_next_file?, #initialize, #process_all, #process_next_file, #rewind, #stdout, #stop

Constructor Details

This class inherits a constructor from FLV::Edit::Processor::Base

Instance Method Details

#meta_dataObject



53
54
55
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
# File 'lib/flv/edit/processor/meta_data_maker.rb', line 53

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
end

#on_cue_point(t) ⇒ Object



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

def on_cue_point(t)
  @cue_points << t
end

#on_header(header) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/flv/edit/processor/meta_data_maker.rb', line 14

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



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

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

#on_meta_data(t) ⇒ Object



31
32
33
34
# File 'lib/flv/edit/processor/meta_data_maker.rb', line 31

def (t)
  @previous_meta = t
  absorb
end

#on_tag(tag) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/flv/edit/processor/meta_data_maker.rb', line 23

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



36
37
38
# File 'lib/flv/edit/processor/meta_data_maker.rb', line 36

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



48
49
50
51
# File 'lib/flv/edit/processor/meta_data_maker.rb', line 48

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

#total_sizeObject



10
11
12
# File 'lib/flv/edit/processor/meta_data_maker.rb', line 10

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