Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds LKFS attribute and allows loudness normalization in transcoding #185

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ options = {
movie.transcode("movie.mp4", options)
```

Normalize the LKFS to -24:

``` ruby
options = { loudness_normalization: movie.normalize_command }

movie.transcode("movie.mp4", options)
```

The transcode function returns a Movie object for the encoded file.

``` ruby
Expand Down
4 changes: 4 additions & 0 deletions lib/ffmpeg/encoding_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def convert_seek_time(value)
["-ss", value]
end

def convert_loudness_normalization(value)
["-af", value]
end

def convert_screenshot(value)
result = []
unless self[:vframes]
Expand Down
39 changes: 38 additions & 1 deletion lib/ffmpeg/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
require 'multi_json'
require 'uri'
require 'net/http'
require 'json'

module FFMPEG
class Movie
attr_reader :path, :duration, :time, :bitrate, :rotation, :creation_time
attr_reader :video_stream, :video_codec, :video_bitrate, :colorspace, :width, :height, :sar, :dar, :frame_rate
attr_reader :video_stream, :video_codec, :video_bitrate, :colorspace, :width, :height, :sar, :dar, :level, :profile, :frame_rate
attr_reader :audio_streams, :audio_stream, :audio_codec, :audio_bitrate, :audio_sample_rate, :audio_channels, :audio_tags
attr_reader :max_volume, :mean_volume, :lkfs, :loudness_lra, :loudness_true_peak, :loudness_threshold, :target_offset, :normalize_command
attr_reader :container
attr_reader :metadata, :format_tags

Expand All @@ -27,6 +29,8 @@ def initialize(path)

@path = path

set_loudness

# ffmpeg will output to stderr
command = [FFMPEG.ffprobe_binary, '-i', path, *%w(-print_format json -show_format -show_streams -show_error)]
std_output = ''
Expand All @@ -37,9 +41,18 @@ def initialize(path)
std_error = stderr.read unless stderr.nil?
end

get_levels_command = [FFMPEG.ffmpeg_binary, '-i', path, *%w(-af volumedetect -f null /dev/null)]
output = Open3.popen3(*get_levels_command) do |stdin, stdout, stderr|
std_error = stderr.read unless stderr.nil?
end

fix_encoding(std_output)
fix_encoding(std_error)

@mean_volume = output[/mean_volume:\ (.*)/, 1]

@max_volume = output[/max_volume:\ (.*)/, 1]

begin
@metadata = MultiJson.load(std_output, symbolize_keys: true)
rescue MultiJson::ParseError
Expand Down Expand Up @@ -84,6 +97,8 @@ def initialize(path)
@video_bitrate = video_stream[:bit_rate].to_i
@sar = video_stream[:sample_aspect_ratio]
@dar = video_stream[:display_aspect_ratio]
@level = video_stream[:level]
@profile = video_stream[:profile]

@frame_rate = unless video_stream[:avg_frame_rate] == '0/0'
Rational(video_stream[:avg_frame_rate])
Expand Down Expand Up @@ -134,6 +149,28 @@ def initialize(path)
@invalid = true if std_error.include?("could not find codec parameters")
end

def set_loudness
lkfs_command = [FFMPEG.ffmpeg_binary, '-i', path, '-af', *%w(loudnorm=I=-24:TP=-1.5:LRA=11:print_format=json -f null -)]
_stdin, _stdout, std_err, wait_thr = Open3.popen3(*lkfs_command)

if wait_thr.value.success?
stats = JSON.parse(std_err.read.lines[-12, 12].join)
@lkfs = stats['input_i']
@loudness_lra = stats['input_lra']
@loudness_threshold = stats['input_thresh']
@loudness_true_peak = stats['input_tp']
@target_offset = stats['target_offset']

# This attribute can be used as the input to 'loudness_normalization' when doing two-pass transcoding
@normalize_command = "loudnorm=I=-24:TP=-1.5:LRA=11:measured_I=#{@lkfs}"\
":measured_LRA=#{@loudness_lra}:measured_TP=#{@loudness_true_peak}:measured_thresh"\
"=#{@loudness_threshold}:offset=#{@target_offset}:linear=true:print_format=summary"
else
raise "Could not retrieve lkfs"
end
std_err.flush
end

def unsupported_streams(std_error)
[].tap do |stream_indices|
std_error.each_line do |line|
Expand Down
7 changes: 7 additions & 0 deletions spec/ffmpeg/encoding_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ module FFMPEG
expect(EncodingOptions.new(frame_rate: 29.9).to_a).to eq(%w(-r 29.9))
end

it "should convert loudness normalization" do
command = "loudnorm=I=-24:TP=-1.5:"\
"LRA=11:measured_I=-62.95:measured_LRA=1.10:measured_TP=-37.62"\
":measured_thresh=-72.95:offset=0.77:linear=true:print_format=summary"
expect(EncodingOptions.new(loudness_normalization: command).to_a).to include("-af", command)
end

it "should convert the resolution" do
expect(EncodingOptions.new(resolution: "640x480").to_a).to include("-s", "640x480")
end
Expand Down
34 changes: 34 additions & 0 deletions spec/ffmpeg/movie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,40 @@ module FFMPEG
expect(movie.bitrate).to eq(481846)
end

it "should expose mean volume" do
expect(movie.mean_volume).to eq("-72.4 dB")
end

it "should expose max volume" do
expect(movie.max_volume).to eq("-38.0 dB")
end

it "should expose lkfs" do
expect(movie.lkfs).to eq("-62.95")
end

it "should expose loudness threshold" do
expect(movie.loudness_threshold).to eq("-72.95")
end

it "should expose loudness true peak" do
expect(movie.loudness_true_peak).to eq( "-37.62")
end

it "should expose loudness LRA" do
expect(movie.loudness_lra).to eq("1.10")
end

it "should expose loudness target offset" do
expect(movie.target_offset).to eq("0.77")
end

it "should calculate loudness 2-pass normalization command" do
expect(movie.normalize_command).to eq("loudnorm=I=-24:TP=-1.5:"\
"LRA=11:measured_I=-62.95:measured_LRA=1.10:measured_TP=-37.62"\
":measured_thresh=-72.95:offset=0.77:linear=true:print_format=summary")
end

it "should return nil rotation when no rotation exists" do
expect(movie.rotation).to eq(nil)
end
Expand Down