class FFmpeg::Media

Attributes

audio_bit_rate[R]
audio_codec[R]
audio_sample_rate[R]
bit_rate[R]
channel_layout[R]
dar[R]
duration[R]
filename[R]
format[R]
frame_rate[R]
height[R]
path[R]
pix_fmt[R]
resolution[R]
sar[R]
size[R]
status[RW]
video_bit_rate[R]
video_codec[R]
width[R]

Public Class Methods

new(path) click to toggle source
# File lib/rmedia/media.rb, line 13
def initialize(path)
  raise Errno::ENOENT, "#{path} does not exist" unless path_exists?(path)

  @path = path

  options = '-v quiet -print_format json -show_format -show_streams'
  command = "#{FFmpeg.ffprobe_bin} #{options} #{path}"

  output = FFmpeg.command(command) { |_stdin, stdout| stdout.read }
  json = JSON.parse(output)

  raise "#{path} does not media" unless json['format']

  media_info(json['format'])
  streams_info(json['streams'])
end

Public Instance Methods

audio?() click to toggle source
# File lib/rmedia/media.rb, line 34
def audio?
  @status & 1 == 1
end
hacked_transcode(output, options = {}, &block) click to toggle source
# File lib/rmedia/media.rb, line 53
def hacked_transcode(output, options = {}, &block)
  opts = EncodeOption.new(options)
  command = OptionParser.hacked_command(@path, output, opts)
  Transcoder.run(command, @duration, &block)
  Media.new(output)
end
screenshot(output, options = {}, &block) click to toggle source
# File lib/rmedia/media.rb, line 45
def screenshot(output, options = {}, &block)
  opts = EncodeOption.new(options.merge(screenshot: true))
  opts['-ss'] = @duration / 2.0 unless opts['-ss']
  command = OptionParser.hacked_command(@path, output, opts)
  Transcoder.run(command, @duration, &block)
  Media.new(output)
end
transcode(output, options = {}, &block) click to toggle source
# File lib/rmedia/media.rb, line 38
def transcode(output, options = {}, &block)
  opts = EncodeOption.new(options)
  command = OptionParser.command(@path, output, opts)
  Transcoder.run(command, @duration, &block)
  Media.new(output)
end
video?() click to toggle source
# File lib/rmedia/media.rb, line 30
def video?
  @status & 2 == 2
end

Private Instance Methods

audio_info(stream) click to toggle source
# File lib/rmedia/media.rb, line 106
def audio_info(stream)
  @audio_codec = stream['codec_name']
  @audio_bit_rate = stream['bit_rate'].to_i
  @audio_sample_rate = stream['sample_rate'].to_i
  @channel_layout = stream['channel_layout']
  @status += 1
end
media_info(format) click to toggle source
# File lib/rmedia/media.rb, line 74
def media_info(format)
  @filename = format['filename'].split('/')[-1]
  @format = format['format_name']
  @duration = format['duration'].to_f
  @size = format['size'].to_i
  @bit_rate = format['bit_rate'].to_i
  @status = 0
end
path_exists?(path) click to toggle source
# File lib/rmedia/media.rb, line 62
def path_exists?(path)
  File.exist?(path) || url_exists?(path)
end
streams_info(streams) click to toggle source

Attributes set first stream both video and audio

# File lib/rmedia/media.rb, line 84
def streams_info(streams)
  streams.each do |stream|
    case stream['codec_type']
    when 'video' then @status & 2 == 0 && video_info(stream)
    when 'audio' then @status & 1 == 0 && audio_info(stream)
    end
  end
end
url_exists?(path) click to toggle source
# File lib/rmedia/media.rb, line 66
def url_exists?(path)
  client = Faraday.new { |f| f.use Faraday::Adapter::NetHttp }
  res = client.head(path)
  res.status == 200
rescue
  false
end
video_info(stream) click to toggle source
# File lib/rmedia/media.rb, line 93
def video_info(stream)
  @video_codec = stream['codec_name']
  @video_bit_rate = stream['bit_rate'].to_i
  @width = stream['width'].to_i
  @height = stream['height'].to_i
  @resolution = "#{width}x#{height}"
  @frame_rate = stream['avg_frame_rate'] == '0/0' ? nil : Rational(stream['avg_frame_rate'])
  @sar = stream['sample_aspect_ratio']
  @dar = stream['display_aspect_ratio']
  @pix_fmt = stream['pix_fmt']
  @status += 2
end