youpy-scissor 0.0.1

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.
Files changed (5) hide show
  1. data/ChangeLog +4 -0
  2. data/README +36 -0
  3. data/Rakefile +105 -0
  4. data/lib/scissor.rb +129 -0
  5. metadata +77 -0
data/ChangeLog ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 / 2009-03-29
2
+
3
+ * initial release
4
+
data/README ADDED
@@ -0,0 +1,36 @@
1
+ = Scissor
2
+
3
+ == Description
4
+ utility to chop mp3 files
5
+
6
+ == Installation
7
+
8
+ === Archive Installation
9
+
10
+ rake install
11
+
12
+ === Gem Installation
13
+
14
+ gem install youpy-scissor
15
+
16
+ == Features/Problems
17
+
18
+ == Synopsis
19
+
20
+ foo = Scissor.new('foo.mp3')
21
+ var = Scissor.new('bar.mp3')
22
+
23
+ # concat
24
+ (foo + bar).to_file('foobar.mp3')
25
+
26
+ # slice + concat
27
+ (foo.slice(10, 1) + bar.slice(2,3)).to_file('slicefoobar.mp3')
28
+
29
+ # slice + concat + loop
30
+ ((foo.slice(10, 1) + bar.slice(2,3)) * 3).to_file('slicefoobarloop.mp3')
31
+
32
+ == Copyright
33
+
34
+ Author:: youpy <youpy@buycheapviagraonlinenow.com>
35
+ Copyright:: Copyright (c) 2009 youpy
36
+ License:: MIT
data/Rakefile ADDED
@@ -0,0 +1,105 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'spec/rake/spectask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'rake/contrib/sshpublisher'
10
+ require 'fileutils'
11
+ include FileUtils
12
+
13
+ NAME = "scissor"
14
+ AUTHOR = "youpy"
15
+ EMAIL = "youpy@buycheapviagraonlinenow.com"
16
+ DESCRIPTION = "utility to chop mp3 files"
17
+ RUBYFORGE_PROJECT = "scissor"
18
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
19
+ BIN_FILES = %w( )
20
+ VERS = "0.0.1"
21
+
22
+ REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
23
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config']
24
+ RDOC_OPTS = [
25
+ '--title', "#{NAME} documentation",
26
+ "--charset", "utf-8",
27
+ "--opname", "index.html",
28
+ "--line-numbers",
29
+ "--main", "README",
30
+ "--inline-source",
31
+ ]
32
+
33
+ task :default => [:spec]
34
+ task :package => [:clean]
35
+
36
+ Spec::Rake::SpecTask.new do |t|
37
+ t.spec_opts = ['--options', "spec/spec.opts"]
38
+ t.spec_files = FileList['spec/*_spec.rb']
39
+ t.rcov = true
40
+ end
41
+
42
+ spec = Gem::Specification.new do |s|
43
+ s.name = NAME
44
+ s.version = VERS
45
+ s.platform = Gem::Platform::RUBY
46
+ s.has_rdoc = true
47
+ s.extra_rdoc_files = ["README", "ChangeLog"]
48
+ s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/']
49
+ s.summary = DESCRIPTION
50
+ s.description = DESCRIPTION
51
+ s.author = AUTHOR
52
+ s.email = EMAIL
53
+ s.executables = BIN_FILES
54
+ s.rubyforge_project = RUBYFORGE_PROJECT
55
+ s.bindir = "bin"
56
+ s.require_path = "lib"
57
+ s.homepage = HOMEPATH
58
+ s.test_files = Dir["test/test_*.rb"]
59
+
60
+ s.add_dependency('ruby-mp3info')
61
+ #s.required_ruby_version = '>= 1.8.2'
62
+
63
+ s.files = %w(README ChangeLog Rakefile) +
64
+ Dir.glob("{bin,doc,test,lib,templates,generator,extras,website,script}/**/*") +
65
+ Dir.glob("ext/**/*.{h,c,rb}") +
66
+ Dir.glob("examples/**/*.rb") +
67
+ Dir.glob("tools/*.rb")
68
+
69
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
70
+ end
71
+
72
+ Rake::GemPackageTask.new(spec) do |p|
73
+ p.need_tar = true
74
+ p.gem_spec = spec
75
+ end
76
+
77
+ task :install do
78
+ name = "#{NAME}-#{VERS}.gem"
79
+ sh %{rake package}
80
+ sh %{sudo gem install pkg/#{name}}
81
+ end
82
+
83
+ task :uninstall => [:clean] do
84
+ sh %{sudo gem uninstall #{NAME}}
85
+ end
86
+
87
+
88
+ Rake::RDocTask.new do |rdoc|
89
+ rdoc.rdoc_dir = 'html'
90
+ rdoc.options += RDOC_OPTS
91
+ rdoc.template = "resh"
92
+ #rdoc.template = "#{ENV['template']}.rb" if ENV['template']
93
+ if ENV['DOC_FILES']
94
+ rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
95
+ else
96
+ rdoc.rdoc_files.include('README', 'ChangeLog')
97
+ rdoc.rdoc_files.include('lib/**/*.rb')
98
+ rdoc.rdoc_files.include('ext/**/*.c')
99
+ end
100
+ end
101
+
102
+ desc 'Show information about the gem.'
103
+ task :debug_gem do
104
+ puts spec.to_ruby
105
+ end
data/lib/scissor.rb ADDED
@@ -0,0 +1,129 @@
1
+ require 'mp3info'
2
+ require 'fileutils'
3
+
4
+ include FileUtils
5
+
6
+ class Scissor
7
+ class Error < StandardError; end
8
+ class CommandNotFound < Error; end
9
+
10
+ attr_reader :fragments
11
+
12
+ def initialize(filename = nil)
13
+ which('ffmpeg')
14
+ which('mp3wrap')
15
+
16
+ @fragments = []
17
+
18
+ if filename
19
+ @fragments << Fragment.new(
20
+ filename,
21
+ 0,
22
+ Mp3Info.new(filename).length)
23
+ end
24
+ end
25
+
26
+ def add_fragment(fragment)
27
+ @fragments << fragment
28
+ end
29
+
30
+ def duration
31
+ @fragments.inject(0) do |memo, fragment|
32
+ memo += fragment.duration
33
+ end
34
+ end
35
+
36
+ def slice(start, length)
37
+ new_mp3 = self.class.new()
38
+ remain = length
39
+
40
+ @fragments.each do |fragment|
41
+ if start >= fragment.duration
42
+ start -= fragment.duration
43
+
44
+ next
45
+ end
46
+
47
+ if (start + fragment.duration) >= remain
48
+ new_mp3.add_fragment(Fragment.new(
49
+ fragment.filename,
50
+ fragment.start + start,
51
+ remain))
52
+
53
+ break
54
+ else
55
+ remain = remain - fragment.duration
56
+ new_mp3.add_fragment(Fragment.new(
57
+ fragment.filename,
58
+ fragment.start + start,
59
+ fragment.duration))
60
+
61
+ start = 0
62
+ end
63
+ end
64
+
65
+ new_mp3
66
+ end
67
+
68
+ def which(command)
69
+ result = `which #{command}`
70
+ $?.exitstatus == 0 ? result.chomp :
71
+ (raise CommandNotFound.new(command + ' not found'))
72
+ end
73
+
74
+ def +(other)
75
+ other.fragments.each do |fragment|
76
+ add_fragment(fragment)
77
+ end
78
+
79
+ self
80
+ end
81
+
82
+ def *(count)
83
+ orig_fragments = @fragments.clone
84
+
85
+ (count - 1).times do
86
+ orig_fragments.each do |fragment|
87
+ add_fragment(fragment)
88
+ end
89
+ end
90
+
91
+ self
92
+ end
93
+
94
+ def to_file(filename)
95
+ outfiles = []
96
+ tmpdir = '/tmp/scissor-' + $$.to_s
97
+ mkdir tmpdir
98
+
99
+ # slice mp3 files
100
+ @fragments.each_with_index do |fragment, index|
101
+ outfile = tmpdir + '/' + index.to_s + '.mp3'
102
+ outfiles << outfile
103
+ cmd = "ffmpeg -i #{fragment.filename} -ss #{fragment.start} -t #{fragment.duration} #{outfile}"
104
+ system cmd
105
+ end
106
+
107
+ # concat mp3 files
108
+ cmd = "mp3wrap #{filename} #{outfiles.join(' ')}"
109
+ system cmd
110
+
111
+ # fix duration and rename
112
+ cmd = "ffmpeg -i #{filename.sub(/\.mp3$/, '_MP3WRAP.mp3')} -acodec copy #{filename}"
113
+ system cmd
114
+
115
+ rm_rf tmpdir
116
+
117
+ open(filename)
118
+ end
119
+
120
+ class Fragment
121
+ attr_reader :filename, :start, :duration
122
+
123
+ def initialize(filename, start, duration)
124
+ @filename = filename
125
+ @start = start
126
+ @duration = duration
127
+ end
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: youpy-scissor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - youpy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-29 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby-mp3info
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: utility to chop mp3 files
26
+ email: youpy@buycheapviagraonlinenow.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - ChangeLog
34
+ files:
35
+ - README
36
+ - ChangeLog
37
+ - Rakefile
38
+ - lib/scissor.rb
39
+ has_rdoc: true
40
+ homepage: http://scissor.rubyforge.org
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --title
44
+ - scissor documentation
45
+ - --charset
46
+ - utf-8
47
+ - --opname
48
+ - index.html
49
+ - --line-numbers
50
+ - --main
51
+ - README
52
+ - --inline-source
53
+ - --exclude
54
+ - ^(examples|extras)/
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: scissor
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: utility to chop mp3 files
76
+ test_files: []
77
+