Class: RVideo::Inspector

Inherits:
Object
  • Object
show all
Defined in:
lib/rvideo/inspector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Inspector

To inspect a video or audio file, initialize an Inspector object.

file = RVideo::Inspector.new(options_hash)

Inspector accepts three options: file, raw_response, and ffmpeg_binary. Either raw_response or file is required; ffmpeg binary is optional.

:file is a path to a file to be inspected.

:raw_response is the full output of “ffmpeg -i [file]”. If the :raw_response option is used, RVideo will not actually inspect a file; it will simply parse the provided response. This is useful if your application has already collected the ffmpeg -i response, and you don’t want to call it again.

:ffmpeg_binary is an optional argument that specifies the path to the ffmpeg binary to be used. If a path is not explicitly declared, RVideo will assume that ffmpeg exists in the Unix path. Type “which ffmpeg” to check if ffmpeg is installed and exists in your operating system’s path.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rvideo/inspector.rb', line 30

def initialize(options = {})
  if options[:raw_response]
    @raw_response = options[:raw_response]
  elsif options[:file]
    if options[:ffmpeg_binary]
      @ffmpeg_binary = options[:ffmpeg_binary]
      raise RuntimeError, "ffmpeg could not be found (trying #{@ffmpeg_binary})" unless FileTest.exist?(@ffmpeg_binary)
    else
      # assume it is in the unix path
      raise RuntimeError, 'ffmpeg could not be found (expected ffmpeg to be found in the Unix path)' unless FileTest.exist?(`which ffmpeg`.chomp)
      @ffmpeg_binary = "ffmpeg"
    end

    file = options[:file]
    @filename = File.basename(file)
    @path = File.dirname(file)
    @full_filename = file
    raise ArgumentError, "File not found (#{file})" unless FileTest.exist?(file.gsub("\"",""))
    @raw_response = `#{@ffmpeg_binary} -i #{@full_filename} 2>&1`
  else
    raise ArgumentError, "Must supply either an input file or a pregenerated response" if options[:raw_response].nil? and file.nil?
  end

   = /(Input \#.*)\n(Must|At\sleast)/m.match(@raw_response)
  
  if /Unknown format/i.match(@raw_response) || .nil?
    @unknown_format = true
  elsif /Duration: N\/A|bitrate: N\/A/im.match(@raw_response)
    @unreadable_file = true
    @raw_metadata = [1] # in this case, we can at least still get the container type
  else
    @raw_metadata = [1]
  end
end

Instance Attribute Details

#ffmpeg_binaryObject

Returns the value of attribute ffmpeg_binary.



6
7
8
# File 'lib/rvideo/inspector.rb', line 6

def ffmpeg_binary
  @ffmpeg_binary
end

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/rvideo/inspector.rb', line 4

def filename
  @filename
end

#full_filenameObject (readonly)

Returns the value of attribute full_filename.



4
5
6
# File 'lib/rvideo/inspector.rb', line 4

def full_filename
  @full_filename
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/rvideo/inspector.rb', line 4

def path
  @path
end

#raw_metadataObject (readonly)

Returns the value of attribute raw_metadata.



4
5
6
# File 'lib/rvideo/inspector.rb', line 4

def 
  @raw_metadata
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



4
5
6
# File 'lib/rvideo/inspector.rb', line 4

def raw_response
  @raw_response
end

Instance Method Details

#audio?Boolean

Does the file have an audio stream?

Returns:

  • (Boolean)


113
114
115
116
117
118
119
# File 'lib/rvideo/inspector.rb', line 113

def audio?
  if audio_match.nil?
    false
  else
    true
  end
end

#audio_bitrateObject

:nodoc:



266
267
268
# File 'lib/rvideo/inspector.rb', line 266

def audio_bitrate # :nodoc:
  nil
end

#audio_channelsObject



335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/rvideo/inspector.rb', line 335

def audio_channels
  return nil unless audio?

  case audio_match[5]
  when "mono"
    1
  when "stereo"
    2
  else
    raise RuntimeError, "Unknown number of channels: #{audio_channels}"
  end
end

#audio_channels_stringObject

The channels used in the audio stream.

Examples:

"stereo"
"mono"
"5:1"


329
330
331
332
333
# File 'lib/rvideo/inspector.rb', line 329

def audio_channels_string
  return nil unless audio?
  
  audio_match[5]
end

#audio_codecObject

The audio codec used.

Example:

"aac"


286
287
288
289
290
# File 'lib/rvideo/inspector.rb', line 286

def audio_codec
  return nil unless audio?
  
  audio_match[2]
end

#audio_sample_rateObject

The sampling rate of the audio stream.

Example:

44100


300
301
302
303
304
# File 'lib/rvideo/inspector.rb', line 300

def audio_sample_rate
  return nil unless audio?
  
  audio_match[3].to_i
end

#audio_sample_unitsObject

The units used for the sampling rate. May always be Hz.

Example:

"Hz"


314
315
316
317
318
# File 'lib/rvideo/inspector.rb', line 314

def audio_sample_units
  return nil unless audio?
  
  audio_match[4]
end

#audio_streamObject



270
271
272
273
274
275
276
# File 'lib/rvideo/inspector.rb', line 270

def audio_stream
  return nil unless valid?
  
  #/\n\s*Stream.*Audio:.*\n/.match(@raw_response)[0].strip
  match = /\n\s*Stream.*Audio:.*\n/.match(@raw_response)
  return match[0].strip if match
end

#audio_stream_idObject

The ID of the audio stream (useful for troubleshooting).

Example:

#0.1


355
356
357
358
359
# File 'lib/rvideo/inspector.rb', line 355

def audio_stream_id
  return nil unless audio?
  
  audio_match[1]
end

#bitrateObject

The bitrate of the movie.

Example:

3132


246
247
248
249
250
# File 'lib/rvideo/inspector.rb', line 246

def bitrate
  return nil unless valid?
  
  bitrate_match[1].to_i
end

#bitrate_unitsObject

The bitrate units used. In practice, this may always be kb/s.

Example:

"kb/s"


260
261
262
263
264
# File 'lib/rvideo/inspector.rb', line 260

def bitrate_units
  return nil unless valid?
  
  bitrate_match[2]
end

#containerObject

Returns the container format for the file. Instead of returning a single format, this may return a string of related formats.

Examples:

"avi"

"mov,mp4,m4a,3gp,3g2,mj2"


200
201
202
203
204
# File 'lib/rvideo/inspector.rb', line 200

def container
  return nil if @unknown_format
  
  /Input \#\d+\,\s*(\S+),\s*from/.match(@raw_metadata)[1]
end

#durationObject

The duration of the movie in milliseconds, as an integer.

Example:

24400         # 24.4 seconds

Note that the precision of the duration is in tenths of a second, not thousandths, but milliseconds are a more standard unit of time than deciseconds.



231
232
233
234
235
236
# File 'lib/rvideo/inspector.rb', line 231

def duration
  return nil unless valid?
  
  units = raw_duration.split(":")
  (units[0].to_i * 60 * 60 * 1000) + (units[1].to_i * 60 * 1000) + (units[2].to_f * 1000).to_i
end

#ffmpeg_buildObject

Returns the build description for ffmpeg.

Example:

built on Apr 15 2006 04:58:19, gcc: 4.0.1 (Apple Computer, Inc. build
  5250)


185
186
187
# File 'lib/rvideo/inspector.rb', line 185

def ffmpeg_build
  /(\n\s*)(built on.*)(\n)/.match(@raw_response)[2]
end

#ffmpeg_configurationObject

Returns the configuration options used to build ffmpeg.

Example:

--enable-mp3lame --enable-gpl --disable-ffplay --disable-ffserver
  --enable-a52 --enable-xvid


157
158
159
# File 'lib/rvideo/inspector.rb', line 157

def ffmpeg_configuration 
  /(\s*configuration:)(.*)\n/.match(@raw_response)[2].strip
end

#ffmpeg_libavObject

Returns the versions of libavutil, libavcodec, and libavformat used by ffmpeg.

Example:

libavutil version: 49.0.0
libavcodec version: 51.9.0
libavformat version: 50.4.0


172
173
174
# File 'lib/rvideo/inspector.rb', line 172

def ffmpeg_libav
  /^(\s*lib.*\n)+/.match(@raw_response)[0].split("\n").each {|l| l.strip! }
end

#ffmpeg_versionObject

Returns the version of ffmpeg used, In practice, this may or may not be useful.

Examples:

SVN-r6399
CVS


144
145
146
# File 'lib/rvideo/inspector.rb', line 144

def ffmpeg_version
  @ffmpeg_version = @raw_response.split("\n").first.split("version").last.split(",").first.strip
end

#fpsObject

The frame rate of the video in frames per second

Example:

"29.97"


452
453
454
455
456
# File 'lib/rvideo/inspector.rb', line 452

def fps
  return nil unless video?
  
  /([0-9\.]+) fps/.match(video_stream)[1]
end

#heightObject

The height of the video in pixels.



424
425
426
427
428
# File 'lib/rvideo/inspector.rb', line 424

def height
  return nil unless video?
  
  video_match[5].to_i
end

#invalid?Boolean

Returns false if the file can be read successfully. Returns false otherwise.

Returns:

  • (Boolean)


81
82
83
# File 'lib/rvideo/inspector.rb', line 81

def invalid?
  !valid?
end

#raw_durationObject

The duration of the movie, as a string.

Example:

"00:00:24.4"  # 24.4 seconds


213
214
215
216
217
# File 'lib/rvideo/inspector.rb', line 213

def raw_duration
  return nil unless valid?
  
  /Duration:\s*([0-9\:\.]+),/.match(@raw_metadata)[1]
end

#resolutionObject

width x height, as a string.

Examples:

320x240
1280x720


438
439
440
441
442
# File 'lib/rvideo/inspector.rb', line 438

def resolution
  return nil unless video?
  
  "#{width}x#{height}"
end

#unknown_format?Boolean

True if the format is not understood (“Unknown Format”)

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/rvideo/inspector.rb', line 89

def unknown_format?
  if @unknown_format
    true
  else
    false
  end
end

#unreadable_file?Boolean

True if the file is not readable (“Duration: N/A, bitrate: N/A”)

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/rvideo/inspector.rb', line 101

def unreadable_file?
  if @unreadable_file
    true
  else
    false
  end
end

#valid?Boolean

Returns true if the file can be read successfully. Returns false otherwise.

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/rvideo/inspector.rb', line 69

def valid?
  if @unknown_format or @unreadable_file
    false
  else
    true
  end
end

#video?Boolean

Does the file have a video stream?

Returns:

  • (Boolean)


125
126
127
128
129
130
131
# File 'lib/rvideo/inspector.rb', line 125

def video?
  if video_match.nil?
    false
  else
    true
  end
end

#video_codecObject

The video codec used.

Example:

"mpeg4"


390
391
392
393
394
# File 'lib/rvideo/inspector.rb', line 390

def video_codec
  return nil unless video?
  
  video_match[2]
end

#video_colorspaceObject

The colorspace of the video stream.

Example:

"yuv420p"


404
405
406
407
408
# File 'lib/rvideo/inspector.rb', line 404

def video_colorspace
  return nil unless video?
  
  video_match[3]
end

#video_streamObject



361
362
363
364
365
366
367
# File 'lib/rvideo/inspector.rb', line 361

def video_stream
  return nil unless valid?
  
  match = /\n\s*Stream.*Video:.*\n/.match(@raw_response)
  return match[0].strip unless match.nil?
  nil
end

#video_stream_idObject

The ID of the video stream (useful for troubleshooting).

Example:

#0.0


376
377
378
379
380
# File 'lib/rvideo/inspector.rb', line 376

def video_stream_id
  return nil unless video?
  
  video_match[1]
end

#widthObject

The width of the video in pixels.



414
415
416
417
418
# File 'lib/rvideo/inspector.rb', line 414

def width
  return nil unless video?
  
  video_match[4].to_i
end