]> Sergey Matveev's repositories - torn.git/blob - torn
Remove ._ after track number. Overwriting safety
[torn.git] / torn
1 #!/usr/bin/env perl
2 # torn - Musical files renaming with russian language transliteration
3 # Copyright (C) 2007-2020 Sergey Matveev (stargrave@stargrave.org)
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, version 3 of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 =pod
18
19 =head1 DESCRIPTION
20
21 This Perl script takes directory file list and renames all files,
22 making some substitution changes.  It has some rules for musical
23 files and rules to convert russian filenames to translit.
24
25 =head1 USAGE
26
27 Simply run this script in directory where you want files and/or
28 directorires to be renamed.
29
30 =head1 OVERVIEW
31
32 =over 4
33
34 =item * Transliterate everything
35
36 =item * Replaces spaces with underscore
37
38 =item * Downcase all extensions
39
40 =item * "_-_" so good looking on the screen is replaced by single dash
41
42 =item * Remove spaces before and after brackets
43
44 =item * Remove "[]" brackets
45
46 =item * Replace ampersand with "and" word
47
48 =back
49
50 =head1 AUTHOR
51
52 Sergey Matveev L<mailto:stargrave@stargrave.org>
53
54 =cut
55
56 use strict;
57 use utf8;
58 use Encode;
59
60 binmode STDOUT, ":utf8";
61
62 my $VERSION = "0.10";
63
64 my $src;
65 my $dst;
66 my $src_filename;
67
68 if ($#ARGV >= 0) {
69     print "torn version $VERSION, Copyright (C) 2007-2020 Sergey Matveev
70 torn comes with ABSOLUTELY NO WARRANTY.  This is free software,
71 and you are welcome to redistribute it under certain conditions.\n
72 Usage: just run inside the directory. Look for POD inside the script itself.\n";
73 };
74
75 opendir DIR, "." or die "Can not open directory\n";
76 foreach (sort readdir DIR) {
77     # Skip directory itself
78     next if /^\.{1,2}$/;
79     next if -d;
80
81     $src_filename = $_;
82     $src = decode "utf-8", $src_filename;
83     $dst = $src;
84
85     # Basic corrections for music files
86     $dst =~ s/ /_/g;
87     $dst =~ s/_-_/-/g;
88     $dst =~ s/_\(/\(/g;
89     $dst =~ s/-\(/-/g;
90     $dst =~ s/\)_/-/g;
91     $dst =~ s/\,_/\,/g;
92     $dst =~ s/\[//g;
93     $dst =~ s/\]//g;
94     $dst =~ s/\(_/\(/g;
95     $dst =~ s/_\)/\)/g;
96     $dst =~ s/\&/and/g;
97
98     # Make translit
99     $dst =~ y/абвгдеёзийклмнопрстуфхцьъыэ/abvgdeezijklmnoprstufhcjjye/;
100     $dst =~ y/АБВГДЕЁЗИЙКЛМНОПРСТУФХЦЬЪЫЭ/ABVGDEEZIJKLMNOPRSTUFHCJJYE/;
101     $dst =~ s/ж/zh/g;
102     $dst =~ s/ч/ch/g;
103     $dst =~ s/ш/sh/g;
104     $dst =~ s/щ/sch/g;
105     $dst =~ s/я/ja/g;
106     $dst =~ s/ю/ju/g;
107     $dst =~ s/Ж/Zh/g;
108     $dst =~ s/Ч/Ch/g;
109     $dst =~ s/Ш/Sh/g;
110     $dst =~ s/Щ/Sch/g;
111     $dst =~ s/Я/Ja/g;
112     $dst =~ s/Ю/Ju/g;
113
114     # Lowercase file extensions
115     if ($dst =~ /^(.*)\.([^\.]+)$/) {
116         $dst = $1 . "." . lc $2;
117     };
118
119     # Change looking of track numbers
120     if ($dst =~ /^(\d+)[-.]_*(.+)$/) {
121         $dst = "$1.$2";
122     };
123
124     next if ($src_filename eq $dst);
125     print "$src -> $dst\n";
126     die "\"$dst\" exists" if -e $dst;
127     rename $src_filename, $dst;
128 };
129 closedir DIR;