Class: TranscodingMachine::Server::MplayerIntegrator

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

Constant Summary collapse

BINARY =
["mplayer"]
OPTIONS =
["-identify", "-vo", "null", "-ao", "null", "-frames", "0", "-really-quiet", "-msgcharset", "utf8"]
TIMEOUT =
60
MPLAYER_TRACK_FIELD_MAP =
{
  'CODEC' => :codec,
  'WIDTH' => :width,
  'HEIGHT' => :height,
  'FORMAT' => :format,
  'ASPECT' => :aspect,
  'ID' => :id,
  'BITRATE' => :bitrate,
  'FPS' => :fps,
  'FILENAME' => :file_name,
  'LENGTH' => :length,
  'DEMUXER' => :demuxer,
  'RATE' => :rate,
  'NCH' => :channels
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ MplayerIntegrator

Returns a new instance of MplayerIntegrator.



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/transcoding_machine/server/media_file_attributes.rb', line 439

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 mplayer"
    raise e
  end

  raise "mplayer error when inspecting #{file_path}: #{result.last}" if result.first.empty? && !result.last.empty?

  mplayer_values = {:container => {:type => :container}}

  match = result.first.match(/.*ID_AUDIO_ID=(\d).*/)
  audio_track = mplayer_values["track_#{match[1]}".to_sym] = {:type => :audio} unless match.nil?

  match = result.first.match(/.*ID_VIDEO_ID=(\d).*/)
  video_track = mplayer_values["track_#{match[1]}".to_sym] = {:type => :video} unless match.nil?

  result.first.split(/\n/).each do |line|
    #puts line
    if (match = line.match(/ID_([^_]+)(_([^=].*))?=(.*)/))
      if match[3]
        key = MPLAYER_TRACK_FIELD_MAP[match[3]] || match[3]
      else
        key = MPLAYER_TRACK_FIELD_MAP[match[1]] || match[1]
      end
      case match[1]
      when 'VIDEO'
        video_track[key] = match[4]
      when 'AUDIO'
        audio_track[key] = match[4]
      else
        mplayer_values[:container][key] = match[4]
      end
    end
  end

  @tracks = MediaFileAttributes.parse_values(mplayer_values)
end

Instance Attribute Details

#tracksObject

Returns the value of attribute tracks.



418
419
420
# File 'lib/transcoding_machine/server/media_file_attributes.rb', line 418

def tracks
  @tracks
end