Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active November 23, 2020 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/68440616243b8449fa946522898a9683 to your computer and use it in GitHub Desktop.
Save Xliff/68440616243b8449fa946522898a9683 to your computer and use it in GitHub Desktop.
Add Track Numbers to your .mp3 Files

Here's a quick and dirty script you can use to prefix your .mp3 files with their track numbers. If you've ever dealt with Tidal, you should find this very useful:

#!/usr/bin/env raku

use Audio::Taglib::Simple;
use File::Find;

sub MAIN ($dir = '.') {

  for find(:$dir, name => / :i '.mp3' $/) {
    if .basename ~~ / ^ \d+ / {
      say "Skipping '{ .basename }'";
    } else {
      my $t = Audio::Taglib::Simple.new($_);
      my $newname = "{ $t.track.fmt('%02d') }-{ .basename }";
      .basename.IO.rename($newname);
      say "Renamed '{ .basename }' to { $newname }";
    }
  }
  
}

You should zef install Audio::Taglib::Simple File::Find before you attempt to run this script.

Have comments, please leave them on this gist. Have improvements? Also leave them here.

UPDATE: Incorporates @lizmat++'s suggestion in the comments

@lizmat
Copy link

lizmat commented Nov 23, 2020

Why not put this in a MAIN sub with a parameter to indicate the directory to look into? With a default to "." ?

@Xliff
Copy link
Author

Xliff commented Nov 23, 2020

That's not a bad idea at all! - Updated post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment