Conditional command depending on file type

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
cuneglas
Posts: 11
Joined: 10 Jun 2018, 05:54

Conditional command depending on file type

Post by cuneglas »

I had this command to process MP3/M4A

Code: Select all

filebot -rename "/input/" -r --output "/output/" --format "{albumArtist0 = albumArtist.sortName().charAt(0); albumArtist0.isDigit() ? '0-9' : albumArtist0}/{music.albumArtist.upperInitial().slash(', ')}/{album.upperInitial().colon(' -')} ({y})/{media.PartPosition!=null ? media.PartPosition+'.' : ''}{pi.pad(2)!=null ? pi.pad(2)+'.' : ''} {t.upperInitial().colon(' -')}" --db ID3
but then I started using FLAC files and had to change the command to keep the same formatting.

Code: Select all

filebot -rename "/input/" -r --output "/output/" --format "{albumArtist0 = albumArtist.sortName().charAt(0); albumArtist0.isDigit() ? '0-9' : albumArtist0}/{music.albumArtist.upperInitial().slash(', ')}/{album.upperInitial().colon(' -')} ({y})/{media.Part!=null ? media.Part+'.' : ''}{pi.pad(2)!=null ? pi.pad(2)+'.' : ''} {t.upperInitial().colon(' -')}" --db ID3
The change was media.PartPosition to media.Part so it took into account the disk number

To filter the files I added

Code: Select all

--file-filter "f =~ /mp3|m4a/"
and

Code: Select all

--file-filter "f =~ /flac/"
But I have to run the command twice for each file type, how can I put everything together in one command?

And by the way, if one of the mp3 or m4a files has a bitrate lower than 320kbps how do I include [000] at the end of the filename?

Thanks for the help
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Conditional command depending on file type

Post by rednoah »

You can make your command smart and generate different paths for different files based on different code paths:

e.g. take media.Part, and if undefined take media.PartPosition, and if still undefined return null, which in turn makes the subsequent String.concat() method call fail the expression:

Code: Select all

{ 
	def part = any{ media.Part }{ media.PartPosition }
	part.concat('.')
}
:idea: Presumably, MediaInfo Inspector shows either media.Part or media.PartPosition set to some value, but never both at the same time.


e.g. print some value in response to some condition:

Code: Select all

{ 
	kbps < 320 ? '[000]' : null
}

:idea: In the future, please paste the raw MediaInfo table for your sample files, one for each use case, otherwise we'll be coding blindly:
Image
:idea: Please read the FAQ and How to Request Help.
cuneglas
Posts: 11
Joined: 10 Jun 2018, 05:54

Re: Conditional command depending on file type

Post by cuneglas »

Thanks for you prompt reply.

MP3 files with media.PartPosition have a numeric value relative to the disc number. In the case of FLAC files the disk number is given by the Part parameter. If you run the command made for FLAC files without the --file-filter on MP3 files the filename gets only the track number without the disc number, the same thing happens the other way around.

As you said, no file has both parameters at the same time. In fact, each of the parameters is unique to the file type, at least I have not found one in which this was not the case.

MP3
Image

Flac
Image

Basically the goal is to end up with #disc.#track. track name. Some MP3 files have the field empty, in that case the filename becomes #track. track name.

What you said to do with def part and the kbps < 320, how would the final command look like? Usually the commands I use in FileBot are composed of various answers you gave in the forums. my know how doesn't reach that much.

Thanks a lot for the help and your work.
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Conditional command depending on file type

Post by rednoah »

You can just copy & paste things together. Each {...} expression can be used independently. Only you can know where you want which piece of information. ;)

e.g.

Code: Select all

{albumArtist0 = albumArtist.sortName().charAt(0); albumArtist0.isDigit() ? '0-9' : albumArtist0}/
{music.albumArtist.upperInitial().slash(', ')}/
{album.upperInitial().colon(' -')} ({y})/
{ 
	def part = any{ media.Part }{ media.PartPosition }
	part.concat('.')
}
 {t.upperInitial().colon(' -')}
{ 
	kbps < 320 ? '[000]' : null
}



:idea: You can make your life easier by maintaining your custom format in an external *.groovy text file:
rednoah wrote: 07 Dec 2015, 07:17 Option Value:

The --format, --mapper, --filter and --file-filter options accept Groovy expressions literally, or as *.groovy UTF-8 encoded plain text files:

e.g. --format *.groovy (UTF-8 encoded plain text file)

Code: Select all

--format /path/to/MovieFormat.groovy
:idea: Please read the FAQ and How to Request Help.
cuneglas
Posts: 11
Joined: 10 Jun 2018, 05:54

Re: Conditional command depending on file type

Post by cuneglas »

It work like a charm, I just had to add the missing {pi.pad(2)+'.'}.

Thank you very much for your help

Thanks for the tip about external *.groovy text file.
Post Reply