Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@lizmat 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

This comment has been minimized.

Copy link
Owner Author

@Xliff 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