forked from instructure/ruby-ffmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscoder_spec.rb
More file actions
148 lines (120 loc) · 4.43 KB
/
transcoder_spec.rb
File metadata and controls
148 lines (120 loc) · 4.43 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
# frozen_string_literal: true
require_relative '../spec_helper'
module FFMPEG
describe Transcoder do
describe '#process' do
let(:media) { Media.new(fixture_media_file('landscape@4k60.mp4')) }
let(:preset1) do
Preset.new(filename: '%<basename>s.mp4') do
video_codec_name 'libx264'
audio_codec_name 'aac'
map media.video_mapping_id do
filter Filters.scale(width: -2, height: 360)
constant_rate_factor 28
end
map media.audio_mapping_id do
audio_bit_rate '96k'
end
end
end
let(:preset2) do
Preset.new(filename: '%<basename>s.aac') do
audio_codec_name 'aac'
map media.audio_mapping_id do
audio_bit_rate '96k'
end
end
end
let(:retries) { 0 }
subject do
described_class.new(presets: [preset1, preset2], retries:) do
raw_arg '-noautorotate'
context :retry do
raw_arg '-xerror'
end
end
end
it 'transcodes a multimedia file using the specified presets' do
output_path = File.join(tmp_dir, SecureRandom.hex(4))
expect(media).to receive(:ffmpeg_execute).and_wrap_original do |method, *args, **kwargs, &block|
expect(args).to eq(
%W[
-c:v libx264 -c:a aac
-map v:0 -filter:v scale=w=-2:h=360 -crf 28
-map a:0 -b:a 96k
#{output_path}.mp4
-c:a aac
-map a:0 -b:a 96k
#{output_path}.aac
]
)
expect(kwargs).to match(
hash_including(
inargs: ['-noautorotate'],
reporters: nil
)
)
method.call(*args, **kwargs, &block)
end
reports = []
status = subject.process(media, output_path) do |report|
reports << report
end
expect(status).to be_a(Transcoder::Status)
expect(status.success?).to be(true)
expect(status.paths).to eq(%W[#{output_path}.mp4 #{output_path}.aac])
expect(status.media).to all(be_a(Media))
expect(status.media.first.video?).to be(true)
expect(status.media.first.height).to eq(360)
expect(status.media.first.streams.length).to eq(2)
expect(status.media.first.audio_bit_rate).to be_within(15_000).of(96_000)
expect(status.media.last.audio?).to be(true)
expect(status.media.last.streams.length).to eq(1)
expect(status.media.last.audio_bit_rate).to be_within(15_000).of(96_000)
expect(reports.length).to be >= 1
expect(reports).to all(be_a(Reporters::Output))
end
context 'when the transcoding process finishes with non-zero exit status' do
let(:retries) { 1 }
it 'retries up to the set number of times' do
output_path = File.join(tmp_dir, SecureRandom.hex(4))
attempts = 0
status = double(Transcoder::Status, success?: false)
expect(media).to receive(:ffmpeg_execute).twice do |*_args, inargs:, **_kwargs|
attempts += 1
if attempts == 2
allow(status).to receive(:success?).and_return(true)
expect(inargs).to include('-xerror')
else
expect(inargs).not_to include('-xerror')
end
status
end
expect(subject.process(media, output_path)).to be(status)
expect(attempts).to eq(2)
end
end
it 'can run ffmpeg in a different working directory (chdir)' do
output_path = File.join(tmp_dir, SecureRandom.hex(4))
status = double(Transcoder::Status, success?: true)
expect(FFMPEG::IO).to receive(:popen3)
.with(any_args, chdir: tmp_dir)
.and_return(status)
subject.process(media, output_path, chdir: tmp_dir)
end
end
describe '#process!' do
it 'calls assert! on the result of process' do
media = instance_double(Media)
output_path = File.join(tmp_dir, SecureRandom.hex(4))
status = instance_double(Transcoder::Status)
block = proc {}
expect(status).to receive(:assert!).and_return(status)
expect(subject).to receive(:process)
.with(media, output_path, chdir: nil, &block)
.and_return(status)
expect(subject.process!(media, output_path, &block)).to eq(status)
end
end
end
end