Class: VideoTranscoding::Media

Inherits:
Object
  • Object
show all
Defined in:
lib/video_transcoding/media.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, title: nil, autocrop: false, extended: true, allow_directory: true) ⇒ Media



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/video_transcoding/media.rb', line 11

def initialize(path: nil, title: nil, autocrop: false, extended: true, allow_directory: true)
  fail 'missing path argument' if path.nil?
  @path     = path
  @previews = autocrop ? 10 : 2
  @extended = extended
  @scan     = nil
  @stat     = File.stat(@path)

  if @stat.directory?
    fail UsageError, "not a file: #{@path}" unless allow_directory
    @title = title

    if @title.nil?
      @scan = Media.scan(@path, 0, 2)
    elsif @title < 1
      fail UsageError, "invalid title index: #{@title}"
    end
  else
    fail UsageError, "invalid title index: #{title}" unless title.nil? or title == 1
    @title = 1
  end

  @scan       = Media.scan(@path, @title, @previews) if @scan.nil?
  @first_scan = @scan
  @mp4_scan   = nil
  @summary    = nil
  @info       = nil
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/video_transcoding/media.rb', line 9

def path
  @path
end

Class Method Details

.scan(path, title, previews) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/video_transcoding/media.rb', line 278

def self.scan(path, title, previews)
  output = ''
  label = title == 0 ? 'media' : "media title #{title}"
  Console.info "Scanning #{label} with HandBrakeCLI..."
  last_seconds = Time.now.tv_sec

  begin
    IO.popen([
      HandBrake.command_name,
      "--title=#{title}",
      '--scan',
      "--previews=#{previews}:0",
      "--input=#{path}"
    ], :err=>[:child, :out]) do |io|
      io.each do |line|
        seconds = Time.now.tv_sec

        if seconds - last_seconds >= 3
          Console.warn '...'
          last_seconds = seconds
        end

        line.encode! 'UTF-8', 'binary', invalid: :replace, undef: :replace, replace: ''
        Console.debug line
        output += line
      end
    end
  rescue SystemCallError => e
    raise "scanning #{label} failed: #{e}"
  end

  fail "scanning #{label} failed" unless $CHILD_STATUS.exitstatus == 0
  output
end

.scan_mp4(path) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/video_transcoding/media.rb', line 313

def self.scan_mp4(path)
  output = ''
  Console.info 'Scanning with mp4track...'

  begin
    IO.popen([
      MP4track.command_name,
      '--list',
      path
    ], :err=>[:child, :out]) { |io| output = io.read }
  rescue SystemCallError => e
    raise "scanning failed: #{e}"
  end

  Console.debug output
  fail 'scanning failed' unless $CHILD_STATUS.exitstatus == 0
  output
end

Instance Method Details

#infoObject



49
50
51
52
53
54
55
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/video_transcoding/media.rb', line 49

