Module: FLVTool2::Base

Defined in:
lib/flvtool2/base.rb

Class Method Summary collapse

Class Method Details

.add(options, stream, in_path, out_path) ⇒ Object



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
# File 'lib/flvtool2/base.rb', line 59

def add(options, stream, in_path, out_path)
  tag_structures = MiXML.parse( File.open( options[:tag_file], File::RDONLY ) { |file| file.readlines }.join )
  
  add_tag = Proc.new do |data|  
    
    tag = FLV::FLVMetaTag.new
  
    overwrite = ( data['overwrite'] && data['overwrite'].downcase ) == 'true' || false
    data.delete( 'overwrite' )
    
    tag.event = data['event'] || 'event'
    data.delete( 'event' )
    
    tag.timestamp = ( data['timestamp'] && data['timestamp'].to_i ) || 0
    tag.timestamp = stream.find_nearest_keyframe_video_tag(tag.timestamp).timestamp if options[:keyframe_mode] && data['type'] == 'navigation'
    data.delete( 'timestamp' )
    data['time'] = tag.timestamp / 1000
    
    tag..merge!( data )
  
    stream.add_tags( tag, false, overwrite )
  end
  
  tag_structures['tags'].each do |tag_name, value|
    case tag_name
    when 'metatag'
      if value.class == Array
        value.each { |_value| add_tag.call( _value ) } 
      else
        add_tag.call( value )
      end
    else
    end
  end
  
  return true
end

.add_meta_data_tag(stream, options) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/flvtool2/base.rb', line 177

def (stream, options)
  # add onLastSecond tag
  onlastsecond = FLV::FLVMetaTag.new
  onlastsecond.event = 'onLastSecond'
  onlastsecond.timestamp = ((stream.duration - 1) * 1000).to_int
  stream.add_tags(onlastsecond, false) if onlastsecond.timestamp >= 0
  
  stream.add_meta_tag({ 'metadatacreator' => options[:metadatacreator], 'metadatadate' => Time.now }.merge(options[:metadata]))
  unless options[:compatibility_mode]
    stream..['duration'] += (stream.frame_sequence || 0) / 1000.0
  end
end

.after_print(options) ⇒ Object



172
173
174
# File 'lib/flvtool2/base.rb', line 172

def after_print(options)
  puts '</fileset>' if options[:xml]
end

.before_print(options) ⇒ Object



148
149
150
# File 'lib/flvtool2/base.rb', line 148

def before_print(options)
  puts "<?xml version=\"1.0\"?>\n<fileset>" if options[:xml]
end

.create_directories_for_path(path_to_build) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/flvtool2/base.rb', line 275

def create_directories_for_path(path_to_build)
  parts = path_to_build.split(File::SEPARATOR)
  parts.shift #removes '/' or 'c:\'
  parts.pop # removes filename
  
  parts.inject('') do |path, dir|
    begin
      path += File::SEPARATOR + dir
      Dir.mkdir path
    rescue Object => e
      raise e unless File.directory?(path)
    end
    path
  end
end

.cut(options, stream, in_path, out_path) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/flvtool2/base.rb', line 118

def cut(options, stream, in_path, out_path)
  
  in_point = options[:keyframe_mode] ? stream.find_nearest_keyframe_video_tag(options[:in_point] || 0).timestamp : options[:in_point]
  stream.cut( :in_point => in_point, :out_point => options[:out_point], :collapse => options[:collapse] )
  
  return true
end

.debug(options, stream, in_path, out_path) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/flvtool2/base.rb', line 98

def debug(options, stream, in_path, out_path)
  
  puts "---\n"
  puts "path: #{in_path}"

  unless stream.nil?
    if options[:tag_number]
      puts "  tag_number: #{options[:tag_number]}"
      puts "  " + stream.tags[ options[:tag_number] - 1 ].inspect.join( "\n  " )
    else
      stream.tags.each_with_index do |tag, index|
        puts "##{index + 1} #{tag.info}"
      end
    end
  end
  
  return false
end

