Skip to content

Commit

Permalink
Merge pull request #58 from smoothdvd/master
Browse files Browse the repository at this point in the history
Add attribute "SAR" to Moive, Fix option name of video profile in README, Add watermark to encoding options
  • Loading branch information
dbackeus committed Oct 24, 2013
2 parents 9231480 + fda0571 commit b4a45cf
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Use the EncodingOptions parser for humanly readable transcoding options. Below y
``` ruby
options = {video_codec: "libx264", frame_rate: 10, resolution: "320x240", video_bitrate: 300, video_bitrate_tolerance: 100,
aspect: 1.333333, keyframe_interval: 90,
x264_profile: "high", x264_preset: "slow",
x264_vprofile: "high", x264_preset: "slow",
audio_codec: "libfaac", audio_bitrate: 32, audio_sample_rate: 22050, audio_channels: 1,
threads: 2,
custom: "-vf crop=60:60:10:10"}
Expand Down Expand Up @@ -128,6 +128,15 @@ options = {video_min_bitrate: 600, video_max_bitrate: 600, buffer_size: 2000}
movie.transcode("movie.flv", options)
```

Add watermark image on the video.

For example, you want to add a watermark on the video at right top corner with 10px padding.

``` ruby
options = {watermark: "full_path_of_watermark.png", resolution: "640x360", watermark_filter: {position: "RT", padding_x: 10, padding_y: 10}}
```
position can be "LT" (Left Top Corner), "RT" (Right Top Corner), "LB" (Left Bottom Corner), "RB" (Right Bottom Corner).

### Taking Screenshots

You can use the screenshot method to make taking screenshots a bit simpler.
Expand Down
17 changes: 17 additions & 0 deletions lib/ffmpeg/encoding_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ def convert_x264_preset(value)
"-preset #{value}"
end

def convert_watermark(value)
"-i #{value}"
end

def convert_watermark_filter(value)
case value[:position].to_s
when "LT"
"-filter_complex 'scale=#{self[:resolution]},overlay=x=#{value[:padding_x]}:y=#{value[:padding_y]}'"
when "RT"
"-filter_complex 'scale=#{self[:resolution]},overlay=x=main_w-overlay_w-#{value[:padding_x]}:y=#{value[:padding_y]}'"
when "LB"
"-filter_complex 'scale=#{self[:resolution]},overlay=x=#{value[:padding_x]}:y=main_h-overlay_h-#{value[:padding_y]}'"
when "RB"
"-filter_complex 'scale=#{self[:resolution]},overlay=x=main_w-overlay_w-#{value[:padding_x]}:y=main_h-overlay_h-#{value[:padding_y]}'"
end
end

def convert_custom(value)
value
end
Expand Down
14 changes: 13 additions & 1 deletion lib/ffmpeg/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module FFMPEG
class Movie
attr_reader :path, :duration, :time, :bitrate, :rotation, :creation_time
attr_reader :video_stream, :video_codec, :video_bitrate, :colorspace, :resolution, :dar
attr_reader :video_stream, :video_codec, :video_bitrate, :colorspace, :resolution, :sar, :dar
attr_reader :audio_stream, :audio_codec, :audio_bitrate, :audio_sample_rate
attr_reader :container

Expand Down Expand Up @@ -46,6 +46,7 @@ def initialize(path)
@video_codec, @colorspace, resolution, video_bitrate = video_stream.split(/\s?,(?![^,\)]+\))\s?/)
@video_bitrate = video_bitrate =~ %r(\A(\d+) kb/s\Z) ? $1.to_i : nil
@resolution = resolution.split(" ").first rescue nil # get rid of [PAR 1:1 DAR 16:9]
@sar = $1 if video_stream[/SAR (\d+:\d+)/]
@dar = $1 if video_stream[/DAR (\d+:\d+)/]
end

Expand Down Expand Up @@ -76,6 +77,10 @@ def calculated_aspect_ratio
aspect_from_dar || aspect_from_dimensions
end

def calculated_pixel_aspect_ratio
aspect_from_sar || 1
end

def size
File.size(@path)
end
Expand Down Expand Up @@ -109,6 +114,13 @@ def aspect_from_dar
aspect.zero? ? nil : aspect
end

def aspect_from_sar
return nil unless sar
w, h = sar.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
Expand Down
36 changes: 36 additions & 0 deletions spec/ffmpeg/encoding_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,42 @@ module FFMPEG
it "should convert x264 preset" do
EncodingOptions.new(x264_preset: "slow").to_s.should == "-preset slow"
end

