Browse files

Use width and height to calculate aspect ratio if DAR is an impossibl…

…e value.
  • Loading branch information...
1 parent 23e18cd commit 1a39ea8ab0541248895db36007ccfe3800f095dd @dbackeus dbackeus committed Mar 5, 2012
Showing with 53 additions and 7 deletions.
  1. +13 −7 lib/ffmpeg/movie.rb
  2. +16 −0 spec/ffmpeg/movie_spec.rb
  3. +24 −0 spec/fixtures/outputs/file_with_weird_dar.txt
View
20 lib/ffmpeg/movie.rb
@@ -65,13 +65,7 @@ def height
end
def calculated_aspect_ratio
- if dar
- w, h = dar.split(":")
- w.to_f / h.to_f
- else
- aspect = width.to_f / height.to_f
- aspect.nan? ? nil : aspect
- end
+ aspect_from_dar || aspect_from_dimensions
end
def size
@@ -95,6 +89,18 @@ def transcode(output_file, options = EncodingOptions.new, transcoder_options = {
end
protected
+ def aspect_from_dar
+ return nil unless dar
+ w, h = dar.split(":")
+ aspect = w.to_f / h.to_f
+ aspect.zero? ? nil : aspect
+ end
+
+ def aspect_from_dimensions
+ aspect = width.to_f / height.to_f
+ aspect.nan? ? nil : aspect
+ end
+
def escape(path)
map = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
path.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { map[$1] }
View
16 spec/ffmpeg/movie_spec.rb
@@ -57,6 +57,22 @@ module FFMPEG
@movie.calculated_aspect_ratio.to_s[0..15].should == "1.73827160493827" # substringed to be 1.9 compatible
end
end
+
+ describe "given an impossible DAR" do
+ before(:all) do
+ fake_output = StringIO.new(File.read("#{fixture_path}/outputs/file_with_weird_dar.txt"))
+ Open3.stub!(:popen3).and_return([nil,nil,fake_output])
+ @movie = Movie.new(__FILE__)
+ end
+
+ it "should parse the DAR" do
+ @movie.dar.should == "0:1"
+ end
+
+ it "should calulate using width and height instead" do
+ @movie.calculated_aspect_ratio.to_s[0..15].should == "1.77777777777777" # substringed to be 1.9 compatible
+ end
+ end
describe "given a file with start-time" do
before(:each) do
View
24 spec/fixtures/outputs/file_with_weird_dar.txt
@@ -0,0 +1,24 @@
+ffmpeg version 0.8.5, Copyright (c) 2000-2011 the FFmpeg developers
+ built on Oct 17 2011 12:25:58 with gcc 4.2.1 (Apple Inc. build 5666) (dot 3)
+ configuration: --prefix=/usr/local/Cellar/ffmpeg/0.8.5 --enable-shared --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --cc=/usr/bin/gcc-4.2 --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libxvid
+ libavutil 51. 9. 1 / 51. 9. 1
+ libavcodec 53. 7. 0 / 53. 7. 0
+ libavformat 53. 4. 0 / 53. 4. 0
+ libavdevice 53. 1. 1 / 53. 1. 1
+ libavfilter 2. 23. 0 / 2. 23. 0
+ libswscale 2. 0. 0 / 2. 0. 0
+ libpostproc 51. 2. 0 / 51. 2. 0
+Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/druiden/Movies/clips/metal_gear-aspect-ratio-fail.mov':
+ Metadata:
+ major_brand : qt
+ minor_version : 537199360
+ compatible_brands: qt
+ creation_time : 2011-05-31 01:47:42
+ Duration: 00:02:40.63, start: 0.000000, bitrate: 17495 kb/s
+ Stream #0.0(eng): Video: h264 (Main), yuv420p, 1280x720, 15955 kb/s, PAR 16777216:16777216 DAR 0:1, 30 fps, 30 tbr, 30 tbn, 60 tbc
+ Metadata:
+ creation_time : 2011-05-31 01:47:42
+ Stream #0.1(eng): Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s
+ Metadata:
+ creation_time : 2011-05-31 01:47:42
+At least one output file must be specified

0 comments on commit 1a39ea8

Please sign in to comment.