-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize-volume.rb
executable file
·71 lines (62 loc) · 2.85 KB
/
normalize-volume.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'tempfile'
require 'json'
targets = Dir.glob("#{ARGV[0]}/**/*.wav")
@mezzanines = []
@access_files = []
def normalize_mezz(target)
puts "Processing: #{target}"
temp = Tempfile.new
file_path = File.dirname(target)
file_base = File.basename(target, ".*")
output_name = file_path + '/' + file_base + '_48kHz.wav'
ffprobe_command = "ffprobe -v quiet -print_format json -show_format -show_streams -select_streams a '" + target + "'"
ffmpeg_command = "ffmpeg -i '" + target + "' -vn -af volumedetect -f null - 2> #{temp.path}"
`#{ffmpeg_command}`
ffprobe_out = JSON.parse(`#{ffprobe_command}`)
normalization_data = File.readlines(temp).select {|line| line.include? 'max_volume'}
codingHistory = nil
normalization_value = (-3.0 - normalization_data[0].split(' ')[4].to_f)
puts normalization_data
puts normalization_value
# get values for BEXT
if codingHistory.nil?
if `ffmpeg -i "#{target}" -c:v copy -c:a pcm_s16le -ar 48000 -dither_method triangular -map_metadata -1 -af volume="#{normalization_value}dB" "#{output_name}"`
@mezzanines << output_name
end
else
mValue = codingHistory.split(',').select {|value| value.include?("M=")}.last
updatedCodingHistory = codingHistory + "\nA=PCM,F=48000,W=16,#{mValue},T=FFmpeg; mezzanine"
originator = ffprobe_out["format"]['tags']['encoded_by']
description = ffprobe_out["format"]['tags']["comment"].split("Original")[0]
date = Time.now.to_s.split(' ')[0]
time = Time.now.to_s.split(' ')[1]
if `ffmpeg -i "#{target}" -c:v copy -c:a pcm_s16le -ar 48000 -dither_method triangular -map_metadata -1 -write_bext 1 -metadata "origination_date=#{date}" -metadata "origination_time=#{time}" -metadata "description=#{description}" -metadata "originator=#{originator}" -metadata "coding_history=#{updatedCodingHistory}" -metadata "IARL=#{originator}" -af volume="#{normalization_value}dB" "#{output_name}"`
@mezzanines << output_name
end
end
output_name
end
def make_access(target)
file_path = File.dirname(target)
file_base = File.basename(target, ".*")
output_name = file_path + '/' + file_base + '.mp3'
if `ffmpeg -i "#{target}" -write_id3v1 1 -id3v2_version 3 -out_sample_rate 48k -qscale:a 1 "#{output_name}"`
@access_files << output_name
end
end
def clean_up_files(targets, mezzanines, access_files)
accessFiles = "#{ARGV[0]}/accessFiles"
mezzanine = "#{ARGV[0]}/mezzanine"
preservationFiles = "#{ARGV[0]}/preservationFiles"
Dir.mkdir(accessFiles)
Dir.mkdir(mezzanine)
Dir.mkdir(preservationFiles)
targets.each {|file| FileUtils.mv(file, preservationFiles)}
mezzanines.each {|file| FileUtils.mv(file, mezzanine)}
access_files.each {|file| FileUtils.mv(file, accessFiles)}
end
targets.each do |target|
new_target = normalize_mezz(target)
make_access(new_target) unless new_target.nil?
end
clean_up_files(targets, @mezzanines, @access_files)