diff mbox

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

Message ID 1476646324-15429-1-git-send-email-cus@passwd.hu
State Superseded
Headers show

Commit Message

Marton Balint Oct. 16, 2016, 7:32 p.m. UTC
From: Kyle Swanson <k@ylo.ph>

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

Comments

Nicolas George Oct. 16, 2016, 7:37 p.m. UTC | #1
Le quintidi 25 vendémiaire, an CCXXV, Marton Balint a écrit :
> From: Kyle Swanson <k@ylo.ph>
> 
> Signed-off-by: Kyle Swanson <k@ylo.ph>
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
>  tools/loudnorm.rb | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 60 insertions(+)
>  create mode 100755 tools/loudnorm.rb
> 
> diff --git a/tools/loudnorm.rb b/tools/loudnorm.rb
> new file mode 100755
> index 0000000..39deb70
> --- /dev/null
> +++ b/tools/loudnorm.rb
> @@ -0,0 +1,60 @@
> +#!/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_string  = "#{ffmpeg_bin} -hide_banner "
> +ff_string += "-i #{ARGF.argv[0]} "
> +ff_string += '-af loudnorm='
> +ff_string += "I=#{target_il}:"
> +ff_string += "LRA=#{target_lra}:"
> +ff_string += "tp=#{target_tp}:"
> +ff_string += 'print_format=json '
> +ff_string += '-f null -'

This will break if the input file name contains special characters. I think
I see popen3 can accept an array of arguments, it would be better to use it.

> +
> +_stdin, _stdout, stderr, wait_thr = Open3.popen3(ff_string)
> +
> +if wait_thr.value.success?
> +  stats = JSON.parse(stderr.read.lines[-12, 12].join)
> +  loudnorm_string  = '-af 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_string  = "#{ffmpeg_bin} -y -hide_banner "
> +ff_string += "-i #{ARGF.argv[0]} "
> +ff_string += "#{loudnorm_string} "
> +ff_string += "-ar #{samplerate} "
> +ff_string += ARGF.argv[1].to_s
> +
> +_stdin, _stdout, stderr, wait_thr = Open3.popen3(ff_string)
> +
> +if wait_thr.value.success?
> +  puts stderr.read.lines[-12, 12].join
> +  exit 0
> +else
> +  puts stderr.read
> +  exit 1
> +end

Regards,
diff mbox

Patch

diff --git a/tools/loudnorm.rb b/tools/loudnorm.rb
new file mode 100755
index 0000000..39deb70
--- /dev/null
+++ b/tools/loudnorm.rb
@@ -0,0 +1,60 @@ 
+#!/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_string  = "#{ffmpeg_bin} -hide_banner "
+ff_string += "-i #{ARGF.argv[0]} "
+ff_string += '-af loudnorm='
+ff_string += "I=#{target_il}:"
+ff_string += "LRA=#{target_lra}:"
+ff_string += "tp=#{target_tp}:"
+ff_string += 'print_format=json '
+ff_string += '-f null -'
+
+_stdin, _stdout, stderr, wait_thr = Open3.popen3(ff_string)
+
+if wait_thr.value.success?
+  stats = JSON.parse(stderr.read.lines[-12, 12].join)
+  loudnorm_string  = '-af 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_string  = "#{ffmpeg_bin} -y -hide_banner "
+ff_string += "-i #{ARGF.argv[0]} "
+ff_string += "#{loudnorm_string} "
+ff_string += "-ar #{samplerate} "
+ff_string += ARGF.argv[1].to_s
+
+_stdin, _stdout, stderr, wait_thr = Open3.popen3(ff_string)
+
+if wait_thr.value.success?
+  puts stderr.read.lines[-12, 12].join
+  exit 0
+else
+  puts stderr.read
+  exit 1
+end