-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.rb
495 lines (422 loc) · 13.5 KB
/
media.rb
1
2
3
4
5
6
7
8
9
10
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
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
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
277
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
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
487
488
489
490
491
492
493
494
495
# frozen_string_literal: true
require 'multi_json'
require 'uri'
require_relative 'errors'
require_relative 'stream'
module FFMPEG
# The Media class represents a multimedia file and provides methods
# to inspect its metadata.
# It accepts a local path or remote URL to a multimedia file as input.
# It uses ffprobe to get the streams and format of the multimedia file.
#
# @example
# media = FFMPEG::Media.new('/path/to/media.mp4')
# media.video? # => true
# media.video_streams? # => true
# media.audio? # => false
# media.audio_streams? # => true
# media.local? # => true
#
# @example
# media = FFMPEG::Media.new('https://example.com/media.mp4', load: false)
# media.loaded? # => false
# media.video? # => true (loaded automatically)
# media.loaded? # => true
# media.remote? # => true
#
# @example
# media = FFMPEG::Media.new('/path/to/media.mp4', load: false, autoload: false)
# media.loaded? # => false
# media.video? # => raises 'Media not loaded'
# media.load!
# media.video? # => true
class Media
class LoadError < FFMPEG::Error; end
private_class_method def self.autoload(*method_names)
method_names.flatten!
method_names.each do |method_name|
method = instance_method(method_name)
define_method(method_name) do |*args, &block|
if loaded?
method.bind(self).call(*args, &block)
elsif @autoload
load!
method.bind(self).call(*args, &block)
else
raise 'Media not loaded'
end
end
end
end
attr_reader :path
autoload attr_reader :size, :metadata, :streams, :tags,
:format_name, :format_long_name,
:start_time, :bit_rate, :duration
# @param path [String] The local path or remote URL to a multimedia file.
# @param ffprobe_args [Array<String>] Additional arguments to pass to ffprobe.
# @param load [Boolean] Whether to load the metadata immediately.
# @param autoload [Boolean] Whether to autoload the metadata when accessing attributes.
def initialize(path, *ffprobe_args, load: true, autoload: true)
@path = path.to_s
@ffprobe_args = ffprobe_args
@autoload = autoload
@loaded = false
@mutex = Mutex.new
load! if load
end
# Load the metadata of the multimedia file.
#
# @return [Boolean]
def load!
@mutex.lock
return @loaded if @loaded
stdout, stderr, = FFMPEG.ffprobe_capture3(
'-i', @path, '-print_format', 'json',
'-show_format', '-show_streams', '-show_error',
*@ffprobe_args
)
begin
@metadata = MultiJson.load(stdout, symbolize_keys: true)
rescue MultiJson::ParseError => e
raise LoadError, e.message.capitalize
end
if @metadata.key?(:error)
raise LoadError, "#{@metadata[:error][:string].capitalize} (code #{@metadata[:error][:code]})"
end
@size = @metadata[:format][:size].to_i
@streams = @metadata[:streams].map { |metadata| Stream.new(metadata, stderr) }
@tags = @metadata[:format][:tags]
@format_name = @metadata[:format][:format_name]
@format_long_name = @metadata[:format][:format_long_name]
@start_time = @metadata[:format][:start_time].to_f
@bit_rate = @metadata[:format][:bit_rate].to_i
@duration = @metadata[:format][:duration].to_f
@valid = @streams.any?(&:supported?)
@loaded = true
ensure
@mutex.unlock
end
# Whether the media has been loaded.
#
# @return [Boolean]
def loaded?
@loaded
end
# Whether the media is on a remote URL.
#
# @return [Boolean]
def remote?
@remote ||= @path =~ URI::DEFAULT_PARSER.make_regexp(%w[http https]) ? true : false
end
# Whether the media is at a local path.
#
# @return [Boolean]
def local?
!remote?
end
# Whether the media is valid (there is at least one stream that is supported).
#
# @return [Boolean]
autoload def valid?
@valid
end
# Returns all video streams.
#
# @return [Array<Stream>, nil]
autoload def video_streams
return @video_streams if instance_variable_defined?(:@video_streams)
@video_streams = @streams.select(&:video?)
end
# Whether the media has video streams.
#
# @return [Boolean]
autoload def video_streams?
!video_streams.empty?
end
# Whether the media has a video stream (excluding attached pictures).
#
# @return [Boolean]
autoload def video?
video_streams.any? { |stream| !stream.attached_pic? }
end
# Returns the default video stream (if any).
#
# @return [Stream, nil]
autoload def default_video_stream
return @default_video_stream if instance_variable_defined?(:@default_video_stream)
@default_video_stream = video_streams.find(&:default?) || video_streams.first
end
# Whether the media is rotated (based on the default video stream).
# (e.g. 90°, 180°, 270°)
#
# @return [Boolean]
autoload def rotated?
default_video_stream&.rotated? || false
end
# Whether the media is portrait (based on the default video stream).
#
# @return [Boolean]
autoload def portrait?
default_video_stream&.portrait? || false
end
# Whether the media is landscape (based on the default video stream).
#
# @return [Boolean]
autoload def landscape?
default_video_stream&.landscape? || false
end
# Returns the width of the default video stream (if any).
#
# @return [Integer, nil]
autoload def width
default_video_stream&.width
end
# Returns the raw (unrotated) width of the default video stream (if any).
#
# @return [Integer, nil]
autoload def raw_width
default_video_stream&.raw_width
end
# Returns the height of the default video stream (if any).
#
# @return [Integer, nil]
autoload def height
default_video_stream&.height
end
# Returns the raw (unrotated) height of the default video stream (if any).
#
# @return [Integer, nil]
autoload def raw_height
default_video_stream&.raw_height
end
# Returns the rotation of the default video stream (if any).
#
# @return [Integer, nil]
autoload def rotation
default_video_stream&.rotation
end
# Returns the resolution of the default video stream (if any).
#
# @return [String, nil]
autoload def resolution
default_video_stream&.resolution
end
# Returns the display aspect ratio of the default video stream (if any).
#
# @return [String, nil]
autoload def display_aspect_ratio
default_video_stream&.display_aspect_ratio
end
# Returns the sample aspect ratio of the default video stream (if any).
#
# @return [String, nil]
autoload def sample_aspect_ratio
default_video_stream&.sample_aspect_ratio
end
# Returns the calculated aspect ratio of the default video stream (if any).
#
# @return [String, nil]
autoload def calculated_aspect_ratio
default_video_stream&.calculated_aspect_ratio
end
# Returns the calculated pixel aspect ratio of the default video stream (if any).
#
# @return [String, nil]
autoload def calculated_pixel_aspect_ratio
default_video_stream&.calculated_pixel_aspect_ratio
end
# Returns the color range of the default video stream (if any).
#
# @return [String, nil]
autoload def color_range
default_video_stream&.color_range
end
# Returns the color space of the default video stream (if any).
#
# @return [String, nil]
autoload def color_space
default_video_stream&.color_space
end
# Returns the frame rate (avg_frame_rate) of the default video stream (if any).
#
# @return [Float, nil]
autoload def frame_rate
default_video_stream&.frame_rate
end
# Returns the number of frames of the default video stream (if any).
#
# @return [Integer, nil]
autoload def frames
default_video_stream&.frames
end
# Returns the index of the default video stream (if any).
#
# @return [Integer, nil]
autoload def video_index
default_video_stream&.index
end
# Returns the mapping index of the default video stream (if any).
# (Can be used as an output option for ffmpeg to select the video stream.)
#
# @return [Integer, nil]
autoload def video_mapping_index
video_streams.index(default_video_stream)
end
# Returns the mapping ID of the default video stream (if any).
# (Can be used as an output option for ffmpeg to select the video stream.)
# (e.g. "-map v:0" to select the first video stream.)
#
# @return [String, nil]
autoload def video_mapping_id
index = video_mapping_index
return if index.nil?
"v:#{index}"
end
# Returns the profile of the default video stream (if any).
#
# @return [String, nil]
autoload def video_profile
default_video_stream&.profile
end
# Returns the codec name of the default video stream (if any).
#
# @return [String, nil]
autoload def video_codec_name
default_video_stream&.codec_name
end
# Returns the bit rate of the default video stream (if any).
#
# @return [Integer, nil]
autoload def video_bit_rate
default_video_stream&.bit_rate
end
# Returns the overview of the default video stream (if any).
#
# @return [String, nil]
autoload def video_overview
default_video_stream&.overview
end
# Returns the tags of the default video stream (if any).
#
# @return [Hash, nil]
autoload def video_tags
default_video_stream&.tags
end
# Returns all audio streams.
#
# @return [Array<Stream>, nil]
autoload def audio_streams
return @audio_streams if instance_variable_defined?(:@audio_streams)
@audio_streams = @streams.select(&:audio?)
end
# Whether the media has audio streams.
#
# @return [Boolean]
autoload def audio_streams?
audio_streams && !audio_streams.empty?
end
# Whether the media only contains audio streams and optional attached pictures.
#
# @return [Boolean]
autoload def audio?
audio_streams? && video_streams.all?(&:attached_pic?)
end
# Whether the media is silent (no audio streams).
#
# @return [Boolean]
autoload def silent?
!audio_streams?
end
# Returns the default audio stream (if any).
#
# @return [Stream, nil]
autoload def default_audio_stream
return @default_audio_stream if instance_variable_defined?(:@default_audio_stream)
@default_audio_stream = audio_streams.find(&:default?) || audio_streams.first
end
# Returns the index of the default audio stream (if any).
#
# @return [Integer, nil]
autoload def audio_index
default_audio_stream&.index
end
# Returns the mapping index of the default audio stream (if any).
# (Can be used as an output option for ffmpeg to select the audio stream.)
#
# @return [Integer, nil]
autoload def audio_mapping_index
audio_streams.index(default_audio_stream)
end
# Returns the mapping ID of the default audio stream (if any).
# (Can be used as an output option for ffmpeg to select the audio stream.)
# (e.g. "-map a:0" to select the first audio stream.)
#
# @return [String, nil]
autoload def audio_mapping_id
index = audio_mapping_index
return if index.nil?
"a:#{index}"
end
# Returns the profile of the default audio stream (if any).
#
# @return [String, nil]
autoload def audio_profile
default_audio_stream&.profile
end
# Returns the codec name of the default audio stream (if any).
#
# @return [String, nil]
autoload def audio_codec_name
default_audio_stream&.codec_name
end
# Returns the bit rate of the default audio stream (if any).
#
# @return [Integer, nil]
autoload def audio_bit_rate
default_audio_stream&.bit_rate
end
# Returns the channels of the default audio stream (if any).
#
# @return [String, nil]
autoload def audio_channels
default_audio_stream&.channels
end
# Returns the channel layout of the default audio stream (if any).
#
# @return [String, nil]
autoload def audio_channel_layout
default_audio_stream&.channel_layout
end
# Returns the sample rate of the default audio stream (if any).
#
# @return [Integer, nil]
autoload def audio_sample_rate
default_audio_stream&.sample_rate
end
# Returns the overview of the default audio stream (if any).
#
# @return [String, nil]
autoload def audio_overview
default_audio_stream&.overview
end
# Returns the tags of the default audio stream (if any).
#
# @return [Hash, nil]
autoload def audio_tags
default_audio_stream&.tags
end
# Execute a ffmpeg command with the media as input.
#
# @param args [Array<String>] The arguments to pass to ffmpeg.
# @param inargs [Array<String>] The arguments to pass before the input.
# @yield [report] Reports from the ffmpeg command (see FFMPEG::Reporters).
# @return [Process::Status]
def ffmpeg_execute(*args, inargs: [], reporters: nil, &block)
if reporters.is_a?(Array)
FFMPEG.ffmpeg_execute(*inargs, '-i', path, *args, reporters: reporters, &block)
else
FFMPEG.ffmpeg_execute(*inargs, '-i', path, *args, &block)
end
end
end
end