-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_media_tagedit.rb
executable file
·82 lines (68 loc) · 2.04 KB
/
s_media_tagedit.rb
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
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# Bin is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bin. If not, see <https://www.gnu.org/licenses/>.
#
# TODO: permited media files extension
# TODO: walk through all sub-directories if required
# Requirements
# gem install taglib-ruby
require 'taglib'
require 'pathname'
require 'optparse'
TITLE = lambda do |info, file|
info = Pathname.new(file).basename('.*').to_path if info.empty?
TagLib::FileRef.open(file) do |t|
t.tag.title = info
t.save
end
end
ARTIST = lambda do |info, file|
TagLib::FileRef.open(file) do |t|
t.tag.artist = info
t.save
end
end
ALBUM = lambda do |info, file|
TagLib::FileRef.open(file) do |t|
t.tag.album = info
t.save
end
end
def run(filepath, info, func)
raise 'FILE NOT FOUND' unless filepath.exist?
if filepath.directory?
filepath.children.each { |file| func.call(info, file.to_path) }
else
func.call(info, filepath.to_path)
end
end
option_parser = OptionParser.new do |opts|
opts.banner = 'Usage: tagedit [options]'
filepath = String.new
opts.on('-fFILE', '--file=FILE', 'file to edit') do |file|
filepath = Pathname.new(file)
end
opts.on('-tTITLE', '--tittle=TITLE', 'edit title tag') do |title|
run(filepath, title, TITLE)
end
opts.on('-aNAME', '--album=NAME', 'edit album tag') do |info|
run(filepath, info, ALBUM)
end
opts.on('-sNAME', '--artist=NAME', 'edit artist tag') do |info|
run(filepath, info, ARTIST)
end
end
option_parser.parse! ['--help'] if ARGV.empty?
option_parser.parse!