Class: MmTool::MmMovieStream

Inherits:
Object
  • Object
show all
Defined in:
lib/mm_tool/mm_movie_stream.rb

Overview

A stream of an MmMovie. Instances contain simple accessors to the data made available by ffmpeg, and have knowledge on how to generate useful arguments for ffmpeg and mkvpropedit.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream_data:, source_file:, file_number:, streams_ref:) ⇒ MmMovieStream


Initialize




36
37
38
39
40
41
42
# File 'lib/mm_tool/mm_movie_stream.rb', line 36

def initialize(stream_data:, source_file:, file_number:, streams_ref:)
  @defaults    = MmUserDefaults.shared_user_defaults
  @data        = stream_data
  @source_file = source_file
  @file_number = file_number
  @streams     = streams_ref
end

Instance Attribute Details

#file_numberObject


Attribute accessors




47
48
49
# File 'lib/mm_tool/mm_movie_stream.rb', line 47

def file_number
  @file_number
end

#source_fileObject

Returns the value of attribute source_file.



48
49
50
# File 'lib/mm_tool/mm_movie_stream.rb', line 48

def source_file
  @source_file
end

Class Method Details

.streams(with_files:) ⇒ Object


Given an array of related files, this class method returns an array of MmMovieStreams reflecting the streams present in each of them.




18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mm_tool/mm_movie_stream.rb', line 18

def self.streams(with_files:)
  # Arrays are passed around by reference; when this array is created and
  # used as a reference in each stream, and *also* returned from this class
  # method, everyone will still be using the same reference. It's important
  # below to build up this array without replacing it with another instance.
  streams = []
  with_files.each_with_index do |path, i|
    ff_movie = FFMPEG::Movie.new(path)
    ff_movie.[:streams].each do |stream|
      streams << MmMovieStream.new(stream_data: stream, source_file: path, file_number: i, streams_ref: streams)
    end
  end
  streams
end

Instance Method Details

#action_labelObject


Property - returns a convenient label indicating the

recommended actions for the stream.



174
175
176
# File 'lib/mm_tool/mm_movie_stream.rb', line 174

def action_label
  "#{output_specifier} #{actions.select {|a| a != :interesting}.join(' ')}"
end

#channel_layoutObject


Property - returns the channel layout of the stream.




102
103
104
# File 'lib/mm_tool/mm_movie_stream.rb', line 102

def channel_layout
  @data[:channel_layout]
end

#channelsObject


Property - returns the number of channels of the stream.




95
96
97
# File 'lib/mm_tool/mm_movie_stream.rb', line 95

def channels
  @data[:channels]
end

#codec_nameObject


Property - returns the codec name of the stream.




67
68
69
# File 'lib/mm_tool/mm_movie_stream.rb', line 67

def codec_name
  @data[:codec_name]
end

#codec_typeObject


Property - returns the codec type of the stream.




74
75
76
# File 'lib/mm_tool/mm_movie_stream.rb', line 74

def codec_type
  @data[:codec_type]
end

#coded_heightObject


Property - returns the coded height of the stream.




88
89
90
# File 'lib/mm_tool/mm_movie_stream.rb', line 88

def coded_height
  @data[:coded_height]
end

#coded_widthObject


Property - returns the coded width of the stream.




81
82
83
# File 'lib/mm_tool/mm_movie_stream.rb', line 81

def coded_width
  @data[:coded_width]
end

#copy?Boolean


Property - stream action includes :copy?


Returns:

  • (Boolean)


211
212
213
# File 'lib/mm_tool/mm_movie_stream.rb', line 211

def copy?
  actions.include?(:copy)
end

#default?Boolean


Property - indicates whether or not the stream is the

default stream per its dispositions.

Returns:

  • (Boolean)


182
183
184
# File 'lib/mm_tool/mm_movie_stream.rb', line 182

def default?
  @data[:disposition][:default] == 1
end

#dispositionsObject


Property - returns the disposition flags of the stream as

a comma-separated list for compactness.



136
137
138
139
140
# File 'lib/mm_tool/mm_movie_stream.rb', line 136

def dispositions
  MmMovie.dispositions
      .collect {|symbol| @data[:disposition][symbol]}
      .join(',')
end

#drop?Boolean


Property - stream action includes :drop?


Returns:

  • (Boolean)


204
205
206
# File 'lib/mm_tool/mm_movie_stream.rb', line 204

def drop?
  actions.include?(:drop)
end

#indexObject


Property - returns the index of the stream.




53
54
55
# File 'lib/mm_tool/mm_movie_stream.rb', line 53

def index
  @data[:index]
end

#input_specifierObject


Property - returns the input specifier of the stream.




60
61
62
# File 'lib/mm_tool/mm_movie_stream.rb', line 60

def input_specifier
  "#{@file_number}:#{index}"
end

#instruction_actionObject


Property - returns an instruction for handling the stream,

according to the action(s) determined.



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/mm_tool/mm_movie_stream.rb', line 294

def instruction_action
  if copy?
    "-codec:#{output_specifier} copy \\"
  elsif transcode?
    if codec_type == 'audio'
      encode_to = @defaults[:codecs_audio_preferred][0]
    elsif codec_type == 'video'
      encode_to = @defaults[:codecs_video_preferred][0]
    else
      raise Exception.new "Error: somehow the program branched where it shouldn't have."
    end
    "-codec:#{output_specifier} #{encoder_string(for_codec: encode_to)} \\"
  else
    nil
  end
end

#instruction_dispositionObject


Property - returns an instruction for setting the stream’s

default disposition, if necessary.