it "should specify input watermark file" do
EncodingOptions.new(watermark: "watermark.png").to_s.should == "-i watermark.png"
end

it "should specify watermark position at left top corner" do
opts = Hash.new
opts[:resolution] = "640x480"
opts[:watermark_filter] = { position: "LT", padding_x: 10, padding_y: 10 }
converted = EncodingOptions.new(opts).to_s
converted.should include "-filter_complex 'scale=640x480,overlay=x=10:y=10'"
end

it "should specify watermark position at right top corner" do
opts = Hash.new
opts[:resolution] = "640x480"
opts[:watermark_filter] = { position: "RT", padding_x: 10, padding_y: 10 }
converted = EncodingOptions.new(opts).to_s
converted.should include "-filter_complex 'scale=640x480,overlay=x=main_w-overlay_w-10:y=10'"
end

it "should specify watermark position at left bottom corner" do
opts = Hash.new
opts[:resolution] = "640x480"
opts[:watermark_filter] = { position: "LB", padding_x: 10, padding_y: 10 }
converted = EncodingOptions.new(opts).to_s
converted.should include "-filter_complex 'scale=640x480,overlay=x=10:y=main_h-overlay_h-10'"
end

it "should specify watermark position at left bottom corner" do
opts = Hash.new
opts[:resolution] = "640x480"
opts[:watermark_filter] = { position: "RB", padding_x: 10, padding_y: 10 }
converted = EncodingOptions.new(opts).to_s
converted.should include "overlay=x=main_w-overlay_w-10:y=main_h-overlay_h-10'"
end
end
end
end
30 changes: 30 additions & 0 deletions spec/ffmpeg/movie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,36 @@ module FFMPEG
end
end

context "given a weird storage/pixel aspect ratio file" do
before(:all) do
@movie = Movie.new("#{fixture_path}/movies/weird_aspect.small.mpg")
end

it "should parse the SAR" do
@movie.sar.should == "64:45"
end

it "should have correct calculated_pixel_aspect_ratio" do
@movie.calculated_pixel_aspect_ratio.to_s[0..14].should == "1.4222222222222" # substringed to be 1.9 compatible
end
end

context "given an impossible SAR" do
before(:all) do
fake_output = StringIO.new(File.read("#{fixture_path}/outputs/file_with_weird_sar.txt"))
Open3.stub(:popen3).and_yield(nil,nil,fake_output)
@movie = Movie.new(__FILE__)
end

it "should parse the SAR" do
@movie.sar.should == "0:1"
end

it "should using square SAR, 1.0 instead" do
@movie.calculated_pixel_aspect_ratio.to_s[0..14].should == "1" # substringed to be 1.9 compatible
end
end

context "given a file with ISO-8859-1 characters in output" do
it "should not crash" do
fake_output = StringIO.new(File.read("#{fixture_path}/outputs/file_with_iso-8859-1.txt"))
Expand Down
16 changes: 16 additions & 0 deletions spec/fixtures/outputs/file_with_weird_sar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ffmpeg version N-57291-g80b8f78 Copyright (c) 2000-2013 the FFmpeg developers
built on Oct 21 2013 23:07:04 with Apple LLVM version 5.0 (clang-500.2.75) (based on LLVM 3.3svn)
configuration: --cc=clang --enable-gpl --enable-shared --enable-libmp3lame --enable-libxvid --enable-libx264 --enable-pthreads --enable-libfaac --enable-postproc --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvorbis --enable-libgsm --enable-nonfree --enable-avfilter --enable-libtheora --enable-version3 --enable-librtmp --enable-openssl --enable-libvpx
libavutil 52. 47.101 / 52. 47.101
libavcodec 55. 37.102 / 55. 37.102
libavformat 55. 19.103 / 55. 19.103
libavdevice 55. 4.100 / 55. 4.100
libavfilter 3. 88.102 / 3. 88.102
libswscale 2. 5.101 / 2. 5.101
libswresample 0. 17.104 / 0. 17.104
libpostproc 52. 3.100 / 52. 3.100
Input #0, mpeg, from 'spec/fixtures/movies/weird_aspect.small.mpg':
Duration: 00:00:01.44, start: 0.220000, bitrate: 1706 kb/s
Stream #0:0[0x1e0]: Video: mpeg1video, yuv420p(tv), 352x288 [SAR 0:1 DAR 704:405], 1500 kb/s, 25 fps, 25 tbr, 90k tbn, 25 tbc
Stream #0:1[0x1c0]: Audio: mp2, 44100 Hz, stereo, s16p, 224 kb/s
At least one output file must be specified

0 comments on commit b4a45cf

Please sign in to comment.