.execute!(options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/flvtool2/base.rb', line 37

def execute!(options)
  
  options[:commands].each do |command|
    before_filter = "before_#{command.to_s}".intern
    send(before_filter, options) if respond_to? before_filter
  end
  
  process_files(options) do |stream, in_path, out_path|
    write_stream = false
    options[:commands].each do |command|
      write_stream = true if send( command, options, stream, in_path, out_path )
    end
    stream.write if write_stream && !options[:simulate]
  end
  
  options[:commands].each do |command|
    after_filter = "after_#{command.to_s}".intern
    send(after_filter, options) if respond_to? after_filter
  end
end

.object_to_hash(object) ⇒ Object



165
166
167
168
169
170
# File 'lib/flvtool2/base.rb', line 165

def object_to_hash(object)
  object.instance_variables.inject( {} ) do |hash, variable|
    hash[variable.gsub('@', '')] = object.instance_variable_get( variable.intern )
    hash
  end
end

.open_stream(in_path, out_path = nil, stream_log = false) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/flvtool2/base.rb', line 253

def open_stream(in_path, out_path = nil, stream_log = false)
  attributes = (RUBY_PLATFORM =~ /win32/) ? File::BINARY : 0
  
  if in_path == 'pipe'
    in_stream = $stdin
  elsif in_path == out_path || out_path.nil?
    in_stream = File.open( in_path, File::RDWR|attributes )
  else
    in_stream = File.open( in_path, File::RDONLY|attributes )
  end
  
  if out_path == 'pipe' || ( in_path == 'pipe' && out_path.nil? )
    out_stream = $stdout
  elsif in_path != out_path && !out_path.nil?
    out_stream = File.open( out_path, File::CREAT|File::WRONLY|attributes )
  else
    out_stream = nil
  end
  
  FLV::FLVStream.new( in_stream, out_stream, stream_log )
end


152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/flvtool2/base.rb', line 152

def print(options, stream, in_path, out_path)
  
  if options[:xml]
    puts "  <flv name=\"#{in_path}\">"
    puts MiXML.dump( stream. && stream.., 2 )
    puts "  </flv>"
  else
    puts MiYAML.dump( { in_path => ( stream. && stream.. ) } )
  end
  
  return false
end

.process_files(options) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/flvtool2/base.rb', line 193

def process_files(options)
  
  if options[:in_pipe]
  
    unless options[:omit_out] || options[:out_path].nil?
      create_directories_for_path( options[:out_path] )
      out_path = options[:out_path]
    else
      out_path = nil
    end
    
    begin
      stream = open_stream( 'pipe', out_path )
      yield stream, 'pipe', out_path
      begin
        stream.close
      rescue
      end
      
    rescue Exception => e
      show_exception(e, options)
    end
    
  else
  
    if File.directory?( options[:in_path] )
      pattern = options[:recursive] ? "#{File::SEPARATOR}**#{File::SEPARATOR}*.flv" : "#{File::SEPARATOR}*.flv"
      file_names = Dir[options[:in_path] + pattern]
    else
      file_names = [options[:in_path]]
    end
    
    file_names.each do |in_path|
  
      if options[:out_pipe]
        out_path = 'pipe'
      else
        out_path = options[:out_path]
        unless options[:out_path].nil?
          out_path = in_path.gsub( options[:in_path], options[:out_path] )
          create_directories_for_path( out_path )
        end
      end
      
      begin
        stream = open_stream( in_path, out_path, options[:stream_log] )
        yield stream, in_path, out_path
  
        begin
          stream.close
        rescue
        end
        
      rescue Exception => e
        show_exception(e, options)
      end
    end
  end
end

.show_exception(e, options) ⇒ Object



291
292
293
294
# File 'lib/flvtool2/base.rb', line 291

def show_exception(e, options)
  puts "ERROR: #{e.message}\nERROR: #{e.backtrace.join("\nERROR: ")}"
  puts "Skipping file #{options[:in_path]}\n" if options[:verbose]
end

.update(options, stream, in_path, out_path) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/flvtool2/base.rb', line 127

def update(options, stream, in_path, out_path)
  
  if ( 
        options[:preserve] && 
        ( 
          !stream. ||
          ( stream. && stream..['metadatacreator'] != options[:metadatacreator] ) 
        ) 
     ) || !options[:preserve]
    
    ( stream, options )
  
    return true
  else
    puts 'Input file is FLV v1.1 yet. No update necessary.' if options[:verbose]
    
    return false
  end
end