alacit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/alacit +160 -0
  2. metadata +59 -0
data/bin/alacit ADDED
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # AlacIt
4
+ # by: Russell Brooks (russbrooks.com)
5
+ #
6
+ # Converts FLAC and WAV files to ALAC (Apple Lossless Audio Codec) files in
7
+ # an M4A container for importation into iTunes. Fast. No loss in quality.
8
+ # Basic metadata ports as well. Puts converted files in same dir as source.
9
+ #
10
+ # Dependency: FFmpeg 0.8.0+. But as far back as 0.5.0 should work too.
11
+ # On OS X : Get Homebrew and type `brew install ffmpeg`.
12
+ # On Linux: `sudo apt-get install flac ffmpeg`
13
+ # Windows : [untested]
14
+
15
+ require 'open3'
16
+ require 'optparse'
17
+ require 'version'
18
+
19
+ module AlacIt
20
+ class Converter
21
+ def initialize
22
+ @options = {}
23
+ @executable_name = File.split($0)[1]
24
+ @usage = "Usage: #{@executable_name} [options] dir [dir ...] [file ...]\n"
25
+ @usage += " #{@executable_name} [options] file [file ...] [dir ...]\n"
26
+
27
+ parse_args
28
+ ffmpeg_exists
29
+
30
+ convert!
31
+ end
32
+
33
+ def convert!
34
+ ARGV.each do |source|
35
+ if File.directory? source
36
+ convert_dir source
37
+ elsif File.file? source
38
+ convert_file source
39
+ else
40
+ abort "Error: #{source}: Not a file or directory."
41
+ end
42
+ end
43
+ exit $?.exitstatus
44
+ end
45
+
46
+ def convert_dir(source_dir)
47
+ source_glob = File.join(source_dir, '*.{flac,wav}')
48
+
49
+ unless Dir.glob(source_glob).empty?
50
+ Dir.glob(source_glob) do |file|
51
+ m4a_file = file.chomp(File.extname(file)) + '.m4a'
52
+
53
+ if !File.exists?(m4a_file) || @options[:force]
54
+ command = 'ffmpeg -y -i "' + file + '" -acodec alac "' + m4a_file + '"'
55
+ stdout_str, stderr_str, status = Open3.capture3(command)
56
+
57
+ if status.success?
58
+ puts "#{file}: Converted."
59
+ else
60
+ STDERR.puts "Error: #{file}: File could not be converted."
61
+ STDERR.puts stderr_str.split("\n").last
62
+ next
63
+ end
64
+ else
65
+ STDERR.puts "Error: #{m4a_file} exists."
66
+ next
67
+ end
68
+ end
69
+ else
70
+ STDERR.puts 'Error: No FLAC or WAV files found.'
71
+ return
72
+ end
73
+ end
74
+
75
+ def convert_file(file)
76
+ if File.extname(file) =~ /(\.flac|\.wav)/i
77
+ if File.exists? file
78
+ m4a_file = file.chomp(File.extname(file)) + '.m4a'
79
+
80
+ if !File.exists?(m4a_file) || @options[:force]
81
+ command = 'ffmpeg -y -i "' + file + '" -acodec alac "' + m4a_file + '"'
82
+ stdout_str, stderr_str, status = Open3.capture3(command)
83
+
84
+ if status.success?
85
+ puts "#{file}: Converted."
86
+ else
87
+ STDERR.puts "Error: #{file}: File could not be converted."
88
+ STDERR.puts stderr_str.split("\n").last
89
+ return
90
+ end
91
+ else
92
+ STDERR.puts "Error: #{m4a_file} exists."
93
+ return
94
+ end
95
+ else
96
+ STDERR.puts "Error: #{file}: No such file."
97
+ return
98
+ end
99
+ else
100
+ STDERR.puts "Error: #{file}: Not a FLAC or WAV file."
101
+ return
102
+ end
103
+ end
104
+
105
+ private
106
+
107
+ def parse_args
108
+ @opts_parser = OptionParser.new do |opts|
109
+ opts.banner = "Converts ALAC and WAV files to Apple Lossless.\n\n"
110
+ opts.banner += @usage + "\n"
111
+
112
+ opts.on('-f', '--force', 'Overwrite output files') do |f|
113
+ @options[:force] = true
114
+ end
115
+
116
+ opts.on('-v', '--version', 'AlacIt version') do
117
+ puts 'AlacIt 0.0.1' # Can't seem to read this from lib/version
118
+ exit
119
+ end
120
+ end
121
+ @opts_parser.parse!
122
+
123
+ args_exist
124
+ end
125
+
126
+ def args_exist
127
+ if ARGV.empty?
128
+ STDERR.puts 'Error: You must supply one or more file or directory names.'
129
+ STDERR.puts
130
+ STDERR.puts @usage
131
+ exit -1
132
+ end
133
+ end
134
+
135
+ def ffmpeg_exists
136
+ unless command? 'ffmpeg'
137
+ error = 'Error: FFmpeg executable not found.'
138
+ error += ' Install Homebrew and type `brew install ffmpeg`.' if os_is_mac?
139
+ error += ' To install, type `sudo apt-get install flac ffmpeg`.' if os_is_linux?
140
+ STDERR.puts error
141
+ exit 2
142
+ end
143
+ end
144
+
145
+ def command?(name)
146
+ `which #{name}`
147
+ $?.success?
148
+ end
149
+
150
+ def os_is_mac?
151
+ RUBY_PLATFORM.downcase.include? 'darwin'
152
+ end
153
+
154
+ def os_is_linux?
155
+ RUBY_PLATFORM.downcase.include? 'linux'
156
+ end
157
+ end
158
+ end
159
+
160
+ c = AlacIt::Converter.new
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alacit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Russell H. Brooks
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-30 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70147585331180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70147585331180
25
+ description: Quickly convert entire directories of FLAC and WAV files to Apple Lossless
26
+ (ALAC) for importation into iTunes, iPhones, iPads, and iPods.
27
+ email: me@russbrooks.com
28
+ executables:
29
+ - alacit
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - bin/alacit
34
+ homepage: http://russbrooks.com
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.11
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: FLAC and WAV to Apple Lossless (ALAC) batch conversion utility.
59
+ test_files: []