333
334
335
336
337
338
339
340
341
# File 'lib/mm_tool/mm_movie_stream.rb', line 333

def instruction_disposition
  set_disposition = output_unique? && !default? && !drop? ? "default " : nil

  if set_disposition
    "-disposition:#{output_specifier} #{set_disposition}\\"
  else
    nil
  end
end

#instruction_inputObject


Property - returns the -i input instruction for this

stream.



273
274
275
276
277
278
279
280
# File 'lib/mm_tool/mm_movie_stream.rb', line 273

def instruction_input
  src = if @file_number == 0
          File.join(File.dirname(@source_file), File.basename(@source_file, '.*') + @defaults[:suffix] + File.extname(@source_file))
        else
          @source_file
        end
  "-i \"#{src}\" \\"
end

#instruction_mapObject


Property - returns the -map instruction for this stream,

according to the action(s) determined.



286
287
288
# File 'lib/mm_tool/mm_movie_stream.rb', line 286

def instruction_map
  drop? ? nil : "-map #{input_specifier} \\"
end

#instruction_metadataObject


Property - returns instructions for setting the metadata

of the stream, if necessary.



315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/mm_tool/mm_movie_stream.rb', line 315

def 
  return [] if @actions.include?(:drop)

  # We only want to set fixed_lang if options allow us to fix the language,
  # and we want to set subtitle language from the filename, if applicable.
  fixed_lang = @defaults[:fix_undefined_language] ? @defaults[:undefined_language] : nil
  lang = subtitle_file_language ? subtitle_file_language : fixed_lang

  result = []
  result << "-metadata:s:#{output_specifier} language=#{lang} \\" if set_language?
  result << "-metadata:s:#{output_specifier} title=\"#{title}\" \\" if title  && ! @defaults[:ignore_titles]
  result
end

#interesting?Boolean


Property - stream action includes :interesting?


Returns:

  • (Boolean)


232
233
234
# File 'lib/mm_tool/mm_movie_stream.rb', line 232

def interesting?
  actions.include?(:interesting)
end

#languageObject


Property - returns the language of the stream, or ‘und’

if the language is not defined.



110
111
112
113
114
115
116
117
118
119
# File 'lib/mm_tool/mm_movie_stream.rb', line 110

def language
  if @data.key?(:tags)
    lang = @data[:tags][:language]
    lang = @data[:tags][:LANGUAGE] unless lang
    lang = 'und' unless lang
  else
    lang = 'und'
  end
  lang
end

#low_quality?Boolean


Property - indicates whether or not the stream is

considered "low quality" based on the application
configuration.

Returns:

  • (Boolean)


191
192
193
194
195
196
197
198
199
# File 'lib/mm_tool/mm_movie_stream.rb', line 191

def low_quality?
  if codec_type == 'audio'
    channels.to_i < @defaults[:min_channels].to_i
  elsif codec_type == 'video'
    coded_width.to_i < @defaults[:min_width].to_i
  else
    false
  end
end

#one_of_a_kind?Boolean


Property - indicates whether or not this stream is the

only one of its type.

Returns:

  • (Boolean)


248
249
250
# File 'lib/mm_tool/mm_movie_stream.rb', line 248

def one_of_a_kind?
  @streams.count {|s| s.codec_type  == codec_type && s != self } == 0
end

#output_indexObject


Property - returns the index of the stream in the output

file.



256
257
258
# File 'lib/mm_tool/mm_movie_stream.rb', line 256

def output_index
  @streams.select {|s| !s.drop? }.index(self)
end

#output_specifierObject


Property - returns a specific output specifier for the

stream, such as v:0 or a:2.



264
265
266
267
# File 'lib/mm_tool/mm_movie_stream.rb', line 264

def output_specifier
  idx = @streams.select {|s| s.codec_type == codec_type && !s.drop?}.index(self)
  idx ? "#{codec_type[0]}:#{idx}" : ''
end

#output_unique?Boolean


Property - indicates whether or not the stream will be

unique for its type at output.

Returns:

  • (Boolean)


240
241
242
# File 'lib/mm_tool/mm_movie_stream.rb', line 240

def output_unique?
  @streams.count {|s| s.codec_type  == codec_type && !s.drop? } == 1
end

#quality_01Object


Property - returns an appropriate “quality” indicator

based on the type of the stream.



146
147
148
149
150
151
152
153
154
# File 'lib/mm_tool/mm_movie_stream.rb', line 146

def quality_01
  if codec_type == 'audio'
    channels
  elsif codec_type == 'video'
    coded_width
  else
    nil
  end
end

#quality_02Object


Property - returns a different appropriate “quality”

indicator based on the type of the stream.



160
161
162
163
164
165
166
167
168
# File 'lib/mm_tool/mm_movie_stream.rb', line 160

def quality_02
  if codec_type == 'audio'
    channel_layout
  elsif codec_type == 'video'
    coded_height
  else
    nil
  end
end

#set_language?Boolean


Property - stream action includes :set_language?


Returns:

  • (Boolean)


225
226
227
# File 'lib/mm_tool/mm_movie_stream.rb', line 225

def set_language?
  actions.include?(:set_language)
end

#titleObject


Property - returns the title of the stream, or nil.




124
125
126
127
128
129
130
# File 'lib/mm_tool/mm_movie_stream.rb', line 124

def title
  if @data.key?(:tags)
    @data[:tags][:title]
  else
    nil
  end
end

#transcode?Boolean


Property - stream action includes :transcode?


Returns:

  • (Boolean)


218
219
220
# File 'lib/mm_tool/mm_movie_stream.rb', line 218

def transcode?
  actions.include?(:transcode)
end