audiomator 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cee451824e25a7fcefed1d9cb967a5921c8d98b1
4
+ data.tar.gz: d9656a18a2d5d63b6f6deab65459885b8cb1eb5e
5
+ SHA512:
6
+ metadata.gz: 8fd4ab80d5e4b943dd94d85a2109bf35b7c8987ace0d645addb11385b714c32cbdc3a983aec75ab419810c73b7dc4cc8f22056ca0456b644af496fccc7f93da0
7
+ data.tar.gz: f65a60dcc9986380b0017f7d112f1491826d562ffc0c98c692488f62af5f422462b018a56a4e7cc6c16954d9f5d166969daec647e867fd8b67d9234ae9645df9
@@ -0,0 +1,13 @@
1
+ ### Steps to reproduce
2
+
3
+ Tell us how to reproduce
4
+
5
+ ### Expected behavior
6
+ Tell us what should happen
7
+
8
+ ### Actual behavior
9
+ Tell us what happens instead
10
+
11
+ ### System configuration
12
+
13
+ **Ruby version**:
@@ -0,0 +1,17 @@
1
+ ### Summary
2
+
3
+ Provide a general description of the code changes in your pull
4
+ request... were there any bugs you had fixed? If so, mention them. If
5
+ these bugs have open GitHub issues, be sure to tag them here as well,
6
+ to keep the conversation linked together.
7
+
8
+ ### Other Information
9
+
10
+ If there's anything else that's important and relevant to your pull
11
+ request, mention that information here. This could include
12
+ benchmarks, or other information.
13
+
14
+ If you are updating any of the CHANGELOG files or are asked to update the
15
+ CHANGELOG files by reviewers, please add the CHANGELOG entry at the top of the file.
16
+
17
+ Thanks for contributing to Audiomator!
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,26 @@
1
+ image:
2
+ ruby:ffmpeg
3
+
4
+ before_script:
5
+ - export LC_ALL="C.UTF-8"
6
+ - export RAILS_ENV=test
7
+ - export RACK_ENV=test
8
+ - eval "$(rbenv init -)"
9
+ - rbenv global 2.2.2
10
+ - rbenv rehash
11
+ - ruby -v
12
+ - gem -v
13
+
14
+ - bundle install -j8 --path /cache/bundle --quiet
15
+
16
+
17
+ stages:
18
+ - test
19
+
20
+ job_test:
21
+ stage: test
22
+ tags:
23
+ - ruby
24
+
25
+ script:
26
+ - bundle exec rspec
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ - 2.3.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in audiomator.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Yuan He
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ # Audiomator
2
+
3
+ A ffmpeg wrapper for ruby to easy split and processing audio files.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'audiomator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install audiomator
20
+
21
+ ## Usage
22
+
23
+ ## Load an audio file
24
+ @audio_file = "PATH/TO/AUDIO_FILE/FILENAME.m4a"
25
+ @record = Audiomator::Record.new(@audio_file)
26
+
27
+ # Get audio metadata
28
+ @record.duration
29
+ @record.bitrate
30
+ @record.size
31
+ @record.sample_rate
32
+ @record.codec
33
+
34
+ # any FFMPEG errors when loading audio
35
+ @record.valid?
36
+
37
+ # clip audio with start_time and end_time
38
+ @start_time = '00:00:00.59'
39
+ @end_time = '00:00:01.53'
40
+ @record.clip(@start_time, @end_time)
41
+
42
+ # Adding metadata to output audio file
43
+ metadata = {description: 'This is AudioMator'}
44
+ @record.clip(@start_time, @end_time, nil, metadata: metadata)
45
+
46
+ # Add prefix to save all clips to a specify folder
47
+ @record.clip(@start_time, @end_time, nil, prefix: 'clips')
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( https://github.com/lenage/audiomator/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task default: :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'audiomator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "audiomator"
8
+ spec.version = Audiomator::VERSION
9
+ spec.authors = ["Yuan He"]
10
+ spec.email = ["lendage@gmail.com"]
11
+
12
+ spec.summary = %q{A ffmpeg wrapper for ruby to easy split and processing audio files.}
13
+ spec.description = %q{A ffmpeg wrapper for ruby to easy split and processing audio files.}
14
+ spec.homepage = "https://github.com/lenage/audiomator"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.3.0"
25
+ spec.add_development_dependency "pry", "~> 0.10.1"
26
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "audiomator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,55 @@
1
+ require 'logger'
2
+
3
+ require 'audiomator/error'
4
+ require 'audiomator/options'
5
+ require 'audiomator/record'
6
+ require 'audiomator/version'
7
+
8
+ # Main module for Audiomator
9
+ module Audiomator
10
+ # Set FFMPEG logs about its process when it's transcoding
11
+ # @params {logger} log you own logger
12
+ # @return [Logger] the logger you ser
13
+ def self.logger=(log)
14
+ @logger = log
15
+ end
16
+
17
+ # Get FFMPEG logger.
18
+ #
19
+ # @return [Logger]
20
+ def self.logger
21
+ return @logger if @logger
22
+ logger = ::Logger.new(STDOUT)
23
+ logger.level = ::Logger::INFO
24
+ @logger = logger
25
+ end
26
+
27
+ # Set the path of ffmpeg
28
+ # Can be useful if you need to specify a path as /usr/local/bin/ffmpeg
29
+ #
30
+ # @param [String] path to the ffmpeg
31
+ # @return [String] the path of ffmpeg
32
+ def self.ffmpeg=(bin)
33
+ @ffmpeg = bin
34
+ end
35
+
36
+ def self.ffmpeg
37
+ @ffmpeg || 'ffmpeg'
38
+ end
39
+
40
+ def self.ffprobe=(bin)
41
+ @ffprobe = bin
42
+ end
43
+
44
+ def self.ffprobe
45
+ @ffprobe || 'ffprobe'
46
+ end
47
+
48
+ def self.timeout=(time)
49
+ @timeout = time
50
+ end
51
+
52
+ def self.timeout
53
+ @timeout || 45
54
+ end
55
+ end
@@ -0,0 +1,4 @@
1
+ module Audiomator
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,20 @@
1
+ require 'shellwords'
2
+ module Audiomator
3
+ # Options which convert audio to
4
+ class Options < Struct.new(:start_time, :end_time, :bitrate, :sample_rate, :metadata)
5
+
6
+ def initialize(start_time, end_time, bitrate = '96k', sample_rate = '44100', metadata = {})
7
+ super
8
+ end
9
+
10
+ # see http://multimedia.cx/eggs/supplying-ffmpeg-with-metadata/ for metadata
11
+ def metadata_string
12
+ return '' if metadata.empty?
13
+ metadata.map { |k, v| "-metadata #{k}=#{Shellwords.escape(v)}" }.join(' ')
14
+ end
15
+
16
+ def to_s
17
+ "-b:a #{bitrate} -ar #{sample_rate} -ss #{start_time} -to #{end_time} #{metadata_string}"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,126 @@
1
+ require 'timeout'
2
+ require 'time'
3
+ require 'shellwords'
4
+ require 'open3'
5
+ require 'audiomator/options'
6
+ require 'fileutils'
7
+
8
+ module Audiomator
9
+ class Record
10
+ attr_reader :path, :duration, :time, :bitrate, :rotation, :creation_time
11
+ attr_reader :stream, :codec, :sample_rate
12
+ attr_reader :container
13
+
14
+ def initialize(path)
15
+ fail Errno::ENOENT, "'#{path}' does not exist" unless File.exist?(path)
16
+ @path = path
17
+
18
+ # ffmpeg will output to stderr
19
+ @output = Open3.popen3(ffmpeg_command) { |_stdin, _stdout, stderr| stderr.read }
20
+
21
+ parse_container
22
+ parse_duration
23
+ parse_time
24
+ parse_creation_time
25
+ parse_stream_info
26
+ validate
27
+ end
28
+
29
+ def valid?
30
+ !@invalid
31
+ end
32
+
33
+ def size
34
+ File.size(@path)
35
+ end
36
+
37
+ def basename
38
+ File.basename(@path, '.*')
39
+ end
40
+
41
+ def clip(start_time, end_time, output = nil, options = {})
42
+ options = default_clip_options.merge!(options)
43
+ output = output_file(start_time, end_time, options) unless output
44
+ FileUtils.mkdir_p(File.dirname(output)) unless Dir.exist?(File.dirname(output))
45
+ opts = Options.new(start_time,
46
+ end_time,
47
+ options[:bitrate],
48
+ options[:sample_rate],
49
+ options[:metadata])
50
+ command = [ffmpeg_command, opts.to_s, output].join(' ')
51
+ Audiomator.logger.info("Running audio processing...\n #{command}\n")
52
+ @output_error = ''
53
+
54
+ begin
55
+ Timeout.timeout(Audiomator.timeout) do
56
+ _stdout, stderr, status = Open3.capture3(command)
57
+ @output_error = stderr
58
+ unless status.success?
59
+ fail Error, "Proecess Failed, Full output: #{stderr}"
60
+ end
61
+ end
62
+ rescue Timeout::Error => e
63
+ raise Error, "Proecess Timeout #{e.message} \n: #{@output_error} \n"
64
+ end
65
+ Audiomator.logger.info("Audio processing finished\n")
66
+ end
67
+
68
+ private
69
+
70
+ def output_file(start_time, end_time, options = default_clip_options)
71
+ options = default_clip_options.merge!(options)
72
+ output_filename = "#{basename}-#{duration}-#{start_time}_#{end_time}.#{options[:format]}"
73
+ output_filename = "#{options[:prefix].to_s}/#{output_filename}" if options[:prefix]
74
+ File.join File.dirname(@path), output_filename
75
+ end
76
+
77
+ def default_clip_options
78
+ { bitrate: '32k', format: 'm4a', metadata: {}, sample_rate: '44100'}
79
+ end
80
+
81
+ def ffmpeg_command
82
+ "#{Audiomator.ffmpeg} -i #{Shellwords.escape(@path)}"
83
+ end
84
+
85
+ def parse_container
86
+ @output[/Input \#\d+\,\s*(\S+),\s*from/]
87
+ @container = $1
88
+ end
89
+
90
+ def parse_duration
91
+ @output[/Duration: (\d{2}):(\d{2}):(\d{2}\.\d{2})/]
92
+ @duration = ($1.to_i * 60 * 60) + ($2.to_i * 60) + $3.to_f
93
+ end
94
+
95
+ def parse_time
96
+ @output[/start: (\d*\.\d*)/]
97
+ @time = $1 ? $1.to_f : 0.0
98
+ end
99
+
100
+ def parse_creation_time
101
+ @output[/creation_time {1,}: {1,}(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/]
102
+ @creation_time = $1 ? Time.parse("#{$1}") : nil
103
+ end
104
+
105
+ def parse_bitrate
106
+ @output[/bitrate: (\d*)/]
107
+ @bitrate = $1 ? $1.to_i : nil
108
+ end
109
+
110
+ def parse_stream_info
111
+ @output[/Audio:\ (.*)/]
112
+ @stream = $1
113
+ if stream
114
+ @codec, audio_sample_rate, @channels, _, audio_bitrate = stream.split(/\s?,\s?/)
115
+ @bitrate = audio_bitrate =~ %r(\A(\d+) kb/s) ? $1.to_i : nil
116
+ @sample_rate = audio_sample_rate[/\d*/].to_i
117
+ end
118
+ end
119
+
120
+ def validate
121
+ @invalid = true if @output.include?('is not supported')
122
+ @invalid = true if @output.include?('Invalid data found when processing input')
123
+ @invalid = true if @output.include?('could not find codec parameters')
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,3 @@
1
+ module Audiomator
2
+ VERSION = "0.3.0"
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: audiomator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuan He
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-09 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: 3.3.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.3.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.1
69
+ description: A ffmpeg wrapper for ruby to easy split and processing audio files.
70
+ email:
71
+ - lendage@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".github/issue_template.md"
77
+ - ".github/pull_request_template.md"
78
+ - ".gitignore"
79
+ - ".gitlab-ci.yml"
80
+ - ".rspec"
81
+ - ".travis.yml"
82
+ - CODE_OF_CONDUCT.md
83
+ - Gemfile
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - audiomator.gemspec
88
+ - bin/console
89
+ - bin/setup
90
+ - lib/audiomator.rb
91
+ - lib/audiomator/error.rb
92
+ - lib/audiomator/options.rb
93
+ - lib/audiomator/record.rb
94
+ - lib/audiomator/version.rb
95
+ homepage: https://github.com/lenage/audiomator
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: A ffmpeg wrapper for ruby to easy split and processing audio files.
119
+ test_files: []