ffmpeg_web 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6f07383478d87fdb82d3f7edb2dfecc86b1faa65
4
+ data.tar.gz: eb67c2bd4e2b2c8b163e2258e352e5ae93d024fe
5
+ SHA512:
6
+ metadata.gz: b26f548704f8b8db547f669bd7c6ba709b460d6cd47254c72719dddaa03d025968f6531036e53ea60cfe5b7f65765f483f9f42bbd0df5257fc627be2335b5a72
7
+ data.tar.gz: a0e37deaf0ca9ed6015cbdd2b4190732b9d0f99d2381f15b164ae9430f03b6c55ed7f91b528301015b71f091240e45a4e52b31cf69ab942439217e44c336ab37
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ffmpeg_web.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jason
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # FfmpegWeb
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ffmpeg_web'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ffmpeg_web
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/ffmpeg_web/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
8
+ task :test => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ffmpeg_web/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ffmpeg_web"
8
+ spec.version = FFmpegWeb::VERSION
9
+ spec.authors = ["Jason"]
10
+ spec.email = ["jsofokleous@googlemail.com"]
11
+ spec.summary = %q{A Ruby wrapper for FFMpeg that allows you to transcode video into a web optimised format. }
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/jasonsof/ffmpeg_web"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
data/lib/ffmpeg_web.rb ADDED
@@ -0,0 +1,143 @@
1
+ require "ffmpeg_web/version"
2
+
3
+ module FFmpegWeb
4
+ class Transcoder
5
+ attr_accessor :input, :output, :video_info
6
+
7
+ def initialize(input, output)
8
+ @input = input
9
+ @output = output
10
+
11
+ raise "Input file does not exist" if !File.exists?(input)
12
+ raise "Output directory does not exist" if !File.exists?(File.dirname(output))
13
+
14
+ @video_info = ffmpeg_info
15
+
16
+ raise "File is not a valid movie file" if !is_valid?
17
+ end
18
+
19
+ # returns the duration of the video in seconds
20
+ def duration
21
+ duration = /Duration: (\d{2}):(\d{2}):(\d{2}).(\d{2})/.match(@video_info)
22
+ duration_in_milliseconds = ($~[1].to_i * 60 * 60) + ($~[2].to_i * 60) + $~[3].to_i + ($~[4].to_i / 1000)
23
+ end
24
+
25
+ # transcodes the file and returns an io pipe object
26
+ def transcode(output_size=nil)
27
+ progress_read, progress_write = IO::pipe
28
+ pid = Process.fork do
29
+ progress_read.close
30
+
31
+ if output_size.nil?
32
+ crf_encode(progress_write)
33
+ else
34
+ two_pass_encode(output_size, progress_write)
35
+ end
36
+ end
37
+
38
+ progress_write.close
39
+
40
+ progress_read
41
+ end
42
+
43
+ private
44
+ def ffmpeg_info
45
+ read, write = IO::pipe
46
+ pid = Process.fork do
47
+ read.close
48
+
49
+ $stderr.reopen(write)
50
+ exec("ffmpeg -y -i '#{@input}'")
51
+ end
52
+ write.close
53
+
54
+ ffmpeg_output = read.read
55
+ ffmpeg_output
56
+ end
57
+
58
+ def is_valid?
59
+ video_stream, audio_stream = nil
60
+
61
+ if match = @video_info.match(/Video: (.*)$/i)
62
+ video_stream = match.captures.first
63
+ end
64
+
65
+ if match = @video_info.match(/Audio: (.*)$/i)
66
+ audio_stream = match.captures.first
67
+ end
68
+
69
+ video_stream && audio_stream
70
+ end
71
+
72
+ def two_pass_encode(output_size, progress_write)
73
+ output_size_in_kilobits = output_size * 8192
74
+ audio_bitrate = 128
75
+ total_bitrate = output_size_in_kilobits / self.duration
76
+ video_bitrate = total_bitrate - audio_bitrate
77
+
78
+ first_pass = "ffmpeg -y -i '#{@input}' -movflags faststart -c:v libx264 -preset slow -b:v #{video_bitrate}k -an -pass 1 -f mp4 /dev/null"
79
+ second_pass = "ffmpeg -y -i '#{@input}' -movflags faststart -c:v libx264 -preset slow -b:v #{video_bitrate}k -c:a libfaac -b:a #{audio_bitrate}k -pass 2 '#{@output}.mp4'"
80
+
81
+ pid, read = do_pass(first_pass)
82
+ log_progress(read, progress_write, 1, 2)
83
+ pid, status = Process.waitpid2(pid)
84
+
85
+ pid, read = do_pass(second_pass)
86
+ log_progress(read, progress_write, 2, 2)
87
+ pid, status = Process.waitpid2(pid)
88
+ status.exitstatus
89
+ end
90
+
91
+ def crf_encode(progress_write)
92
+ pid, read = do_pass("ffmpeg -y -i '#{@input}' -movflags faststart -c:v libx264 -preset slow -crf 20 -c:a libfaac '#{output}.mp4'")
93
+ log_progress(read, progress_write, 1, 1)
94
+
95
+ pid, status = Process.waitpid2(pid)
96
+ status.exitstatus
97
+ end
98
+
99
+ def do_pass(cmd)
100
+ read, write = IO::pipe
101
+ pid = Process.fork do
102
+ read.close
103
+
104
+ $stderr.reopen(write)
105
+ $stdout.reopen(write)
106
+ exec(cmd)
107
+ end
108
+ write.close
109
+
110
+ [pid, read]
111
+ end
112
+
113
+ def log_progress(input_stream, output_stream, current_pass, number_of_passes)
114
+ duration = 0.00 #milliseconds
115
+ elapsed = 0.00 #milliseconds
116
+
117
+ while line = input_stream.gets
118
+ if line =~ /Duration: (\d{2}):(\d{2}):(\d{2}).(\d{2})/ && duration == 0.00
119
+ duration = ($~[1].to_i * 60 * 60 * 1000) + ($~[2].to_i * 60 * 1000) + ($~[3].to_i * 1000) + $~[4].to_i
120
+ break
121
+ end
122
+ end
123
+
124
+ while line = input_stream.gets("\r")
125
+ line = line.encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '') if line
126
+ if line =~ /time=(\d{2}):(\d{2}):(\d{2}).(\d{2})/
127
+ elapsed = ($~[1].to_i * 60 * 60 * 1000) + ($~[2].to_i * 60 * 1000) + ($~[3].to_i * 1000) + $~[4].to_i
128
+
129
+ pass_progress = (elapsed.to_f / duration.to_f) * 100
130
+ if number_of_passes > 1 && current_pass > 1
131
+ progress = (pass_progress / number_of_passes) + (100 / number_of_passes)
132
+ elsif number_of_passes > 1 && current_pass == 1
133
+ progress = pass_progress / number_of_passes
134
+ else
135
+ progress = pass_progress
136
+ end
137
+
138
+ output_stream.puts progress.round(2).to_s + "%"
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,3 @@
1
+ module FFmpegWeb
2
+ VERSION = "0.0.2"
3
+ end
Binary file
Binary file
@@ -0,0 +1,12 @@
1
+ require 'ffmpeg_web'
2
+
3
+ RSpec.configure do |config|
4
+ end
5
+
6
+ def fixture_path
7
+ @fixture_path ||= File.join(File.dirname(__FILE__), 'fixtures')
8
+ end
9
+
10
+ def base_path
11
+ @base_path ||= Dir.pwd#File.expand_path("..",Dir.pwd)
12
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ describe FFmpegWeb::Transcoder do
4
+ let(:good_input) { "#{fixture_path}/10 seconds.mov" }
5
+ let(:bad_input) { "#{fixture_path}/broken.mp4" }
6
+ let(:output) { "#{fixture_path}/output" }
7
+
8
+ describe "#initialize" do
9
+ it "sets the @input variable" do
10
+ transcoder = FFmpegWeb::Transcoder.new(good_input, output)
11
+ expect(transcoder.instance_variable_get(:@input)).to eq good_input
12
+ end
13
+ it "sets the @output variable" do
14
+ transcoder = FFmpegWeb::Transcoder.new(good_input, output)
15
+ expect(transcoder.instance_variable_get(:@output)).to eq output
16
+ end
17
+
18
+ context "when the input is an invalid video file" do
19
+ it "raises an exception" do
20
+ expect {
21
+ transcoder = FFmpegWeb::Transcoder.new(bad_input, output)
22
+ }.to raise_exception
23
+ end
24
+ end
25
+
26
+ context "when the input does not exist" do
27
+ it "raises an exception" do
28
+ input = "/path/to/nonexistent/input.mov"
29
+
30
+ expect {
31
+ transcoder = FFmpegWeb::Transcoder.new(input, output)
32
+ }.to raise_exception
33
+ end
34
+ end
35
+
36
+ context "when the output directory does not exist" do
37
+ it "raises an exception" do
38
+ output = "/path/nonexistent/output"
39
+
40
+ expect {
41
+ transcoder = FFmpegWeb::Transcoder.new(good_input, output)
42
+ }.to raise_exception
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#duration" do
48
+ context "when the input is a valid video file" do
49
+ it "returns the videos duration in seconds" do
50
+ transcoder = FFmpegWeb::Transcoder.new(good_input, output)
51
+ expect(transcoder.duration).to eq(10)
52
+ end
53
+
54
+ it "returns a Fixnum" do
55
+ transcoder = FFmpegWeb::Transcoder.new(good_input, output)
56
+ expect(transcoder.duration).to be_a(Fixnum)
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "#transcode" do
62
+ let(:megabyte) { 1024.0 * 1024.0 }
63
+
64
+ after(:each) do
65
+ begin
66
+ FileUtils.rm("#{fixture_path}/output.mp4")
67
+ FileUtils.rm("#{base_path}/ffmpeg2pass-0.log")
68
+ FileUtils.rm("#{base_path}/ffmpeg2pass-0.log.mbtree")
69
+ rescue Errno::ENOENT
70
+ end
71
+ end
72
+
73
+ context "given an output filesize" do
74
+ it "creates an output file around the size given" do
75
+ desired_output_size_in_mb = 10
76
+
77
+ transcoder = FFmpegWeb::Transcoder.new(good_input, output)
78
+ progress_stream = transcoder.transcode(desired_output_size_in_mb)
79
+ while progress = progress_stream.gets
80
+ end
81
+
82
+ expect(File.exists?("#{output}.mp4")).to be_true
83
+ output_size = File.size("#{output}.mp4").to_f / megabyte
84
+ expect(output_size).to be_within(1).of(desired_output_size_in_mb)
85
+ end
86
+ end
87
+
88
+ context "without given an output filesize" do
89
+ it "creates an output file" do
90
+ transcoder = FFmpegWeb::Transcoder.new(good_input, output)
91
+ progress_stream = transcoder.transcode(10)
92
+ while progress = progress_stream.gets
93
+ end
94
+
95
+ expect(File.exists?("#{output}.mp4")).to be_true
96
+ end
97
+ end
98
+
99
+ it "returns an IO stream" do
100
+ transcoder = FFmpegWeb::Transcoder.new(good_input, output)
101
+ progress_stream = transcoder.transcode(10)
102
+ expect(progress_stream).to be_an(IO)
103
+ end
104
+ end
105
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffmpeg_web
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jason
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ''
56
+ email:
57
+ - jsofokleous@googlemail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - ffmpeg_web.gemspec
68
+ - lib/ffmpeg_web.rb
69
+ - lib/ffmpeg_web/version.rb
70
+ - spec/fixtures/10 seconds.mov
71
+ - spec/fixtures/broken.mp4
72
+ - spec/spec_helper.rb
73
+ - spec/transcoder_spec.rb
74
+ homepage: https://github.com/jasonsof/ffmpeg_web
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A Ruby wrapper for FFMpeg that allows you to transcode video into a web optimised
98
+ format.
99
+ test_files:
100
+ - spec/fixtures/10 seconds.mov
101
+ - spec/fixtures/broken.mp4
102
+ - spec/spec_helper.rb
103
+ - spec/transcoder_spec.rb