Class: VTools::Video

Inherits:
Object
  • Object
show all
Defined in:
lib/vtools/video.rb

Overview

Video instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Video

Returns a new instance of Video.

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vtools/video.rb', line 26

def initialize path

  @invalid = true
  @uncertain_duration = true
  @convert_options  = {}
  @thumbs_options   = {}
  @path = set_path path

  path =~ /([^\/\\]+?)(\.\w+)?$/
  @name = $1

  raise FileError, "the file '#{@path}' does not exist" unless File.exists?(@path)

  @converter    = Converter.new self
  @thumbnailer  = Thumbnailer.new self
end

Instance Attribute Details

#audio_bitrateObject (readonly)

Returns the value of attribute audio_bitrate.



8
9
10
# File 'lib/vtools/video.rb', line 8

def audio_bitrate
  @audio_bitrate
end

#audio_codecObject (readonly)

Returns the value of attribute audio_codec.



8
9
10
# File 'lib/vtools/video.rb', line 8

def audio_codec
  @audio_codec
end

#audio_sample_rateObject (readonly)

Returns the value of attribute audio_sample_rate.



8
9
10
# File 'lib/vtools/video.rb', line 8

def audio_sample_rate
  @audio_sample_rate
end

#audio_streamObject (readonly)

Returns the value of attribute audio_stream.



8
9
10
# File 'lib/vtools/video.rb', line 8

def audio_stream
  @audio_stream
end

#bitrateObject (readonly)

Returns the value of attribute bitrate.



8
9
10
# File 'lib/vtools/video.rb', line 8

def bitrate
  @bitrate
end

#colorspaceObject (readonly)

Returns the value of attribute colorspace.



8
9
10
# File 'lib/vtools/video.rb', line 8

def colorspace
  @colorspace
end

#convert_optionsObject (readonly)

Returns the value of attribute convert_options.



8
9
10
# File 'lib/vtools/video.rb', line 8

def convert_options
  @convert_options
end

#darObject (readonly)

Returns the value of attribute dar.



8
9
10
# File 'lib/vtools/video.rb', line 8

def dar
  @dar
end

#durationObject (readonly)

Returns the value of attribute duration.



8
9
10
# File 'lib/vtools/video.rb', line 8

def duration
  @duration
end

#frame_rateObject (readonly)

Returns the value of attribute frame_rate.



8
9
10
# File 'lib/vtools/video.rb', line 8

def frame_rate
  @frame_rate
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/vtools/video.rb', line 8

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/vtools/video.rb', line 8

def path
  @path
end

#resolutionObject (readonly)

Returns the value of attribute resolution.



8
9
10
# File 'lib/vtools/video.rb', line 8

def resolution
  @resolution
end

#startObject (readonly)

Returns the value of attribute start.



8
9
10
# File 'lib/vtools/video.rb', line 8

def start
  @start
end

#thumbs_optionsObject (readonly)

Returns the value of attribute thumbs_options.



8
9
10
# File 'lib/vtools/video.rb', line 8

def thumbs_options
  @thumbs_options
end

#video_bitrateObject (readonly)

Returns the value of attribute video_bitrate.



8
9
10
# File 'lib/vtools/video.rb', line 8

def video_bitrate
  @video_bitrate
end

#video_codecObject (readonly)

Returns the value of attribute video_codec.



8
9
10
# File 'lib/vtools/video.rb', line 8

def video_codec
  @video_codec
end

#video_streamObject (readonly)

Returns the value of attribute video_stream.



8
9
10
# File 'lib/vtools/video.rb', line 8

def video_stream
  @video_stream
end

Instance Method Details

#audio_channelsObject

valid audio channels index



133
134
135
136
137
138
139
# File 'lib/vtools/video.rb', line 133

def audio_channels
  return nil unless @audio_channels
  return @audio_channels[/\d*/].to_i if @audio_channels["channels"]
  return 1 if @audio_channels["mono"]
  return 2 if @audio_channels["stereo"]
  return 6 if @audio_channels["5.1"]
