Class: TranscodingMachine::Server::FfmpegIntegrator

Inherits:
Object
  • Object
show all
Defined in:
lib/transcoding_machine/server/media_file_attributes.rb

Constant Summary collapse

BINARY =
["ffmpeg"]
OPTIONS =
["-i"]
TIMEOUT =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ FfmpegIntegrator

Returns a new instance of FfmpegIntegrator.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/transcoding_machine/server/media_file_attributes.rb', line 297

def initialize(file_path)
  commandline = []
  commandline += BINARY
  commandline += OPTIONS
  commandline += [file_path]
  puts "trying to run: #{commandline.join(' ')}"
  result = begin
    timeout(TIMEOUT) do
      Open3.popen3(*commandline) do |i, o, e|
        [o.read, e.read]
      end
    end
  rescue Timeout::Error => e
    puts "Timeout reached when inspecting #{file_path} with ffmpeg"
    raise e
  end

  result = result.join("\n")

  ffmpeg_values = Hash.new

  start_index = result.index("Input #0, ")
  @tracks = {}

  unless start_index.nil?
    result = result[start_index..-1]

    result.split(/\n/).each do |line|
      line.strip!
      if match = line.match(/^Duration: ((\d\d):(\d\d):(\d\d(.\d)?)), .*, bitrate: (\d+) kb\/s/)
        puts "found duration #{line}"
        ffmpeg_values[:container] = {:type => :container}
        if match[1]
          hours = match[2] ? match[2].to_i : 0
          minutes = match[3] ? match[3].to_i : 0
          seconds = match[4] ? match[4].to_f : 0
          ffmpeg_values[:container][:length] = (hours * 60 * 60) + (minutes * 60) + seconds
        end
        if match[6]
          ffmpeg_values[:container][:bitrate] = match[6].to_i * 1000
        end
      elsif match = line.match(/^Stream #0.(\d).*: Video: ([^,]*)(, [^,]*)?(, (\d+)x(\d+))(, (\d+.\d+) fps)?/)
        puts "found video #{line}"
        track_info = ffmpeg_values["track_#{match[1]}".to_sym] = {:type => :video}
        if match[2]
          track_info[:codec] = match[2]
        end
        if match[5]
          track_info[:width] = match[5]
        end
        if match[6]
          track_info[:height] = match[6]
        end
        if match[8]
          track_info[:fps] = match[8]
        end        
      elsif match = line.match(/^Stream #0.(\d).*: Audio: ([^,]*)(, (\d+) Hz)?(, (mono|stereo))?(, (\d+) kb\/s)?/)
        puts "found audio #{line}"
        track_info = ffmpeg_values["track_#{match[1]}".to_sym] = {:type => :audio}
        if match[2]
          track_info[:codec] = match[2]
        end
        if match[4]
          track_info[:rate] = match[4]
        end
        if match[6]
          if match[6] == 'stereo'
            track_info[:channels] = 2
          elsif match[6] == 'mono'
            track_info[:channels] = 1
          end
        end
        if match[8]
          track_info[:bitrate] = match[8].to_i * 1000
        end
      elsif match = line.match(/^Stream #0.(\d).*: Data: (.*)/)
        puts "found data #{line}"
        track_info = ffmpeg_values["track_#{match[1]}".to_sym] = {:type => :data}
      else
        puts "found other #{line}"
      end
    end

    @tracks = MediaFileAttributes.parse_values(ffmpeg_values)
  end
end

Instance Attribute Details

#tracksObject

Returns the value of attribute tracks.



292
293
294
# File 'lib/transcoding_machine/server/media_file_attributes.rb', line 292

def tracks
  @tracks
end

Class Method Details

.create_thumbnail(file_path, width, height, time) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/transcoding_machine/server/media_file_attributes.rb', line 384

def self.create_thumbnail(file_path, width, height, time)
  thumbnail_file_path = "#{file_path}.jpg"
  commandline = []
  commandline += BINARY
  commandline << '-ss'
  commandline << time.to_s
  commandline << '-i'
  commandline << file_path
  commandline += ['-f', 'mjpeg', '-deinterlace', '-vframes', '1', '-an', '-y', '-s']
  commandline << "#{width}x#{height}"
  commandline << thumbnail_file_path

  puts "trying to run: #{commandline.join(' ')}"
  result = begin
    timeout(60) do
      Open3.popen3(*commandline) do |i, o, e|
        [o.read, e.read]
      end
    end
  rescue Timeout::Error => e
    puts "Timeout reached when inspecting #{file_path} with ffmpeg"
    raise e
  end
  thumbnail_file = File.new(thumbnail_file_path)
  if thumbnail_file.stat.size == 0
    FileUtils.rm_f(thumbnail_file.path)
    throw result.join
  end

  return thumbnail_file
end