Skip to content
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
15 changes: 13 additions & 2 deletions lib/ffmpeg/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
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 :audio_streams, :audio_stream, :audio_codec, :audio_bitrate, :audio_sample_rate, :audio_channels, :audio_tags
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, :max_volume, :mean_volume
attr_reader :container
attr_reader :metadata, :format_tags

Expand Down Expand Up @@ -37,9 +37,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 +93,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
8 changes: 8 additions & 0 deletions spec/ffmpeg/movie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ 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 return nil rotation when no rotation exists" do
expect(movie.rotation).to eq(nil)
end
Expand Down