From 235ff86eecd71f97ff6c34220acb335f51d3fcb2 Mon Sep 17 00:00:00 2001 From: Marina Chirchikova Date: Thu, 21 Sep 2017 15:24:44 -0400 Subject: [PATCH 1/3] Extract level and profile for video stream --- lib/ffmpeg/movie.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ffmpeg/movie.rb b/lib/ffmpeg/movie.rb index 0e148bb3..d86cee05 100644 --- a/lib/ffmpeg/movie.rb +++ b/lib/ffmpeg/movie.rb @@ -6,7 +6,7 @@ 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 :container attr_reader :metadata, :format_tags @@ -84,6 +84,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]) From 996a7a97c7021ad417195b60c8ba396a352f56f9 Mon Sep 17 00:00:00 2001 From: Marina Chirchikova Date: Thu, 21 Sep 2017 17:21:18 -0400 Subject: [PATCH 2/3] Add audio mean and max volume levels --- lib/ffmpeg/movie.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/ffmpeg/movie.rb b/lib/ffmpeg/movie.rb index d86cee05..01684b84 100644 --- a/lib/ffmpeg/movie.rb +++ b/lib/ffmpeg/movie.rb @@ -7,7 +7,7 @@ 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, :level, :profile, :frame_rate - attr_reader :audio_streams, :audio_stream, :audio_codec, :audio_bitrate, :audio_sample_rate, :audio_channels, :audio_tags + 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 @@ -37,9 +37,20 @@ def initialize(path) std_error = stderr.read unless stderr.nil? end + get_levels_command = "ffmpeg -i #{path} -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) + output[/mean_volume:\ (.*)/] + @mean_volume = $1 + + output[/max_volume:\ (.*)/] + @max_volume = $1 + begin @metadata = MultiJson.load(std_output, symbolize_keys: true) rescue MultiJson::ParseError From a450c8bcd4a1fd2fce749f0be9cc7efadd5d7d60 Mon Sep 17 00:00:00 2001 From: Marina Chirchikova Date: Mon, 25 Sep 2017 11:00:49 -0400 Subject: [PATCH 3/3] Refactor and add specs --- lib/ffmpeg/movie.rb | 8 +++----- spec/ffmpeg/movie_spec.rb | 8 ++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/ffmpeg/movie.rb b/lib/ffmpeg/movie.rb index 01684b84..07fa6257 100644 --- a/lib/ffmpeg/movie.rb +++ b/lib/ffmpeg/movie.rb @@ -37,7 +37,7 @@ def initialize(path) std_error = stderr.read unless stderr.nil? end - get_levels_command = "ffmpeg -i #{path} -af \"volumedetect\" -f null /dev/null" + 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 @@ -45,11 +45,9 @@ def initialize(path) fix_encoding(std_output) fix_encoding(std_error) - output[/mean_volume:\ (.*)/] - @mean_volume = $1 + @mean_volume = output[/mean_volume:\ (.*)/, 1] - output[/max_volume:\ (.*)/] - @max_volume = $1 + @max_volume = output[/max_volume:\ (.*)/, 1] begin @metadata = MultiJson.load(std_output, symbolize_keys: true) diff --git a/spec/ffmpeg/movie_spec.rb b/spec/ffmpeg/movie_spec.rb index 2e994fc2..c0ec052d 100644 --- a/spec/ffmpeg/movie_spec.rb +++ b/spec/ffmpeg/movie_spec.rb @@ -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