def info
  return @info unless @info.nil?
  @info = {}

  if @title.nil?
    if @scan =~ /\+ title ([0-9]+):\r?\n  \+ Main Feature/m
      @title = $1.to_i
    elsif @scan =~ /^\+ title ([0-9]+):/
      @title = $1.to_i
    else
      @title = 1
    end

    @scan = Media.scan(@path, @title, @previews)
  end

  if @scan =~ / libhb: scan thread found ([0-9]+) valid title/
    fail 'multiple titles found' if $1.to_i > 1
  end

  @info[:title]     = @title
  @info[:size]      = @stat.size
  @info[:directory] = @stat.directory?

  unless @scan =~ /^  \+ duration: ([0-9]{2}):([0-9]{2}):([0-9]{2})/
    fail 'media duration not found'
  end

  @info[:duration] = ($1.to_i * 60 * 60) + ($2.to_i * 60) + $3.to_i

  unless @scan =~ / scan: [0-9]+ previews, ([0-9]+)x([0-9]+), ([0-9.]+) fps, autocrop = ([0-9]+)\/([0-9]+)\/([0-9]+)\/([0-9]+), aspect [0-9.]+:[0-9.]+, PAR [0-9]+:[0-9]+/
    fail 'video information not found'
  end

  @info[:width]   = $1.to_i
  @info[:height]  = $2.to_i
  @info[:fps]     = $3.to_f

  if @previews > 2
    @info[:autocrop] = {:top => $4.to_i, :bottom => $5.to_i, :left => $6.to_i, :right => $7.to_i}
  else
    @info[:autocrop] = nil
  end

  return @info unless @extended

  unless @scan =~ /  \+ audio tracks:\r?\n(.*)  \+ subtitle tracks:\r?\n(.*)HandBrake has exited./m
    fail 'audio and subtitle information not found'
  end

  audio             = $1
  subtitle          = $2
  @info[:audio]     = {}
  @info[:subtitle]  = {}

  audio.gsub(/\r/, '').each_line do |line|
    if line =~ /^    \+ ([0-9]+), [^(]+\(([^)]+)\) .*\(([0-9.]+) ch\) .*\(iso639-2: ([a-z]{3})\)/
      track                 = $1.to_i
      track_info            = {}
      track_info[:format]   = $2
      track_info[:channels] = $3.to_f
      track_info[:language] = $4

      if line =~ /([0-9]+)bps/
        track_info[:bps] = $1.to_i
      else
        track_info[:bps] = nil
      end

      @info[:audio][track] = track_info
    end
  end

  subtitle.gsub(/\r/, '').each_line do |line|
    if line =~ /^    \+ ([0-9]+), .*\(iso639-2: ([a-z]{3})\) \((?:Text|Bitmap)\)\(([^)]+)\)/
      track                   = $1.to_i
      track_info              = {}
      track_info[:language]   = $2
      track_info[:format]     = $3
      @info[:subtitle][track] = track_info
    end
  end

  @info[:h264]    = false
  @info[:mpeg2]   = false
  audio_track     = 0
  subtitle_track  = 0

  @scan.each_line do |line|
    if line =~ /[ ]+Stream #0\.([0-9]+)[^ ]*: (Video|Audio|Subtitle): (.*)/
      stream      = $1.to_i
      type        = $2
      attributes  = $3

      case type
      when 'Video'
        unless @info.has_key? :stream
          @info[:stream] = stream

          if attributes =~ /^h264/
            @info[:h264] = true
          elsif attributes =~ /^mpeg2video/
            @info[:mpeg2] = true
          end
        end
      when 'Audio'
        audio_track += 1

        if @info[:audio].has_key? audio_track
          track_info = @info[:audio][audio_track]
          track_info[:stream] = stream

          if attributes =~ /\(default\)/
            track_info[:default] = true
          else
            track_info[:default] = false
          end

          if @scan =~ /[ ]+Stream #0\.#{stream}[^ ]*: Audio: [^\n]+\n[ ]+Metadata:\r?\n^[ ]+title[ ]+: ([^\r\n]+)/m
            track_info[:name] = $1
          else
            track_info[:name] = nil
          end
        end
      when 'Subtitle'
        subtitle_track += 1

        if @info[:subtitle].has_key? subtitle_track
          track_info = @info[:subtitle][subtitle_track]
          track_info[:stream] = stream

          if attributes =~ /\(default\)/
            track_info[:default] = true
          else
            track_info[:default] = false
          end

          if attributes =~ /\(forced\)/
            track_info[:forced] = true
          else
            track_info[:forced] = false
          end

          if @scan =~ /[ ]+Stream #0\.#{stream}[^ ]*: Subtitle: [^\n]+\n[ ]+Metadata:\r?\n^[ ]+title[ ]+: ([^\r\n]+)/m
            track_info[:name] = $1
          else
            track_info[:name] = nil
          end
        end
      end
    end
  end

  @info[:bd]  = false
  @info[:dvd] = false

  if @scan =~ / scan: (BD|DVD) has [0-9]+ title/
    @info[$1.downcase.to_sym] = true
    @info[:mpeg2] = true if @info[:dvd]
  end

  if @scan =~ /Input #0, matroska/
    @info[:mkv] = true
  else
    @info[:mkv] = false
  end

  if @scan =~ /Input #0, mov,mp4/
    @info[:mp4] = true
  else
    @info[:mp4] = false
  end

  if @info[:mp4]
    @mp4_scan = Media.scan_mp4(@path)
    types     = []
    flags     = []
    names     = []

    @mp4_scan.each_line do |line|
      if line =~ /^[ ]+type[ ]+=[ ]+(.*)$/
        types << $1
      elsif line =~ /^[ ]+enabled[ ]+=[ ]+(.*)$/
        if $1 == 'true'
          flags << true
        else
          flags << false
        end
      elsif line =~ /^[ ]+userDataName[ ]+=[ ]+(.*)$/
        if $1 == '<absent>'
          names << nil
        else
          names << $1
        end
      end
    end

    index           = 0
    audio_track     = 0
    subtitle_track  = 0

    types.each do |type|
      case type
      when 'audio'
        audio_track += 1

        if @info[:audio].has_key? audio_track
          track_info = @info[:audio][audio_track]
          track_info[:default] = flags[index]
          track_info[:name] = names[index]
        end
      when 'text'
        subtitle_track += 1

        if @info[:subtitle].has_key? subtitle_track
          track_info = @info[:subtitle][subtitle_track]
          track_info[:default] = flags[index]
          track_info[:forced] = flags[index]
          track_info[:name] = names[index]
        end
      end

      index += 1
    end
  end

  @info
end

#summaryObject



40
41
42
43
44
45
46
47
# File 'lib/video_transcoding/media.rb', line 40

def summary
  if @summary.nil?
    @summary = ''
    @first_scan.each_line { |line| @summary += line if line =~ /^ *\+ (?!(autocrop|support))/ }
  end

  @summary
end