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
|
# File 'lib/wavesync/file_converter.rb', line 7
def convert(audio, source_file_path, path_resolver, source_format, target_format, padding_seconds: nil, &before_transcode)
needs_format_conversion = target_format.file_type || target_format.sample_rate || target_format.bit_depth
return false unless needs_format_conversion || padding_seconds&.positive?
target_path = path_resolver.resolve(source_file_path, audio, target_file_type: target_format.file_type)
files_to_cleanup = path_resolver.find_files_to_cleanup(target_path, audio)
files_to_cleanup.each { |file| FileUtils.rm_f(file) }
if target_format.file_type
source_converted_path = Pathname(source_file_path).sub_ext(".#{target_format.file_type}")
return false if source_converted_path.exist?
end
if target_path.exist?
existing_duration = Audio.new(target_path.to_s).duration
expected_duration = audio.duration + (padding_seconds || 0)
return false if (existing_duration - expected_duration).abs < DURATION_TOLERANCE_SECONDS
target_path.delete
end
target_path.dirname.mkpath
before_transcode&.call
audio.transcode(target_path.to_s, target_sample_rate: target_format.sample_rate,
target_file_type: target_format.file_type,
target_bit_depth: target_format.bit_depth || source_format.bit_depth,
padding_seconds: padding_seconds)
true
end
|