Struggling with renaming Audiobook files in Audiobookshelf format

Any questions? Need some help?
Post Reply
SeñorWill
Posts: 5
Joined: 27 Sep 2024, 17:15

Struggling with renaming Audiobook files in Audiobookshelf format

Post by SeñorWill »

Audiobookshelf has their supported directory structure listed here:
https://www.audiobookshelf.org/docs/#bo ... -structure

I used the FileBot article here to begin building something that align to their standards:
viewtopic.php?t=14137

I've went through a ton of scripts and attempts, but this is what generally works:

Format: Select all

{ drive }/Audiobooks/
{
    def query = fn.space(' ').removeAll(/^\d+ - | - \d+$| \d+$|-Part\d+$/)
    def url = 'https://www.audible.com/search'.toURL(keywords: query, ipRedirectOverride: true, overrideBaseCountry: true)
    def ul = html(url).select('li.authorLabel').first().parent()

    def title = ul.select('h3 a').text()
    def author = ul.select('li.authorLabel a').text()
    def narrator = ul.select('li.narratorLabel a').text()
    
    def series = ul.select('li.seriesLabel a').text()
    def book = any { ul.select('li.seriesLabel').text().tokenize().last() } { null }

    def trackNumber = String.format('%02d', i )

    // Define file extensions
    def nonAudioExtensions = ['jpg', 'jpeg', 'nfo']
    def audioExtensions = ['mp3', 'm4b', 'flac']

    if (nonAudioExtensions.contains(ext.toLowerCase())) {
        // Copy non-audio files without renaming
        if (series) {
            return "$author/$series/Book $book - $title {$narrator}/Book $book $title {$narrator}"    
        } else {
            return "$author/$title {$narrator}/${fn}"    
        }
    } else {
        // Default behavior for audio files
        if (series) {
            return "$author/$series/Book $book - $title {$narrator}/Book $book - $title {$narrator} ${trackNumber}.${ext}"    
        } else {
            return "$author/$title {$narrator}/$title {$narrator} ${trackNumber}.${ext}"    
        }
    }
}
Everything seems to work perfectly with this except for one thing: it always starts the numbering of audio files at 03. I think this is because there's 1 NFO file and 1JPG file and so the audio file is 3rd and so it gets 03 (even though the non-audio files aren't numbered at all):

Screenshot

I can't figure out how to either 1) have the audio files begin numbering at 01, or 2) have filebot retain the file numbers that exist (although not all audio books will have a number in the same format cleanly at the end).

The only way I've been able to "fix" this is to exclude anything other than audiofiles. Then it will start the numbering at 01.

Any idea how I can fix this? I embarrassed to admit how long I've tried on this today.
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Struggling with renaming Audiobook files in Audiobookshelf format

Post by rednoah »

{i} is the row number in New Names list for the item at hand, i.e. {i} is 3 for the 3rd row in the list. Your code uses {i} for audio files only:

Groovy: Select all

def trackNumber = String.format('%02d', i )

:?: You may want:
(1) the number of the file in the list (counting only files of the same extension)
(2) the number of the file in its parent folder (counting only files of the same extension)
(3) the number of the file as per the current file name


:arrow: Looking at the screenshot above, you can just match the number from the original file name, so (3) is the easiest solution:

Format: Select all

{ fn.match(/[0-9]+$/) }
:idea: Please read the FAQ and How to Request Help.
SeñorWill
Posts: 5
Joined: 27 Sep 2024, 17:15

Re: Struggling with renaming Audiobook files in Audiobookshelf format

Post by SeñorWill »

Is this how'd you use that (as an example), or would it go some where else in the script?

Format: Select all

return "$author/$series/Book $book - $title {$narrator}/Book $book - $title {$narrator} { fn.match(/[0-9]+$/) }.${ext}"
Would this logic also work when audio books are just a single audio file without a number at the end? Would it just rename the file and not try to add a number?

I don't know if it's possible to have that last piece of logic, but I feel like that's really all I need for this.

Thanks for the help! How do you leave a tip?
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Struggling with renaming Audiobook files in Audiobookshelf format

Post by rednoah »

{ fn.match(/[0-9]+$/) } only works for files that have a number at the end of the file name, that's what pattern /[0-9]+$/ matches. Otherwise, the { fn.match(/[0-9]+$/) } block simply fails and does not add anything to the target file path.


:arrow: You'd add the {...} at the very end of your format, since you want the number at the end of your file name:

Format: Select all

{
	...
}
{
	...
}
{
	fn.match(/[0-9]+$/)
}

:!: Note that each top-level {...} independently generates a bit of the target file path. You can move top-level {...} around as you see fit. The code you posted is notably incorrect because you pasted the { fn.match(/[0-9]+$/) } block into the middle of another top-level {...} block.
:idea: Please read the FAQ and How to Request Help.
Post Reply