end

#calculated_aspect_ratioObject

aspect ratio calculator



117
118
119
120
121
122
123
124
125
# File 'lib/vtools/video.rb', line 117

def calculated_aspect_ratio
  if dar
    w, h = dar.split(":")
    w.to_f / h.to_f
  else
    aspect = width.to_f / height.to_f
    (aspect.nan? || aspect == 1.0/"x".to_f) ? nil : aspect
  end
end

#convert(setup = {}) ⇒ Object

convert video



50
51
52
53
# File 'lib/vtools/video.rb', line 50

def convert setup = {}
  @convert_options = ConvertOptions.new(setup, {:aspect => calculated_aspect_ratio})
  @converter.run
end

#create_thumbs(setup = {}) ⇒ Object

generate thumbs



44
45
46
47
# File 'lib/vtools/video.rb', line 44

def create_thumbs setup = {}
  @thumbs_options = ThumbsOptions.new setup
  @thumbnailer.run
end

#get_infoObject

generate video informatioin



56
57
58
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
96
97
# File 'lib/vtools/video.rb', line 56

def get_info

  stdin, stdout, stderr = Open3.popen3("#{CONFIG[:ffmpeg_binary]} -i '#{@path}'")
  # Output will land in stderr
  output = stderr.read
  VTools.fix_encoding output

  output[/Duration: (\d{2}):(\d{2}):(\d{2}\.\d{1})/]
  @duration = ($1.to_i*60*60) + ($2.to_i*60) + $3.to_f

  output[/start: (\d*\.\d*)/]
  @start = $1.to_f

  output[/bitrate: (\d*)/]
  @bitrate = $1.to_i || nil

  output[/Video: (.*)/]
  @video_stream = $1

  output[/Audio: (.*)/]
  @audio_stream = $1

  @uncertain_duration = true #output.include?("Estimating duration from bitrate, this may be inaccurate") || @start > 0

  if @video_stream
    @video_codec, @colorspace, resolution, video_bitrate = @video_stream.split(/\s?,\s?/)
    @video_bitrate = video_bitrate =~ %r(\A(\d+) kb/s\Z) ? $1.to_i : nil
    @resolution = resolution.split(" ").first rescue nil # get rid of [PAR 1:1 DAR 16:9]
    @dar = $1 if @video_stream[/DAR (\d+:\d+)/]
  end

  if @audio_stream
    @audio_codec, audio_sample_rate, @audio_channels, unused, audio_bitrate = @audio_stream.split(/\s?,\s?/)
    @audio_bitrate = audio_bitrate =~ %r(\A(\d+) kb/s\Z) ? $1.to_i : nil
    @audio_sample_rate = audio_sample_rate[/\d*/].to_i
  end

  @invalid = false unless @video_stream.to_s.empty?
  @invalid = true if output.include?("is not supported")

  self
end

#heightObject



112
113
114
# File 'lib/vtools/video.rb', line 112

def height
  resolution.split("x").last.to_i rescue nil
end

#sizeObject

video file size



128
129
130
# File 'lib/vtools/video.rb', line 128

def size
  File.size(@path)
end

#to_json(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vtools/video.rb', line 14

def to_json(*args)
  ignore = [:@convert_options, :@thumbs_options, :@converter, :@thumbnailer, :@uncertain_duration, :invalid]

  hsh = instance_variables.inject({}) do |data, var|
    data[ var[1..-1] ] = instance_variable_get(var) unless ignore.include? var.to_sym
    data
  end

  hsh["valid"] = !@invalid
  hsh.to_json(*args)
end

#uncertain_duration?Boolean

validate duration

Returns:

  • (Boolean)


104
105
106
# File 'lib/vtools/video.rb', line 104

def uncertain_duration?
  @uncertain_duration
end

#valid?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/vtools/video.rb', line 99

def valid?
  not @invalid
end

#widthObject



108
109
110
# File 'lib/vtools/video.rb', line 108

def width
  resolution.split("x").first.to_i rescue nil
end