sandreas / m4b-tool

m4b-tool is a command line utility to merge, split and chapterize audiobook files such as mp3, ogg, flac, m4a or m4b
MIT License
1.18k stars 76 forks source link

Using --filename-template with --batch-pattern does not skip existing files #238

Open Drudoo opened 1 year ago

Drudoo commented 1 year ago

I've been using --batch-pattern for a long time, when converting my files. It is great as i can add new files to my input folder and if it has already been converted to the output folder, it will just be skipped.

I've had issues with folder structure and series naming and when using Audiobookshelf and therefore I've started to use --filename-template, which is amazing.

My current command looks like:

php m4b-tool.phar merge -vvv -n --jobs=4 --output-file="Plex/" --batch-pattern="input/%a/%s/%p - %n/" --batch-pattern="input/%a/%n/" --filename-template="{{outputPath}}{{\"%02d\"|format(seriesPart)}} - {{title}}/{{artist}} - [{{series}} - {{\"%02d\"|format(seriesPart)}}] - {{title}}" --equate=name,album "input/"

This is mainly a test to make sure books in a series are tagged correctly.

But it seems like m4b-tools first check if the output file (using standard batch-pattern) filename exists, then convert and moves the file using the filename-template. It should be checking if the file exists using the filename-template?

A side question, is it possible to have a single filename-template that doesn't output parts of the name if it doesn't exists?

Eg. if the book doesn't have a series, don't create the series folder ({{seriesPart}} - {{title}}/)? Some softwares you can do stuff like {{seriesPart - title/}}.

sandreas commented 1 year ago

I've had issues with folder structure and series naming and when using Audiobookshelf and therefore I've started to use --filename-template, which is amazing.

Ha, I did not know that this is working and never even had the idea of doing that, you're a genius... Recently I reorganized my whole Filesystem structure just to make it fit for audiobookshelf and never thought of just reencoding my lib.

So, obviously the use of --filename-template was never intended, this is why it does not work correctly. I remember, that I recently fixed something to make it work, but never thought about this possibility.

A side question, is it possible to have a single filename-template that doesn't output parts of the name if it doesn't exists?

You should be able to use the whole world of twig templates, that means:

{% if series %}{{seriesPart}} - {{title}}{% else %}{{title}}{% endif %}

It is kind of fiddly and I never tested this, but it should work.

Back to the main question: Ok, I know, what has to be done here. Since I'm busy as hell atm, I'll write some things down to remember how to fix that and I hope I can get to this possibility in the near future:

The core of this problem is, that the Tags already have to exist in order to rename the file via template, but the output file have to exist to determine the tags. So this is a bit of a chicken or the egg causality.

What I could do: Determine the tags, that are possible before having an output file (read from metadata files, input arguments, etc.) and build the filename via template before calling shouldSkip (see below). Then apply the rest of the improvers, that only can be loaded on an existing output file (silence detector, chapter improvers). That would probably work in MOST cases... I'll have to investigate this, but this is not an easy Task.