summaryrefslogtreecommitdiff
path: root/omptagger
blob: f4198ca01d628c47f59ab2e4a2f1979aa210ec3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env ruby
#
# omptagger
# http://github.com/omp/omptagger
# Modify and display metadata of audio files.
#
# Copyright 2007-2010 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
# Distributed under the terms of the GNU General Public License v3.
# See http://www.gnu.org/licenses/gpl.txt for the full license text.

require 'getoptlong'
require 'rubygems'
require 'TagLib'

class Metadata
  def output(tag, val)
    spacing = 2 + @tags.keys.inject(0) do |length, object|
      object.length > length ? object.length : length
    end - tag.length

    puts '    ' + tag + ' ' * spacing + val
  end
end

class VorbisComment < Metadata
  def view
    puts '  Viewing tags:'

    if @tags.empty?
      puts '    No tags are set.'
      return
    end

    @tags.sort.each do |tag, val|
      val.each do |val|
        output(tag, val)
      end
    end
  end
end

class FLAC < VorbisComment
  def initialize(file)
    @filename = File.expand_path(file)
    @write = false

    @file = TagLib::FLAC::File.new(file)
    @tags = @file.xiphComment.fieldListMap.hash
  end
end

def help
  puts <<-end
Usage: omptagger [options] [files]

Options:
  --view        -v  View all tags
  --view-tag    -t  View a tag
  --set-tag     -s  Set a tag
  --remove      -r  Remove all tags
  --remove-tag  -d  Remove a tag
  --generate    -g  Generate tags based on filename
  --rename      -m  Generate filename based on tags
  --scheme      -n  Specify a file naming scheme
  --pretend     -p  Do not make any actual changes
  --list        -l  Display list of available tags
  --help        -h  Display help information

Notes:
  * The default file naming scheme is Artist - Title.
  * Schemes must be specified prior to actions that use them.
  end

  exit
end

def list
  puts <<-end
┌────────────────────────┐
│ Keys   Vorbis Comments │
├────────────────────────┤
│ %a     Artist          │
│ %b     Album           │
│ %d     Date            │
│ %n     TrackNumber     │
│ %t     Title           │
│ %*     Wildcard        │
├────────────────────────┤
│        Contact         │
│        Copyright       │
│        Description     │
│        Genre           │
│        ISRC            │
│        License         │
│        Location        │
│        Organization    │
│        Performer       │
│        Version         │
└────────────────────────┘
  end

  exit
end

actions = Array.new
options = Array.new
scheme  = '%a - %t'

GetoptLong.new(
  ['--view',       '-v', GetoptLong::NO_ARGUMENT],
  ['--view-tag',   '-t', GetoptLong::REQUIRED_ARGUMENT],
  ['--set-tag',    '-s', GetoptLong::REQUIRED_ARGUMENT],
  ['--remove',     '-r', GetoptLong::NO_ARGUMENT],
  ['--remove-tag', '-d', GetoptLong::REQUIRED_ARGUMENT],
  ['--generate',   '-g', GetoptLong::NO_ARGUMENT],
  ['--rename',     '-m', GetoptLong::NO_ARGUMENT],
  ['--scheme',     '-n', GetoptLong::REQUIRED_ARGUMENT],
  ['--pretend',    '-p', GetoptLong::NO_ARGUMENT],
  ['--list',       '-l', GetoptLong::NO_ARGUMENT],
  ['--help',       '-h', GetoptLong::NO_ARGUMENT]
).each do |option, argument|
  option = option.delete('-').intern

  case option
  when :scheme
    scheme = argument
  when :help, :list, :pretend
    options << option
  when :viewtag, :settag, :removetag
    actions << [option, argument]
  when :generate, :rename
    actions << [option, scheme]
  else
    actions << [option]
  end
end

help if options.include?(:help)
list if options.include?(:list)
help if actions.empty?

if ARGV.empty?
  puts 'No files specified.'
  exit
end

ARGV.each do |file|
  begin
    raise 'File does not exist.' unless File.exist?(file)

    case File.extname(file).downcase
    when '.flac'
      track = FLAC.new(file)
    else
      raise 'File extension not recognised.'
    end

    puts file + ':'

    actions.each do |action|
      track.send(*action)
    end
  rescue RuntimeError => message
    $stderr.puts file + ':'
    $stderr.puts '  ' + message
  end
end