-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommand_args.rb
More file actions
216 lines (183 loc) · 7.57 KB
/
command_args.rb
File metadata and controls
216 lines (183 loc) · 7.57 KB
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
# frozen_string_literal: true
require_relative 'raw_command_args'
module FFMPEG
# A helper class for composing command arguments for FFMPEG.
# It provides a DSL for setting arguments based on media properties.
#
# @example
# args = FFMPEG::CommandArgs.compose(media) do
# map media.video_mapping_id do
# video_codec_name 'libx264'
# frame_rate 30
# end
# end
# args.to_s # "-map 0:v:0 -c:v:0 libx264 -r 30"
class CommandArgs < RawCommandArgs
STANDARD_FRAME_RATES = [12, 24, 25, 30, 50, 60, 90, 120, 240].freeze
STANDARD_AUDIO_SAMPLE_RATES = [
8000, 11_025, 16_000, 22_050, 32_000, 44_100,
48_000, 88_200, 96_000, 176_400, 192_000
].freeze
class << self
# Composes a new instance of CommandArgs with the given media.
# The block is evaluated in the context of the new instance.
#
# @param media [FFMPEG::Media] The media to transcode.
# @param context [Hash, nil] Additional context for composing the arguments.
# @return [FFMPEG::CommandArgs] The new FFMPEG::CommandArgs object.
def compose(media, context: nil, &block)
new(media, context:).tap do |args|
args.instance_exec(&block) if block_given?
end
end
end
attr_reader :media
# @param media [FFMPEG::Media] The media to transcode.
# @param context [Hash, nil] Additional context for composing the arguments.
def initialize(media, context: nil)
@media = media
super(context:)
end
# Sets the frame rate to the minimum of the current frame rate and the target value.
#
# @param target_value [Integer, Float] The target frame rate.
# @return [self]
def frame_rate(target_value)
return self if target_value.nil?
super(adjusted_frame_rate(target_value))
end
# Sets the video bit rate to the minimum of the current video bit rate and the target value.
# The target value can be an Integer or a String (e.g.: 128k or 1M).
#
# @param target_value [Integer, String] The target bit rate.
# @param kwargs [Hash] Additional options (see FFMPEG::RawCommandArgs#video_bit_rate).
# @return [self]
def video_bit_rate(target_value, **kwargs)
return self if target_value.nil?
super(adjusted_video_bit_rate(target_value), **kwargs)
end
# Sets the minimum video bit rate to the minimum of the current video bit rate and the target value.
# The target value can be an Integer or a String (e.g.: 128k or 1M).
#
# @param target_value [Integer, String] The target bit rate.
# @return [self]
def min_video_bit_rate(target_value)
return self if target_value.nil?
super(adjusted_video_bit_rate(target_value))
end
# Sets the maximum video bit rate to the minimum of the current video bit rate and the target value.
# The target value can be an Integer or a String (e.g.: 128k or 1M).
#
# @param target_value [Integer, String] The target bit rate.
# @return [self]
def max_video_bit_rate(target_value)
return self if target_value.nil?
super(adjusted_video_bit_rate(target_value))
end
# Sets the audio bit rate to the minimum of the current audio bit rate and the target value.
# The target value can be an Integer or a String (e.g.: 128k or 1M).
#
# @param target_value [Integer, String] The target bit rate
# @return [self]
def audio_bit_rate(target_value, **kwargs)
return self if target_value.nil?
super(adjusted_audio_bit_rate(target_value), **kwargs)
end
# Sets the audio sample rate to the minimum of the current audio sample rate and the target value.
#
# @param target_value [Integer] The target sample rate.
# @return [self]
def audio_sample_rate(target_value)
return self if target_value.nil?
super(adjusted_audio_sample_rate(target_value))
end
# Sets the audio channels to the minimum of the current audio channels and the target value.
#
# @param target_value [Integer] The target audio channels.
# @return [self]
def audio_channels(target_value)
return self if target_value.nil?
super(adjusted_audio_channels(target_value))
end
# Returns the minimum of the current frame rate and the target value.
#
# @param target_value [Integer, Float] The target frame rate.
# @return [Numeric]
def adjusted_frame_rate(target_value)
return target_value if media.frame_rate.nil?
return target_value if media.frame_rate <= 0
return target_value if media.frame_rate > target_value
STANDARD_FRAME_RATES.min_by { (_1 - media.frame_rate).abs }
end
# Returns the minimum of the current video bit rate and the target value.
# The target value can be an Integer or a String (e.g.: 128k or 1M).
# The result is a String with the value in kilobits.
#
# @param target_value [Integer, String] The target video bit rate.
# @return [String]
def adjusted_video_bit_rate(target_value)
min_bit_rate(media.video_bit_rate, target_value)
end
# Returns the minimum of the current audio bit rate and the target value.
# The target value can be an Integer or a String (e.g.: 128k or 1M).
# The result is a String with the value in kilobits.
#
# @param target_value [Integer, String] The target audio bit rate.
# @return [String]
def adjusted_audio_bit_rate(target_value)
min_bit_rate(media.audio_bit_rate, target_value)
end
# Returns the minimum of the current audio sample rate and the target value.
# Returns the target value if the current sample rate is nil or zero/negative.
# If the media sample rate is lower than the target, returns the closest standard sample rate.
#
# @param target_value [Integer] The target audio sample rate.
# @return [Integer]
def adjusted_audio_sample_rate(target_value)
return target_value if media.audio_sample_rate.nil?
return target_value if media.audio_sample_rate <= 0
return target_value if media.audio_sample_rate > target_value
STANDARD_AUDIO_SAMPLE_RATES.min_by { (_1 - media.audio_sample_rate).abs }
end
# Returns the minimum of the current audio channels and the target value.
# Returns the target value if the current audio channels is nil or zero/negative.
# If the media audio channels is lower than the target, returns the current audio channels.
#
# @param target_value [Integer] The target audio channels.
# @return [Integer]
def adjusted_audio_channels(target_value)
return target_value if media.audio_channels.nil?
return target_value if media.audio_channels <= 0
return target_value if media.audio_channels > target_value
media.audio_channels
end
private
def min_bit_rate(*values)
bit_rate =
values.filter_map do |value|
# Some muxers (webm) might not expose birate under certain conditions
next false if value.nil?
next (value.positive? ? value : false) if value.is_a?(Integer)
unless value.is_a?(String)
raise ArgumentError,
"Unknown bit rate format #{value.class}, expected #{Integer} or #{String}"
end
match = value.match(/\A([1-9]\d*)([kM])\z/)
unless match
raise ArgumentError,
"Unknown bit rate format #{value}, expected [1-9]\\d*[kM]"
end
value = match[1].to_i
case match[2]
when 'k'
value * 1_000
when 'M'
value * 1_000_000
else
value
end
end.min
"#{(bit_rate.to_f / 1000).round}k"
end
end
end