Class: FFMPEG::Movie
- Inherits:
-
Object
- Object
- FFMPEG::Movie
- Defined in:
- lib/ffmpeg/movie.rb
Instance Attribute Summary collapse
-
#audio_bitrate ⇒ Object
readonly
Returns the value of attribute audio_bitrate.
-
#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.
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#creation_time ⇒ Object
readonly
Returns the value of attribute creation_time.
-
#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.
-
#rotation ⇒ Object
readonly
Returns the value of attribute rotation.
-
#sar ⇒ Object
readonly
Returns the value of attribute sar.
-
#sizep ⇒ Object
readonly
Returns the value of attribute sizep.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
-
#video_bitrate ⇒ Object
readonly
Returns the value of attribute video_bitrate.
-
#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
- #calculated_pixel_aspect_ratio ⇒ Object
- #frame_rate ⇒ Object
- #height ⇒ Object
-
#initialize(path) ⇒ Movie
constructor
A new instance of Movie.
- #screenshot(output_file, options = EncodingOptions.new, transcoder_options = {}, &block) ⇒ Object
- #size ⇒ Object
- #transcode(output_file, options = EncodingOptions.new, transcoder_options = {}, &block) ⇒ Object
- #valid? ⇒ Boolean
- #width ⇒ Object
Constructor Details
#initialize(path) ⇒ Movie
Returns a new instance of Movie.
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ffmpeg/movie.rb', line 10 def initialize(path) raise Errno::ENOENT, "the file '#{path}' does not exist" unless File.exists?(path) @path = path # ffmpeg will output to stderr command = "#{FFMPEG.ffmpeg_binary} -i #{Shellwords.escape(path)}" output = Open3.popen3(command) { |stdin, stdout, stderr| stderr.read } fix_encoding(output) output[/Input \#\d+\,\s*(\S+),\s*from/] @container = $1 output[/Duration: (\d{2}):(\d{2}):(\d{2}\.\d{2})/] @duration = ($1.to_i*60*60) + ($2.to_i*60) + $3.to_f output[/start: (\d*\.\d*)/] @time = $1 ? $1.to_f : 0.0 output[/creation_time {1,}: {1,}(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/] @creation_time = $1 ? Time.parse("#{$1}") : nil output[/bitrate: (\d*)/] @bitrate = $1 ? $1.to_i : nil output[/rotate\ {1,}:\ {1,}(\d*)/] @rotation = $1 ? $1.to_i : nil output[/Video:\ (.*) (|(default))/] @video_stream = $1 output[/Audio:\ (.*) (|(default))/] @audio_stream = $1 if @video_stream commas_except_in_parenthesis = /(?:\([^()]*\)|[^,])+/ # regexp to handle "yuv420p(tv, bt709)" colorspace etc from http://goo.gl/6oi645 @video_codec, @colorspace, resolution, video_bitrate = @video_stream.scan(commas_except_in_parenthesis).map(&:strip) @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] @sizep = @resolution.split("x").last.to_i rescue nil @sar = $1 if @video_stream[/SAR (\d+:\d+)/] @dar = $1 if @video_stream[/DAR (\d+:\d+)/] end if @audio_stream @audio_codec, audio_sample_rate, @audio_channels, unused, audio_bitrate = @audio_stream.split(/\s?,\s?/) @audio_bitrate = audio_bitrate =~ %r(\A(\d+)) ? $1.to_i : nil @audio_sample_rate = audio_sample_rate[/\d*/].to_i end @invalid = true if @video_stream.to_s.empty? && @audio_stream.to_s.empty? @invalid = true if output.include?("is not supported") @invalid = true if output.include?("could not find codec parameters") end |
Instance Attribute Details
#audio_bitrate ⇒ Object (readonly)
Returns the value of attribute audio_bitrate.
7 8 9 |
# File 'lib/ffmpeg/movie.rb', line 7 def audio_bitrate @audio_bitrate end |
#audio_codec ⇒ Object (readonly)
Returns the value of attribute audio_codec.
7 8 9 |
# File 'lib/ffmpeg/movie.rb', line 7 def audio_codec @audio_codec end |
#audio_sample_rate ⇒ Object (readonly)
Returns the value of attribute audio_sample_rate.
7 8 9 |
# File 'lib/ffmpeg/movie.rb', line 7 def audio_sample_rate @audio_sample_rate end |
#audio_stream ⇒ Object (readonly)
Returns the value of attribute audio_stream.
7 8 9 |
# File 'lib/ffmpeg/movie.rb', line 7 def audio_stream @audio_stream end |
#bitrate ⇒ Object (readonly)
Returns the value of attribute bitrate.
5 6 7 |
# File 'lib/ffmpeg/movie.rb', line 5 def bitrate @bitrate end |
#colorspace ⇒ Object (readonly)
Returns the value of attribute colorspace.
6 7 8 |
# File 'lib/ffmpeg/movie.rb', line 6 def colorspace @colorspace end |
#container ⇒ Object (readonly)
Returns the value of attribute container.
8 9 10 |
# File 'lib/ffmpeg/movie.rb', line 8 def container @container end |
#creation_time ⇒ Object (readonly)
Returns the value of attribute creation_time.
5 6 7 |
# File 'lib/ffmpeg/movie.rb', line 5 def creation_time @creation_time end |
#dar ⇒ Object (readonly)
Returns the value of attribute dar.
6 7 8 |
# File 'lib/ffmpeg/movie.rb', line 6 def dar @dar end |
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
5 6 7 |
# File 'lib/ffmpeg/movie.rb', line 5 def duration @duration end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/ffmpeg/movie.rb', line 5 def path @path end |
#resolution ⇒ Object (readonly)
Returns the value of attribute resolution.
6 7 8 |
# File 'lib/ffmpeg/movie.rb', line 6 def resolution @resolution end |
#rotation ⇒ Object (readonly)
Returns the value of attribute rotation.
5 6 7 |
# File 'lib/ffmpeg/movie.rb', line 5 def rotation @rotation end |
#sar ⇒ Object (readonly)
Returns the value of attribute sar.
6 7 8 |
# File 'lib/ffmpeg/movie.rb', line 6 def sar @sar end |
#sizep ⇒ Object (readonly)
Returns the value of attribute sizep.
8 9 10 |
# File 'lib/ffmpeg/movie.rb', line 8 def sizep @sizep end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
5 6 7 |
# File 'lib/ffmpeg/movie.rb', line 5 def time @time end |
#video_bitrate ⇒ Object (readonly)
Returns the value of attribute video_bitrate.
6 7 8 |
# File 'lib/ffmpeg/movie.rb', line 6 def video_bitrate @video_bitrate end |
#video_codec ⇒ Object (readonly)
Returns the value of attribute video_codec.
6 7 8 |
# File 'lib/ffmpeg/movie.rb', line 6 def video_codec @video_codec end |
#video_stream ⇒ Object (readonly)
Returns the value of attribute video_stream.
6 7 8 |
# File 'lib/ffmpeg/movie.rb', line 6 def video_stream @video_stream end |
Instance Method Details
#audio_channels ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/ffmpeg/movie.rb', line 90 def audio_channels return nil unless @audio_channels return @audio_channels[/\d*/].to_i if @audio_channels["channels"] return 1 if @audio_channels["mono"] return 2 if @audio_channels["stereo"] return 6 if @audio_channels["5.1"] end |
#calculated_aspect_ratio ⇒ Object
78 79 80 |
# File 'lib/ffmpeg/movie.rb', line 78 def calculated_aspect_ratio aspect_from_dar || aspect_from_dimensions end |
#calculated_pixel_aspect_ratio ⇒ Object
82 83 84 |
# File 'lib/ffmpeg/movie.rb', line 82 def calculated_pixel_aspect_ratio aspect_from_sar || 1 end |
#frame_rate ⇒ Object
98 99 100 101 |
# File 'lib/ffmpeg/movie.rb', line 98 def frame_rate return nil unless video_stream video_stream[/(\d*\.?\d*)\s?fps/] ? $1.to_f : nil end |
#height ⇒ Object
74 75 76 |
# File 'lib/ffmpeg/movie.rb', line 74 def height resolution.split("x")[1].to_i rescue nil end |
#screenshot(output_file, options = EncodingOptions.new, transcoder_options = {}, &block) ⇒ Object
107 108 109 |
# File 'lib/ffmpeg/movie.rb', line 107 def screenshot(output_file, = EncodingOptions.new, = {}, &block) Transcoder.new(self, output_file, .merge(screenshot: true), ).run &block end |
#size ⇒ Object
86 87 88 |
# File 'lib/ffmpeg/movie.rb', line 86 def size File.size(@path) end |
#transcode(output_file, options = EncodingOptions.new, transcoder_options = {}, &block) ⇒ Object
103 104 105 |
# File 'lib/ffmpeg/movie.rb', line 103 def transcode(output_file, = EncodingOptions.new, = {}, &block) Transcoder.new(self, output_file, , ).run &block end |
#valid? ⇒ Boolean
66 67 68 |
# File 'lib/ffmpeg/movie.rb', line 66 def valid? not @invalid end |
#width ⇒ Object
70 71 72 |
# File 'lib/ffmpeg/movie.rb', line 70 def width resolution.split("x")[0].to_i rescue nil end |