Module: FLVTool2::Base
- Defined in:
- lib/flvtool2/base.rb
Class Method Summary collapse
- .add(options, stream, in_path, out_path) ⇒ Object
- .add_meta_data_tag(stream, options) ⇒ Object
- .after_print(options) ⇒ Object
- .before_print(options) ⇒ Object
- .create_directories_for_path(path_to_build) ⇒ Object
- .cut(options, stream, in_path, out_path) ⇒ Object
- .debug(options, stream, in_path, out_path) ⇒ Object
- .execute!(options) ⇒ Object
- .object_to_hash(object) ⇒ Object
- .open_stream(in_path, out_path = nil, stream_log = false) ⇒ Object
- .print(options, stream, in_path, out_path) ⇒ Object
- .process_files(options) ⇒ Object
- .show_exception(e, options) ⇒ Object
- .update(options, stream, in_path, out_path) ⇒ Object
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(, stream, in_path, out_path) tag_structures = MiXML.parse( File.open( [: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. = ( data['timestamp'] && data['timestamp'].to_i ) || 0 tag. = stream.find_nearest_keyframe_video_tag(tag.). if [:keyframe_mode] && data['type'] == 'navigation' data.delete( 'timestamp' ) data['time'] = tag. / 1000 tag..merge!( data ) stream.( 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, ) # add onLastSecond tag onlastsecond = FLV::FLVMetaTag.new onlastsecond.event = 'onLastSecond' onlastsecond. = ((stream.duration - 1) * 1000).to_int stream.(onlastsecond, false) if onlastsecond. >= 0 stream.({ 'metadatacreator' => [:metadatacreator], 'metadatadate' => Time.now }.merge([:metadata])) unless [: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() puts '</fileset>' if [:xml] end |
.before_print(options) ⇒ Object
148 149 150 |
# File 'lib/flvtool2/base.rb', line 148 def before_print() puts "<?xml version=\"1.0\"?>\n<fileset>" if [: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(, stream, in_path, out_path) in_point = [:keyframe_mode] ? stream.find_nearest_keyframe_video_tag([:in_point] || 0). : [:in_point] stream.cut( :in_point => in_point, :out_point => [:out_point], :collapse => [: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(, stream, in_path, out_path) puts "---\n" puts "path: #{in_path}" unless stream.nil? if [:tag_number] puts " tag_number: #{[:tag_number]}" puts " " + stream.[ [:tag_number] - 1 ].inspect.join( "\n " ) else stream..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!() [:commands].each do |command| before_filter = "before_#{command.to_s}".intern send(before_filter, ) if respond_to? before_filter end process_files() do |stream, in_path, out_path| write_stream = false [:commands].each do |command| write_stream = true if send( command, , stream, in_path, out_path ) end stream.write if write_stream && ![:simulate] end [:commands].each do |command| after_filter = "after_#{command.to_s}".intern send(after_filter, ) 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 |
.print(options, stream, in_path, out_path) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/flvtool2/base.rb', line 152 def print(, stream, in_path, out_path) if [: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() if [:in_pipe] unless [:omit_out] || [:out_path].nil? create_directories_for_path( [:out_path] ) out_path = [: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, ) end else if File.directory?( [:in_path] ) pattern = [:recursive] ? "#{File::SEPARATOR}**#{File::SEPARATOR}*.flv" : "#{File::SEPARATOR}*.flv" file_names = Dir[[:in_path] + pattern] else file_names = [[:in_path]] end file_names.each do |in_path| if [:out_pipe] out_path = 'pipe' else out_path = [:out_path] unless [:out_path].nil? out_path = in_path.gsub( [:in_path], [:out_path] ) create_directories_for_path( out_path ) end end begin stream = open_stream( in_path, out_path, [:stream_log] ) yield stream, in_path, out_path begin stream.close rescue end rescue Exception => e show_exception(e, ) end end end end |
.show_exception(e, options) ⇒ Object
291 292 293 294 |
# File 'lib/flvtool2/base.rb', line 291 def show_exception(e, ) puts "ERROR: #{e.}\nERROR: #{e.backtrace.join("\nERROR: ")}" puts "Skipping file #{[:in_path]}\n" if [: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(, stream, in_path, out_path) if ( [:preserve] && ( !stream. || ( stream. && stream..['metadatacreator'] != [:metadatacreator] ) ) ) || ![:preserve] ( stream, ) return true else puts 'Input file is FLV v1.1 yet. No update necessary.' if [:verbose] return false end end |