Class: FFMPEG::Movie
- Inherits:
-
Object
- Object
- FFMPEG::Movie
- Defined in:
- lib/ffmpeg/movie.rb
Instance Attribute Summary collapse
-
#audio_codec ⇒ Object
readonly
Returns the value of attribute audio_codec.
-
#audio_sample_rate ⇒ Object
readonly
Returns the value of attribute audio_sample_rate.
-
#audio_stream ⇒ Object
readonly
Returns the value of attribute audio_stream.
-
#bitrate ⇒ Object
readonly
Returns the value of attribute bitrate.
-
#colorspace ⇒ Object
readonly
Returns the value of attribute colorspace.
-
#dar ⇒ Object
readonly
Returns the value of attribute dar.
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#resolution ⇒ Object
readonly
Returns the value of attribute resolution.
-
#video_codec ⇒ Object
readonly
Returns the value of attribute video_codec.
-
#video_stream ⇒ Object
readonly
Returns the value of attribute video_stream.
Instance Method Summary collapse
- #audio_channels ⇒ Object
- #calculated_aspect_ratio ⇒ Object
- #frame_rate ⇒ Object
- #height ⇒ Object
-
#initialize(path) ⇒ Movie
constructor
A new instance of Movie.
- #size ⇒ Object
- #transcode(output_file, options = EncodingOptions.new, transcoder_options = {}, &block) ⇒ Object
- #uncertain_duration? ⇒ Boolean
- #valid? ⇒ Boolean
- #width ⇒ Object
Constructor Details
#initialize(path) ⇒ Movie
Returns a new instance of Movie.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ffmpeg/movie.rb', line 7 def initialize(path) raise Errno::ENOENT, "the file '#{path}' does not exist" unless File.exists?(path) @path = escape(path) stdin, stdout, stderr = Open3.popen3("ffmpeg -i '#{path}'") # Output will land in stderr output = stderr.read output[/Duration: (\d{2}):(\d{2}):(\d{2}\.\d{1})/] @duration = ($1.to_i*60*60) + ($2.to_i*60) + $3.to_f output[/bitrate: (\d*)/] @bitrate = $1 ? $1.to_i : nil output[/Video: (.*)/] @video_stream = $1 output[/Audio: (.*)/] @audio_stream = $1 @uncertain_duration = output.include?("Estimating duration from bitrate, this may be inaccurate") if video_stream @video_codec, @colorspace, resolution = video_stream.split(/\s?,\s?/) @resolution = resolution.split(" ").first # get rid of [PAR 1:1 DAR 16:9] @dar = $1 if video_stream[/DAR (\d+:\d+)/] end if audio_stream @audio_codec, audio_sample_rate, @audio_channels = audio_stream.split(/\s?,\s?/) @audio_sample_rate = audio_sample_rate[/\d*/].to_i end @invalid = @video_stream.to_s.empty? && @audio_stream.to_s.empty? end |
Instance Attribute Details
#audio_codec ⇒ Object (readonly)
Returns the value of attribute audio_codec.
5 6 7 |
# File 'lib/ffmpeg/movie.rb', line 5 def audio_codec @audio_codec end |
#audio_sample_rate ⇒ Object (readonly)
Returns the value of attribute audio_sample_rate.
5 6 7 |
# File 'lib/ffmpeg/movie.rb', line 5 def audio_sample_rate @audio_sample_rate end |
#audio_stream ⇒ Object (readonly)
Returns the value of attribute audio_stream.
5 6 7 |
# File 'lib/ffmpeg/movie.rb', line 5 def audio_stream @audio_stream end |
#bitrate ⇒ Object (readonly)
Returns the value of attribute bitrate.
3 4 5 |
# File 'lib/ffmpeg/movie.rb', line 3 def bitrate @bitrate end |
#colorspace ⇒ Object (readonly)
Returns the value of attribute colorspace.
4 5 6 |
# File 'lib/ffmpeg/movie.rb', line 4 def colorspace @colorspace end |
#dar ⇒ Object (readonly)
Returns the value of attribute dar.
4 5 6 |
# File 'lib/ffmpeg/movie.rb', line 4 def dar @dar end |
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
3 4 5 |
# File 'lib/ffmpeg/movie.rb', line 3 def duration @duration end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/ffmpeg/movie.rb', line 3 def path @path end |
#resolution ⇒ Object (readonly)
Returns the value of attribute resolution.
4 5 6 |
# File 'lib/ffmpeg/movie.rb', line 4 def resolution @resolution end |
#video_codec ⇒ Object (readonly)
Returns the value of attribute video_codec.
4 5 6 |
# File 'lib/ffmpeg/movie.rb', line 4 def video_codec @video_codec end |
#video_stream ⇒ Object (readonly)
Returns the value of attribute video_stream.
4 5 6 |
# File 'lib/ffmpeg/movie.rb', line 4 def video_stream @video_stream end |
Instance Method Details
#audio_channels ⇒ Object
73 74 75 76 77 |
# File 'lib/ffmpeg/movie.rb', line 73 def audio_channels return @audio_channels[/\d*/].to_i if @audio_channels["channels"] return 1 if @audio_channels["mono"] return 2 if @audio_channels["stereo"] end |
#calculated_aspect_ratio ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/ffmpeg/movie.rb', line 59 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 end |
#frame_rate ⇒ Object
79 80 81 |
# File 'lib/ffmpeg/movie.rb', line 79 def frame_rate video_stream[/(\d*\.?\d*)\s?fps/] ? $1.to_f : nil end |
#height ⇒ Object
55 56 57 |
# File 'lib/ffmpeg/movie.rb', line 55 def height resolution.split("x")[1].to_i rescue nil end |
#size ⇒ Object
69 70 71 |
# File 'lib/ffmpeg/movie.rb', line 69 def size File.size(@path) end |
#transcode(output_file, options = EncodingOptions.new, transcoder_options = {}, &block) ⇒ Object
83 84 85 |
# File 'lib/ffmpeg/movie.rb', line 83 def transcode(output_file, = EncodingOptions.new, = {}, &block) Transcoder.new(self, output_file, , ).run &block end |
#uncertain_duration? ⇒ Boolean
47 48 49 |
# File 'lib/ffmpeg/movie.rb', line 47 def uncertain_duration? @uncertain_duration end |
#valid? ⇒ Boolean
43 44 45 |
# File 'lib/ffmpeg/movie.rb', line 43 def valid? not @invalid end |
#width ⇒ Object
51 52 53 |
# File 'lib/ffmpeg/movie.rb', line 51 def width resolution.split("x")[0].to_i rescue nil end |