carrierwave-audio 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 49be079517a1eb28406b25ccca4a0060bd179c6e
4
+ data.tar.gz: 2700deff88842e204e30d388c4a31024259f39c7
5
+ SHA512:
6
+ metadata.gz: e515174ee12fe72e4a2964fb9d91d7276ef86ddb6ee00bbb19f85bc31b3f95105f3ebc9fefa8951f677644d4f40177308db7b646184cac8d5bbb54c4ac944841
7
+ data.tar.gz: 25510a82ccf5ee7fc64cad64ab1127f4d8cf7b2345881bec62dee7b871a93e254d5ad235c3eeb83e1d035650546a4c60afb8b22b27a288eac34d904aa3f2cb13
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jiri Kolarik
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.
@@ -0,0 +1,105 @@
1
+ # CarrierWave::Audio
2
+
3
+ Simple SoX wrapper for CarrierWave uploader that allows audio file conversion and watermarking
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'carrierwave-audio'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install carrierwave-audio
18
+
19
+ ## Usage
20
+
21
+ Include CarrierWave::Audio into your CarrierWave uploader class:
22
+
23
+ ```ruby
24
+ class VideoUploader < CarrierWave::Uploader::Base
25
+ include CarrierWave::Audio
26
+ end
27
+ ```
28
+
29
+ This gem provides two new processors for uploading audio files, `convert` and `watermark`.
30
+
31
+ ### Convert
32
+
33
+ If you'd like to convert from your initially uploaded file-type to a different one, use `convert` like so:
34
+
35
+ ```ruby
36
+ version :mp3 do
37
+ process :convert => [:mp3]
38
+
39
+ def full_filename(for_file)
40
+ "#{super.chomp(File.extname(super))}.mp3"
41
+ end
42
+ end
43
+ ```
44
+
45
+ `process :convert` accepts two parameters:
46
+
47
+ ```ruby
48
+ process :convert => [output_format, output_options]
49
+ ```
50
+
51
+ `output_format` - Optional. The only currently available option is the default, `:mp3`.
52
+
53
+ `output_options` - Optional. Sox options for the output file (see [ruby-sox](https://github.com/TMXCredit/ruby-sox) and the [SoX documentation](http://sox.sourceforge.net/sox.pdf)). Defaults to:
54
+
55
+ ```ruby
56
+ {
57
+ type: output_format.to_s,
58
+ rate: 44100,
59
+ channels: 2,
60
+ compression: 128
61
+ }
62
+ ```
63
+
64
+ ### Watermark
65
+
66
+ If you'd like to add a watermark over the top of your file, use `watermark` like so:
67
+
68
+ ```ruby
69
+ version :watermarked do
70
+ process :watermark => ["#{Rails.root}/db/watermark.mp3"]
71
+
72
+ def full_filename(for_file)
73
+ "#{super.chomp(File.extname(super))}.mp3"
74
+ end
75
+ end
76
+ ```
77
+
78
+ `process :watermark` accepts three parameters:
79
+
80
+ ```ruby
81
+ process :watermark => [watermark_file_path, output_format, output_options]
82
+ ```
83
+
84
+ `watermark_file_path` - REQUIRED. Path to where your watermarked file is stored
85
+
86
+ `output_format` - Optional. The only currently available option is the default, `:mp3`.
87
+
88
+ `output_options` - Optional. Sox options for the output file (see [ruby-sox](https://github.com/TMXCredit/ruby-sox) and the [SoX documentation](http://sox.sourceforge.net/sox.pdf)). Defaults to:
89
+
90
+ ```ruby
91
+ {
92
+ type: output_format.to_s,
93
+ rate: 44100,
94
+ channels: 2,
95
+ compression: 128
96
+ }
97
+ ```
98
+
99
+ ## Contributing
100
+
101
+ 1. Fork it
102
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
103
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
104
+ 4. Push to the branch (`git push origin my-new-feature`)
105
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require 'carrierwave/audio'
@@ -0,0 +1,97 @@
1
+ require 'carrierwave'
2
+ require 'ruby-sox'
3
+ require 'soxi/wrapper'
4
+
5
+ module CarrierWave
6
+ module Audio
7
+ module ClassMethods
8
+ extend ActiveSupport::Concern
9
+
10
+ def convert output_format = :mp3, output_options = {}
11
+ process convert: [ output_format, output_options ]
12
+ end
13
+
14
+ def watermark watermark_file_path, output_format = :mp3, output_options = {}
15
+ process watermark: [ watermark_file_path, output_format, output_options ]
16
+ end
17
+ end
18
+
19
+ def convert output_format = :mp3, output_options = {}
20
+ format = sanitized_format(output_format)
21
+ ext = File.extname(current_path)
22
+ input_options = { type: ext.gsub(/\./, '') }
23
+ current_filename_without_extension = File.basename(current_path, ext)
24
+ tmp_path = File.join File.dirname(current_path), "tmp_#{current_filename_without_extension}_#{Time.current.to_i}.#{format}"
25
+ convert_file(current_path, input_options, tmp_path, default_output_options(format).merge(output_options))
26
+ File.rename tmp_path, current_path
27
+ set_content_type format
28
+ end
29
+
30
+ def watermark watermark_file_path, output_format = :mp3, output_options = {}
31
+ format = sanitized_format(output_format)
32
+ ext = File.extname(current_path)
33
+ watermark_ext = File.extname(watermark_file_path)
34
+ input_options = { type: ext.gsub(/\./, '') }
35
+ watermark_options = { type: watermark_ext.gsub(/\./, '') }
36
+ current_filename_without_extension = File.basename(current_path, ext)
37
+
38
+ # Normalize file to -6dB
39
+ normalized_tmp_path = File.join File.dirname(current_path), "tmp_norm_#{current_filename_without_extension}_#{Time.current.to_i}.#{input_options[:type]}"
40
+ convert_file(current_path, input_options, normalized_tmp_path, input_options, { gain: "-n -6" })
41
+
42
+ # Combine normalized file and watermark, normalizing final product to 0dB
43
+ final_tmp_path = File.join File.dirname(current_path), "tmp_wtmk_#{current_filename_without_extension}_#{Time.current.to_i}.#{format}"
44
+ converter = Sox::Cmd.new(combine: :mix)
45
+ converter.add_input normalized_tmp_path, input_options
46
+ converter.add_input watermark_file_path, watermark_options
47
+ converter.set_output final_tmp_path, default_output_options(format).merge(output_options)
48
+ converter.set_effects({ trim: "0 #{Soxi::Wrapper.file(current_path).seconds}", gain: "-n" })
49
+ converter.run
50
+ File.rename final_tmp_path, current_path
51
+ set_content_type format
52
+ end
53
+
54
+ private
55
+
56
+ def convert_file input_file_path, input_options, output_file_path, output_options, fx = {}
57
+ converter = Sox::Cmd.new
58
+ converter.add_input input_file_path, input_options
59
+ converter.set_output output_file_path, output_options
60
+ converter.set_effects fx
61
+ converter.run
62
+ end
63
+
64
+ def sanitized_format format
65
+ supported_formats = [:mp3]
66
+ if supported_formats.include?(format.to_sym)
67
+ format.to_s
68
+ else
69
+ raise CarrierWave::ProcessingError.new("Unsupported audio format #{format}. Only conversion to #{supported_formats.to_sentence} allowed.")
70
+ end
71
+ end
72
+
73
+ def default_output_options format
74
+ if format.to_sym == :mp3
75
+ {
76
+ type: format.to_s,
77
+ rate: 44100,
78
+ channels: 2,
79
+ compression: 128
80
+ }
81
+ else
82
+ {
83
+ type: format.to_s,
84
+ rate: 44100,
85
+ channels: 2
86
+ }
87
+ end
88
+ end
89
+
90
+ def set_content_type format
91
+ case format.to_sym
92
+ when :mp3
93
+ self.file.instance_variable_set(:@content_type, "audio/mpeg3")
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,5 @@
1
+ module CarrierWave
2
+ module Audio
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-audio
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Trevor Hinesley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: carrierwave
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-sox
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: soxi-wrapper
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: CarrierWave Audio
84
+ email:
85
+ - trevor@trevorhinesley.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE.txt
91
+ - README.md
92
+ - lib/carrierwave-audio.rb
93
+ - lib/carrierwave/audio.rb
94
+ - lib/carrierwave/audio/version.rb
95
+ homepage: https://github.com/TrevorHinesley/carrierwave-audio
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Simple SoX wrapper for CarrierWave uploader that allows audio file conversion
119
+ and watermarking
120
+ test_files: []