-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathffmpeg_spec.rb
More file actions
326 lines (269 loc) · 11.4 KB
/
Copy pathffmpeg_spec.rb
File metadata and controls
326 lines (269 loc) · 11.4 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
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
# frozen_string_literal: true
require 'spec_helper'
describe FFMPEG do
before do
described_class.instance_variable_set(:@logger, nil)
described_class.instance_variable_set(:@ffmpeg_binary, nil)
described_class.instance_variable_set(:@ffmpeg_version, nil)
described_class.instance_variable_set(:@ffprobe_binary, nil)
described_class.instance_variable_set(:@ffprobe_version, nil)
end
after do
described_class.instance_variable_set(:@logger, nil)
described_class.instance_variable_set(:@ffmpeg_binary, nil)
described_class.instance_variable_set(:@ffmpeg_version, nil)
described_class.instance_variable_set(:@ffprobe_binary, nil)
described_class.instance_variable_set(:@ffprobe_version, nil)
end
describe '.logger' do
it 'defaults to a Logger with info level' do
expect(described_class.logger).to be_instance_of(Logger)
expect(described_class.logger.level).to eq(Logger::INFO)
end
end
describe '.logger=' do
it 'assigns the logger' do
logger = Logger.new($stdout)
described_class.logger = logger
expect(described_class.logger).to eq(logger)
end
end
describe '.ffmpeg_binary' do
it 'defaults to finding from path' do
expect(described_class).to receive(:which).and_return('/path/to/ffmpeg')
expect(described_class.ffmpeg_binary).to eq('/path/to/ffmpeg')
end
end
describe '.ffmpeg_binary=' do
it 'assigns the ffmpeg binary' do
expect(File).to receive(:executable?).with('/path/to/ffmpeg').and_return(true)
described_class.ffmpeg_binary = '/path/to/ffmpeg'
expect(described_class.ffmpeg_binary).to eq('/path/to/ffmpeg')
end
it 'clears the cached ffmpeg version' do
expect(File).to receive(:executable?).with('/path/to/ffmpeg').and_return(true)
described_class.instance_variable_set(:@ffmpeg_version, '4.4.6')
described_class.ffmpeg_binary = '/path/to/ffmpeg'
expect(described_class.instance_variable_get(:@ffmpeg_version)).to be_nil
end
it 'clears the cached muxers' do
expect(File).to receive(:executable?).with('/path/to/ffmpeg').and_return(true)
described_class.instance_variable_set(:@muxers, Set.new(['mp4']))
described_class.ffmpeg_binary = '/path/to/ffmpeg'
expect(described_class.instance_variable_get(:@muxers)).to be_nil
end
context 'when the assigned value is nil' do
it 'clears the ffmpeg binary and version' do
described_class.instance_variable_set(:@ffmpeg_binary, '/path/to/ffmpeg')
described_class.instance_variable_set(:@ffmpeg_version, '4.4.6')
described_class.ffmpeg_binary = nil
expect(described_class.instance_variable_get(:@ffmpeg_binary)).to be_nil
expect(described_class.instance_variable_get(:@ffmpeg_version)).to be_nil
end
end
context 'when the assigned value is not executable' do
it 'raises an error' do
expect(File).to receive(:executable?).with('/path/to/ffmpeg').and_return(false)
expect { described_class.ffmpeg_binary = '/path/to/ffmpeg' }.to raise_error(Errno::ENOENT)
end
end
end
describe '.ffmpeg_version' do
it 'returns the version string from ffmpeg binary' do
expect(FFMPEG::IO).to receive(:capture3)
.with(described_class.ffmpeg_binary, '-version')
.and_return(['ffmpeg version 4.4.6 Copyright (c) 2000-2025 the FFmpeg developers', ''])
expect(described_class.ffmpeg_version).to eq('4.4.6')
end
it 'caches the version' do
expect(FFMPEG::IO).to receive(:capture3)
.once
.with(described_class.ffmpeg_binary, '-version')
.and_return(['ffmpeg version 8.0 Copyright (c) 2000-2025 the FFmpeg developers', ''])
described_class.ffmpeg_version
expect(described_class.ffmpeg_version).to eq('8.0')
end
it 'handles versions with different formats' do
expect(FFMPEG::IO).to receive(:capture3)
.with(described_class.ffmpeg_binary, '-version')
.and_return(['ffmpeg version 5.1.2-static https://johnvansickle.com/ffmpeg/', ''])
expect(described_class.ffmpeg_version).to eq('5.1.2')
end
end
describe '.ffmpeg_version?' do
context 'when the version matches the pattern' do
it 'returns true' do
expect(FFMPEG::IO).to receive(:capture3)
.with(described_class.ffmpeg_binary, '-version')
.and_return(['ffmpeg version 4.4.6 Copyright (c) 2000-2025 the FFmpeg developers', ''])
expect(described_class.ffmpeg_version?('4.4')).to be true
expect(described_class.ffmpeg_version?(/^4\.\d+/)).to be true
end
end
context 'when the version does not match the pattern' do
it 'returns false' do
expect(FFMPEG::IO).to receive(:capture3)
.with(described_class.ffmpeg_binary, '-version')
.and_return(['ffmpeg version 4.4.6 Copyright (c) 2000-2025 the FFmpeg developers', ''])
expect(described_class.ffmpeg_version?('5')).to be false
expect(described_class.ffmpeg_version?(/^5/)).to be false
end
end
end
describe '.muxers' do
before { described_class.instance_variable_set(:@muxers, nil) }
after { described_class.instance_variable_set(:@muxers, nil) }
it 'returns a set of available muxer names' do
expect(described_class.muxers).to be_a(Set)
expect(described_class.muxers).to include('mp4', 'matroska', 'webm')
end
it 'caches the result' do
expect(described_class).to receive(:ffmpeg_capture3).once.and_call_original
described_class.muxers
described_class.muxers
end
end
describe '.ffmpeg_execute' do
it 'returns the process status' do
args = ['-i', fixture_media_file('hello.wav'), '-f', 'null', '-']
status = described_class.ffmpeg_execute(*args)
expect(status).to be_a(FFMPEG::Status)
expect(status.exitstatus).to eq(0)
end
it 'forwards spawn parameter to popen3' do
spawn = { chdir: '/tmp/test' }
expect(FFMPEG::IO).to receive(:popen3).with(any_args, chdir: '/tmp/test')
described_class.ffmpeg_execute(spawn:)
end
context 'when ffmpeg hangs' do
before do
described_class.ffmpeg_binary = fixture_file('bin/mock-ffmpeg')
end
context 'with IO timeout set' do
before do
FFMPEG::IO.timeout = 0.5
end
after do
FFMPEG::IO.remove_instance_variable(:@timeout)
end
it 'raises IO::TimeoutError' do
expect { described_class.ffmpeg_execute!('hello', 'world') }.to raise_error(IO::TimeoutError)
end
end
context 'with operation timeout set' do
it 'raises Timeout::Error' do
expect { described_class.ffmpeg_execute!('hello', 'world', timeout: 0.5) }.to raise_error(Timeout::Error)
end
end
end
end
describe '.ffmpeg_execute!' do
it 'raises an error when the process is unsuccessful' do
expect { described_class.ffmpeg_execute!('-v') }.to raise_error(FFMPEG::Error)
end
context 'when called in a subprocess' do
before do
described_class.ffmpeg_binary = fixture_file('bin/mock-ffmpeg')
end
context 'with exit signal traps' do
it 'does not raise an error' do
pid = fork do
Signal.trap('QUIT') {} # rubocop:disable Lint/EmptyBlock
described_class.ffmpeg_execute!('-n=3', '-progress', 'hello', 'world')
end
sleep 1
Process.kill('QUIT', pid)
Process.wait(pid)
expect($CHILD_STATUS&.exitstatus).to eq(0)
end
end
context 'without exit signal traps' do
it 'does not raise an error' do
pid = fork do
described_class.ffmpeg_execute!('-n=10', '-progress', 'hello', 'world')
end
sleep 1
Process.kill('QUIT', pid)
_, status = Process.wait2(pid)
expect(status.exitstatus).not_to eq(0)
end
end
end
end
describe '.ffprobe_binary' do
it 'defaults to finding from path' do
expect(described_class).to receive(:which).and_return('/path/to/ffprobe')
expect(described_class.ffprobe_binary).to eq('/path/to/ffprobe')
end
end
describe '.ffprobe_binary=' do
it 'assigns the ffprobe binary' do
expect(File).to receive(:executable?).with('/path/to/ffprobe').and_return(true)
described_class.ffprobe_binary = '/path/to/ffprobe'
expect(described_class.ffprobe_binary).to eq '/path/to/ffprobe'
end
it 'clears the cached ffprobe version' do
expect(File).to receive(:executable?).with('/path/to/ffprobe').and_return(true)
described_class.instance_variable_set(:@ffprobe_version, '4.4.6')
described_class.ffprobe_binary = '/path/to/ffprobe'
expect(described_class.instance_variable_get(:@ffprobe_version)).to be_nil
end
context 'when the assigned value is nil' do
it 'clears the ffprobe binary and version' do
described_class.instance_variable_set(:@ffprobe_binary, '/path/to/ffprobe')
described_class.instance_variable_set(:@ffprobe_version, '4.4.6')
described_class.ffprobe_binary = nil
expect(described_class.instance_variable_get(:@ffprobe_binary)).to be_nil
expect(described_class.instance_variable_get(:@ffprobe_version)).to be_nil
end
end
context 'when the assigned value is not executable' do
it 'raises an error' do
expect(File).to receive(:executable?).with('/path/to/ffprobe').and_return(false)
expect { described_class.ffprobe_binary = '/path/to/ffprobe' }.to raise_error(Errno::ENOENT)
end
end
end
describe '.ffprobe_version' do
it 'returns the version string from ffprobe binary' do
expect(FFMPEG::IO).to receive(:capture3)
.with(described_class.ffprobe_binary, '-version')
.and_return(['ffprobe version 4.4.6 Copyright (c) 2007-2025 the FFmpeg developers', ''])
expect(described_class.ffprobe_version).to eq('4.4.6')
end
it 'caches the version' do
expect(FFMPEG::IO).to receive(:capture3)
.once
.with(described_class.ffprobe_binary, '-version')
.and_return(['ffprobe version 8.0 Copyright (c) 2007-2025 the FFmpeg developers', ''])
described_class.ffprobe_version
expect(described_class.ffprobe_version).to eq('8.0')
end
it 'handles versions with different formats' do
expect(FFMPEG::IO).to receive(:capture3)
.with(described_class.ffprobe_binary, '-version')
.and_return(['ffprobe version 5.1.2-static https://johnvansickle.com/ffmpeg/', ''])
expect(described_class.ffprobe_version).to eq('5.1.2')
end
end
describe '.ffprobe_version?' do
context 'when the version matches the pattern' do
it 'returns true' do
expect(FFMPEG::IO).to receive(:capture3)
.with(described_class.ffprobe_binary, '-version')
.and_return(['ffprobe version 4.4.6 Copyright (c) 2007-2025 the FFmpeg developers', ''])
expect(described_class.ffprobe_version?('4.4')).to be true
expect(described_class.ffprobe_version?(/^4\.\d+/)).to be true
end
end
context 'when the version does not match the pattern' do
it 'returns false' do
expect(FFMPEG::IO).to receive(:capture3)
.with(described_class.ffprobe_binary, '-version')
.and_return(['ffprobe version 4.4.6 Copyright (c) 2007-2025 the FFmpeg developers', ''])
expect(described_class.ffprobe_version?('5')).to be false
expect(described_class.ffprobe_version?(/^5/)).to be false
end
end
end
end