forked from rb2k/viddl-rb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio-helper.rb
53 lines (45 loc) · 2.21 KB
/
audio-helper.rb
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
module ViddlRb
# This class is responsible for extracting audio from video files using ffmpeg.
class AudioHelper
def self.extract(file_name, save_dir)
# capture stderr because ffmpeg expects an output param and will error out
ffmpeg_app_name = ['ffmpeg', 'avconv'].find {|name| ViddlRb::UtilityHelper.os_has?(name)}
puts "Gathering information about the downloaded file."
escaped_input_file_path = UtilityHelper.make_shellsafe_path(File.join(save_dir, file_name))
file_info = Open3.popen3("#{ffmpeg_app_name} -i #{escaped_input_file_path}") {|stdin, stdout, stderr, wait_thr| stderr.read }
puts "Done gathering information about the downloaded file."
if !file_info.to_s.empty?
audio_format_matches = file_info.match(/Audio: (\w*)/)
if audio_format_matches
audio_format = audio_format_matches[1]
puts "detected audio format: #{audio_format}"
else
raise "ERROR: Couldn't find any audio:\n#{file_info.inspect}"
end
extension_mapper = {
'aac' => 'm4a',
'mp3' => 'mp3',
'vorbis' => 'ogg'
}
if extension_mapper.key?(audio_format)
output_extension = extension_mapper[audio_format]
else
#lame fallback
puts "Unknown audio format: #{audio_format}, using name as extension: '.#{audio_format}'."
output_extension = audio_format
end
no_ext_filename = File.basename(file_name, File.extname(file_name))
output_file_path = File.join(save_dir, "#{no_ext_filename}.#{output_extension}")
escaped_output_file_path = Shellwords.escape(output_file_path)
if File.exist?(output_file_path)
puts "Audio file seems to exist already, removing it before extraction."
File.delete(output_file_path)
end
Open3.popen3("#{ffmpeg_app_name} -i #{escaped_input_file_path} -vn -acodec copy #{escaped_output_file_path}") { |stdin, stdout, stderr, wait_thr| stdout.read }
puts "Done extracting audio to #{output_file_path}"
else
raise "ERROR: Error while checking audio track of #{file_path}"
end
end
end
end