diff mbox

[FFmpeg-devel,PATCHv2,1/3] tools: add loudnorm script example to use loudnorm

Message ID 1477258870-22625-1-git-send-email-cus@passwd.hu
State Accepted
Commit 7b8445f03d10faf7ed232e6201bf04ba73d980d7
Headers show

Commit Message

Marton Balint Oct. 23, 2016, 9:41 p.m. UTC
Based on a patch by Kyle Swanson <k@ylo.ph>.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 tools/loudnorm.rb | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100755 tools/loudnorm.rb

Comments

Marton Balint Nov. 11, 2016, 11:57 p.m. UTC | #1
On Sun, 23 Oct 2016, Marton Balint wrote:

> Based on a patch by Kyle Swanson <k@ylo.ph>.
>
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
> tools/loudnorm.rb | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 61 insertions(+)
> create mode 100755 tools/loudnorm.rb
>

Applied.

Regards,
Marton
diff mbox

Patch

diff --git a/tools/loudnorm.rb b/tools/loudnorm.rb
new file mode 100755
index 0000000..4ad374a
--- /dev/null
+++ b/tools/loudnorm.rb
@@ -0,0 +1,61 @@ 
+#!/usr/bin/env ruby
+
+require 'open3'
+require 'json'
+
+ffmpeg_bin = 'ffmpeg'
+target_il  = -24.0
+target_lra = +11.0
+target_tp  = -2.0
+samplerate = '48k'
+
+if ARGF.argv.count != 2
+  puts "Usage: #{$PROGRAM_NAME} input.wav output.wav"
+  exit 1
+end
+
+ff_cmd = Array.new([
+  ffmpeg_bin,
+  '-hide_banner',
+  '-i', ARGF.argv[0],
+  '-af', "loudnorm='I=#{target_il}:LRA=#{target_lra}:tp=#{target_tp}:print_format=json'",
+  '-f', 'null',
+  '-']);
+
+_stdin, _stdout, stderr, wait_thr = Open3.popen3(*ff_cmd)
+
+if wait_thr.value.success?
+  stats = JSON.parse(stderr.read.lines[-12, 12].join)
+  loudnorm_string  = 'loudnorm='
+  loudnorm_string += 'print_format=summary:'
+  loudnorm_string += 'linear=true:'
+  loudnorm_string += "I=#{target_il}:"
+  loudnorm_string += "LRA=#{target_lra}:"
+  loudnorm_string += "tp=#{target_tp}:"
+  loudnorm_string += "measured_I=#{stats['input_i']}:"
+  loudnorm_string += "measured_LRA=#{stats['input_lra']}:"
+  loudnorm_string += "measured_tp=#{stats['input_tp']}:"
+  loudnorm_string += "measured_thresh=#{stats['input_thresh']}:"
+  loudnorm_string += "offset=#{stats['target_offset']}"
+else
+  puts stderr.read
+  exit 1
+end
+
+ff_cmd = Array.new([
+  ffmpeg_bin,
+  '-y', '-hide_banner',
+  '-i', ARGF.argv[0],
+  '-af', loudnorm_string,
+  '-ar', samplerate,
+  ARGF.argv[1].to_s]);
+
+_stdin, _stdout, stderr, wait_thr = Open3.popen3(*ff_cmd)
+
+if wait_thr.value.success?
+  puts stderr.read.lines[-12, 12].join
+  exit 0
+else
+  puts stderr.read
+  exit 1
+end