Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

345 řádky
9.8KB

  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Getopt::Long;
  5. use File::Find;
  6. use Data::Dumper;
  7. use File::Basename;
  8. my $opt_no_genre;
  9. my $opt_comment;
  10. my $opt_catid;
  11. my $opt_rg;
  12. # TODO fill this out
  13. my %genreMap = (
  14. edm => 52,
  15. soundtrack => 24,
  16. );
  17. # this is a godsent page
  18. # https://wiki.hydrogenaud.io/index.php?title=Tag_Mapping
  19. # https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html
  20. # a lot of this may not work
  21. # TODO escape potential 's
  22. my %idLookup = (
  23. album => 'TALB',
  24. albumsort => 'TSOA',
  25. discsubtitle => 'TSST',
  26. grouping => 'TIT1',
  27. title => 'TIT2',
  28. titlesort => 'TSOT',
  29. subtitle => 'TIT3',
  30. subtitle => 'TIT3',
  31. albumartist => 'TPE2',
  32. albumartistsort => 'TSO2', # Maybe?
  33. artist => 'TPE1',
  34. artistsort => 'TSOP',
  35. arranger => 'TIPL=arranger',
  36. author => 'TEXT',
  37. composer => 'TCOM',
  38. conductor => 'TPE3',
  39. engineer => 'TIPL=engineer',
  40. djmixer => 'TIPL=DJ-mix',
  41. mixer => 'TIPL=mix',
  42. #performer => 'TMCL', # This produces some really weird tags
  43. producer => 'TIPL=producer',
  44. publisher => 'TPUB',
  45. organization => 'TPUB',
  46. label => 'TPUB',
  47. remixer => 'TPE4',
  48. discnumber => ['TPOS', sub {
  49. my $t = shift;
  50. my $totalkey = exists($t->{disctotal}) ? 'disctotal' : 'totaldiscs';
  51. return "$t->{discnumber}" if !exists($t->{$totalkey});
  52. return "$t->{discnumber}/$t->{$totalkey}";
  53. }],
  54. totaldiscs => undef,
  55. disctotal => undef,
  56. tracknumber => ['TRCK', sub {
  57. my $t = shift;
  58. my $totalkey = exists($t->{tracktotal}) ? 'tracktotal' : 'totaltracks';
  59. return "$t->{tracknumber}" if !exists($t->{$totalkey});
  60. return "$t->{tracknumber}/$t->{$totalkey}";
  61. }],
  62. totaltracks => undef,
  63. tracktotal => undef,
  64. #date => 'TDRC', # This is for id3v2.4
  65. #date => 'TYER',
  66. date => [undef, sub {
  67. my $t = shift;
  68. my $date = $t->{date};
  69. if (length($date) == 4) { # Only year
  70. return "TYER=$date";
  71. }
  72. if (!($date =~ m/^\d{4}\.\d{2}\.\d{2}$/)) {
  73. print("Date format unknown: $date\n");
  74. exit 1;
  75. }
  76. $date =~ s/\./-/g;
  77. return "TDRL=$date"; # Release date
  78. }],
  79. originaldate => 'TDOR', # Also for 2.4 only
  80. 'release date' => 'TDOR', # Also for 2.4 only
  81. isrc => 'TSRC',
  82. barcode => 'TXXX=BARCODE',
  83. catalog => ['TXXX=CATALOGNUMBER', sub { return tagmap_catalogid(shift, 'catalog'); } ],
  84. catalognumber => ['TXXX=CATALOGNUMBER', sub { return tagmap_catalogid(shift, 'catalognumber'); } ],
  85. catalogid => ['TXXX=CATALOGNUMBER', sub { return tagmap_catalogid(shift, 'catalogid'); } ],
  86. labelno => ['TXXX=CATALOGNUMBER', sub { return tagmap_catalogid(shift, 'labelno'); } ],
  87. 'encoded-by' => 'TENC',
  88. encoder => 'TSSE',
  89. encoding => 'TSSE',
  90. 'encoder settings' => 'TSSE',
  91. media => 'TMED',
  92. genre => ['TCON', sub {
  93. return undef if ($opt_no_genre);
  94. my $genreName = shift->{genre};
  95. if (!exists($genreMap{lc($genreName)})) {
  96. # If no genre number exists, use the name
  97. return $genreName;
  98. }
  99. return $genreMap{$genreName};
  100. }],
  101. #mood => ['TMOO', sub {
  102. #}],
  103. bpm => 'TBPM',
  104. comment => ['COMM=Comment', sub {
  105. return undef if (defined($opt_comment) && $opt_comment eq "");
  106. return shift->{comment};
  107. }],
  108. copyright => 'TCOP',
  109. language => 'TLAN',
  110. #replaygain_album_peak => 'TXXX=REPLAYGAIN_ALBUM_PEAK',
  111. #replaygain_album_gain => 'TXXX=REPLAYGAIN_ALBUM_GAIN',
  112. replaygain_track_gain => sub {
  113. return undef if (!$opt_rg);
  114. shift->{replaygain_track_gain} =~ /^(-?\d+\.\d+) dB$/;
  115. my $gain_db = $1;
  116. exit(1) if ($gain_db eq "");
  117. return "--replaygain-accurate --gain $gain_db";
  118. },
  119. #replaygain_album_gain => 'TXXX=REPLAYGAIN_ALBUM_GAIN',
  120. #replaygain_album_peak => 'TXXX=REPLAYGAIN_ALBUM_PEAK',
  121. #replaygain_track_gain => 'TXXX=REPLAYGAIN_TRACK_GAIN',
  122. #replaygain_track_peak => 'TXXX=REPLAYGAIN_TRACK_PEAK',
  123. script => 'TXXX=SCRIPT',
  124. lyrics => 'USLT',
  125. circle => 'TXXX=CIRCLE',
  126. event => 'TXXX=EVENT',
  127. discid => 'TXXX=DISCID',
  128. originaltitle => 'TXXX=ORIGINALTITLE',
  129. );
  130. sub tagmap_catalogid {
  131. my $t = shift;
  132. my $own_tag_name = shift;
  133. return undef if (defined($opt_catid) && $opt_catid eq "");
  134. return $t->{$own_tag_name};
  135. }
  136. my $opt_genre;
  137. my $opt_help;
  138. my @opt_tagreplace;
  139. my $opt_cbr = 0;
  140. GetOptions(
  141. "genre|g=s" => \$opt_genre,
  142. "no-genre|G" => \$opt_no_genre,
  143. "replay-gain|r" => \$opt_rg,
  144. "help|h" => \$opt_help,
  145. "catid=s" => \$opt_catid,
  146. "comment=s" => \$opt_comment,
  147. "tagreplace|t=s" => \@opt_tagreplace,
  148. "320|3" => \$opt_cbr,
  149. ) or die("Error in command line option");
  150. if ($opt_help) {
  151. help();
  152. }
  153. if (scalar(@ARGV) != 2) {
  154. print("Bad arguments\n");
  155. usage();
  156. }
  157. my ($IDIR, $ODIR) = @ARGV;
  158. if (!-e $ODIR) {
  159. mkdir $ODIR;
  160. }
  161. find({ wanted => \&iterFlac, no_chdir => 1 }, $IDIR);
  162. sub iterFlac {
  163. # Return if file is not a file, or if it's not a flac
  164. return if (!-f || !/\.flac$/);
  165. my @required_tags = ("artist", "title", "album", "tracknumber");
  166. my $flacDir = substr($File::Find::name, length($IDIR));
  167. my $flac = $_;
  168. my $flac_o = $flac;
  169. shellsan(\$flac);
  170. my $dest = "$ODIR/" . $flacDir;
  171. #print("DEBUG: $dest\n");
  172. $dest =~ s/\.flac$/\.mp3/;
  173. my $tags = getFlacTags($flac);
  174. my $has_req_tags = 1;
  175. foreach (@required_tags) {
  176. if (!exists($tags->{lc($_)})) {
  177. $has_req_tags = 0;
  178. last;
  179. }
  180. }
  181. if (!$has_req_tags) {
  182. print("WARNING: File: '$flac' does not have all the required tags. Skipping\n");
  183. exit(1);
  184. return;
  185. }
  186. argsToTags($tags, $flac_o);
  187. my $tagopts = tagsToOpts($tags);
  188. #print("Debug: @$tagopts\n");
  189. shellsan(\$dest);
  190. my $cmd;
  191. if ($opt_cbr) {
  192. $cmd = "flac -cd -- '$flac' | lame -S -b 320 -q 0 --add-id3v2 @$tagopts - '$dest'";
  193. } else {
  194. $cmd = "flac -cd -- '$flac' | lame -S -V0 --vbr-new -q 0 --add-id3v2 @$tagopts - '$dest'";
  195. }
  196. #print("Debug - CMD: [$cmd]\n");
  197. qx($cmd);
  198. if ($? != 0) {
  199. exit(1);
  200. }
  201. }
  202. sub argsToTags {
  203. my $argTags = shift;
  204. my $fname = shift;
  205. $fname =~ s!^.*/!!;
  206. if (defined($opt_genre)) {
  207. $argTags->{genre} = $opt_genre;
  208. }
  209. if (defined($opt_comment) && $opt_comment ne "") {
  210. $argTags->{comment} = $opt_comment;
  211. }
  212. if (defined($opt_catid) && $opt_catid ne "") {
  213. $argTags->{catalognumber} = $opt_catid;
  214. }
  215. if (scalar @opt_tagreplace > 0) {
  216. foreach my $trepl (@opt_tagreplace) {
  217. $trepl =~ m!(.*?)/(.*?)=(.*)!;
  218. my ($freg, $tag, $tagval) = ($1, $2, $3);
  219. if ($fname =~ m!$freg!) {
  220. $argTags->{lc($tag)} = $tagval;
  221. }
  222. }
  223. }
  224. }
  225. sub tagsToOpts {
  226. my $tags = shift;
  227. my @tagopts;
  228. # TODO escape ' and =?
  229. foreach my $currKey (keys (%$tags)) {
  230. if (!exists($idLookup{$currKey})) {
  231. print("Tag: '$currKey' doesn't have a mapping, skipping\n");
  232. next;
  233. }
  234. my $tagName = $idLookup{$currKey};
  235. my $type = ref($tagName);
  236. if ($type eq "" && defined($tagName)) {
  237. # If tag name is defined and tag contents exists
  238. my $tagCont = $tags->{$currKey};
  239. shellsan(\$tagCont);
  240. push(@tagopts, qq(--tv '$tagName=$tagCont'));
  241. } elsif ($type eq "ARRAY") {
  242. my $tagCont = $tagName->[1]->($tags);
  243. my $tagKey = $tagName->[0];
  244. if (defined($tagCont)) {
  245. if (defined($tagKey)) {
  246. shellsan(\$tagCont);
  247. push(@tagopts, qq(--tv '$tagName->[0]=$tagCont'));
  248. } else {
  249. if (ref($tagCont) eq 'ARRAY') {
  250. # If we have an array of tags
  251. foreach my $tC (@$tagCont) {
  252. shellsan(\$tC);
  253. push(@tagopts, qq(--tv '$tC'));
  254. }
  255. } else {
  256. # If we have only one
  257. shellsan(\$tagCont);
  258. push(@tagopts, qq(--tv '$tagCont'));
  259. }
  260. }
  261. }
  262. } elsif ($type eq 'CODE') {
  263. # If we have just a code reference
  264. # do not assume, that this is a tag, rather a general cmd opt
  265. my $opt = $tagName->($tags);
  266. if (defined($opt)) {
  267. shellsan(\$opt);
  268. push(@tagopts, qq($opt));
  269. }
  270. }
  271. }
  272. return \@tagopts;
  273. }
  274. sub getFlacTags {
  275. my $flac = shift;
  276. my %tags;
  277. my @tagtxt = qx(metaflac --list --block-type=VORBIS_COMMENT -- '$flac');
  278. if ($? != 0) {
  279. exit(1);
  280. }
  281. foreach my $tagline (@tagtxt) {
  282. if ($tagline =~ /comment\[\d+\]:\s(.*?)=(.*)/) {
  283. if ($2 eq '') {
  284. print("Empty tag: $1\n");
  285. next;
  286. }
  287. $tags{lc($1)} = $2;
  288. }
  289. }
  290. return \%tags;
  291. }
  292. sub shellsan {
  293. ${$_[0]} =~ s/'/'\\''/g;
  294. }
  295. sub usage {
  296. print("Usage: flac2mp3.pl [-h | --help] [-r] [-3] [-g | --genre NUM] <input_dir> <output_dir>\n");
  297. exit 1;
  298. }
  299. sub help {
  300. my $h = <<EOF;
  301. Usage:
  302. flac2mp3.pl [options] <input_dir> <output_dir>
  303. -h, --help print this help text
  304. -g, --genre NUM force this genre as a tag (lame --genre-list)
  305. -G, --no-genre ignore genre in flac file
  306. -r, --replay-gain use replay gain values
  307. --catid STRING the catalog id to set (or "")
  308. --comment STRING the comment to set (or "")
  309. -t --tagreplace STR Replace flac tags for a specific file only
  310. Like -t '02*flac/TITLE=Some other title'
  311. -3, --320 Convert into CBR 320 instead into the default V0
  312. EOF
  313. print($h);
  314. exit 0;
  315. }
  316. # vim: ts=4 sw=4 et sta