Command line FileBot and tagging

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
UKenGB
Posts: 49
Joined: 04 Jan 2017, 09:26

Command line FileBot and tagging

Post by UKenGB »

Still working on several actual format strings to do what I want with the various media types and then I'll have to set up the command line version to use those in a single script to run. Is there any documentation about using that? I have installed it all, but no man page or anything. I could do with a basic reference to all that is possible. E.g. like with many other file processing 'programs', it is possible to either create a command line with all the required options, or put everything in a separate file and the command line then just specifies the file to use. It seems to me that Filebot has some sort of facility to do this, but how?

I have another problem with iTunes (whose files I am processing). I need to use the Category tag which iTunes can read from the file, display, and use in Smart Playlists, but it would appear, not edit directly. However, it is available via AppleScript, but adding/editing that tag in a script just changes iTunes database and is not written to the file. :x So...

Can anyone suggest a command line utility to write tag data to a file? I just need to write a value to that one tag (Category). If so it would save me the time of having to search on-line for a solution.
User avatar
rednoah
The Source
Posts: 22984
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Command line FileBot and tagging

Post by rednoah »

1.
filebot -help should get you started.

You will find simple example here:
http://www.filebot.net/cli.html

The FileBot CLI is extremely flexible and many many things are possible. What exactly are you trying to do?

Code: Select all

filebot -rename ...

2.
You can pass in arguments via @files which is useful if you want to avoid overly complex command-line calls:
viewtopic.php?f=3&t=3244


3.
Not sure about tagging. After files have been processed and xattr tagged, then all the information you would need for your own iTunes tagging command would be readily available.

e.g.

Code: Select all

filebot -mediainfo /files "{f} => {xattr}"
:idea: Please read the FAQ and How to Request Help.
UKenGB
Posts: 49
Joined: 04 Jan 2017, 09:26

Re: Command line FileBot and tagging

Post by UKenGB »

That link is helpful, thanks. I hadn't found that before. I'll have a good loot at it.

After much searching I've found that mp4v2, with it's mp4tags utility will write tags (including [category]) into an existing file. Perfect. Except iTunes shouldn't be treating this tag as read only in the first place. What nonsense.
UKenGB
Posts: 49
Joined: 04 Jan 2017, 09:26

Re: Command line FileBot and tagging

Post by UKenGB »

Hard to know the best place to ask this, but it is concerned with what I want to do with the CLI, so here goes, here...

When creating my symlinks with FileBot CLI, I don't want to re-create any that already exist and instead just skip those, not changing any modification/access dates or times. So:-
  • How can I check for the existence of a file at a known path?

    Can I define a variable without it actually 'echoing' the result, but simply use it later in the format string?

    How can I NOT write a file? Just return ""?
I'll keep looking (mainly in the examples thread probably), but helpful suggestions would save time and be appreciated.
User avatar
rednoah
The Source
Posts: 22984
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Command line FileBot and tagging

Post by rednoah »

1.
--conflict skip is default, so if the destination path already exists, then it'll just skip it, and not touch anything.


2.
FileBot will take the result of the Groovy expression. It's not "echo" in the bash sense.

e.g. these expressions all yield the same value:

Code: Select all

{2} {1+1} {return 2} {def x = "Hello"; 2}

3.
If you don't want to process certain files then they shouldn't have been passed into the filebot call in the first place. There's many ways to do this. Depends on what exactly you're trying to do.

If you're using --db xattr -non-strict (i.e. plain file mode) then --filter can be used to include or exclude files.
:idea: Please read the FAQ and How to Request Help.
UKenGB
Posts: 49
Joined: 04 Jan 2017, 09:26

Re: Command line FileBot and tagging

Post by UKenGB »

Aha, so if it will simply skip any already existing files (symlinks in my case), I don't need to worry about filtering. It can process every file in the folder to which I point it. Then I just need to delete any orphaned symlinks and everything will be completely in sync.

Regarding {def x = "Hello"; 2} result = 2, what would {def x = "Hello"} on its own yield? I seem to see the value of the assignment, so {def x = "Hello"} - {x] would result in Hello - Hello. I was trying to define a variable with a value and then use that in 2 places in the format string. But I would see it 3 times. So I used the first definition/assignment as the first time I need to use it and then used the variable for the second. It seemed that I was not able to silently define and set the variable for later use.

Is '--db xattr -non-strict' the same as selecting 'Plain file' as a preset source, no on-line matching? What would '--db ID3' do for video files? Seems like there ought to be a '--db mediainfo' option meaning only fetch embedded tags, whatever the file type and access them with media.xxxx. Or is that exactly the same as '--db xattr.....'?
User avatar
rednoah
The Source
Posts: 22984
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Command line FileBot and tagging

Post by rednoah »

1.
UKenGB wrote:I don't need to worry about filtering.
Depends on what you're doing. If it's all offline (i.e. --db xattr) then you can do whatever you want. But if you use TheTVDB / TheMovieDB or any of the other online sources then hitting them with the same requests over and over and over will get you banned.


2.
Assignment expressions yield the assignment value of course. Variables are limited to the expression/scope they're defined in.

e.g. the first expression yields 1 and the second throws an exception because x is undefined in that expression/scope:

Code: Select all

{def x=1} {x}
If you want to use variables, then they all need to be used in the same scope:

Code: Select all

{def x=1; def y=2; x+y}

3.
--db ID3 is Music Mode but instead of search online it'll just create Music objects based on the available MediaInfo and ignore files where MediaInfo doesn't yield enough information. There is no "ID3" equivalent for Movie Mode and Episode Mode.


4.
--db xattr matches files with known metadata (e.g. Episode, Movie, etc) previously stored by FileBot as xattr metadata. Files without xattr metadata will be ignored, unless -non-strict in which case each file is matched to itself.

So Plain File mode is equivalent to --db xattr -non-strict IF AND ONLY IF none of the files are xattr tagged. Bindings like {n} have a different meaning depending on what kind of object (Episode/Movie/File) is being formatted, while bindings like {fn} or {media} are the same regardless of info object because they're based on the file.

Viewing the xattr metadata may help you understand what's going on:
viewtopic.php?f=4&t=5#p5394
:idea: Please read the FAQ and How to Request Help.
UKenGB
Posts: 49
Joined: 04 Jan 2017, 09:26

Re: Command line FileBot and tagging

Post by UKenGB »

xattrs are irrelevant in my context. I am working on files that have already been FULLY tagged In iTunes. All I need to do is create suitably named symlinks that Plex can use. All the info possibly required for the filenames and folders are already in the file tags. So all I need FileBot to do is read the tags and create the symlink with the formatted name as specified in the format string. For this process I want to ensure FileBot never looks up anything. Not needed and a waste of time and xattr are not used at all.

So I really just need the most suitable options to tell FileBot to read the tags and not look up anything.

If there are problems and Plex cannot find something, I do my own checks and adjust in iTunes, then run my filebot commands again for the next sync. Otherwise I've got too many automated lookups being done and I won't know what's what.
User avatar
rednoah
The Source
Posts: 22984
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Command line FileBot and tagging

Post by rednoah »

Alright, that's a clear use case.

e.g.

Code: Select all

filebot -rename "/path/to/iTunes/TV Shows" --db xattr -non-strict --output "/path/to/symlinks" --format "TV Shows/{media.Album}/{media.Album} - {media.PartID} - {media.Title}" --action symlink
I'd do a dry-run with --action test first just to see what happens.
:idea: Please read the FAQ and How to Request Help.
UKenGB
Posts: 49
Joined: 04 Jan 2017, 09:26

Re: Command line FileBot and tagging

Post by UKenGB »

Running stuff like this can take a long time with hundreds of files to process, most of which will be unchanged from the last time. But FileBot has to do its renaming process on every file to decide whether or not it already exists, so updating 2 files out of hundreds takes many minutes instead of a few seconds.

However, I could write a script to establish which files need processing and pass these to filebot. But can filebot accept these on stdin?

Alternatively, can I write reasonably complex filtering conditions for filebot? These would have to be based on file and symlink checking, not just some textual comparisons of names.

I'll have another look at filters, but would this be possible, or does it accept a list of files to process on stdin?
User avatar
rednoah
The Source
Posts: 22984
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Command line FileBot and tagging

Post by rednoah »

If you run the script every day, then you can just ignore older files with --filter or pass in only new files via command-line arguments via find -exec.

e.g. ignore files older than 1 day:

Code: Select all

--filter "age < 2"
find -exec is even faster of course, because FileBot won't even be called if there's no new files. ;)


You will find Option 3 and Option 4 in How to process only specific kinds of files? inspirational for both GUI and CLI use cases.
:idea: Please read the FAQ and How to Request Help.
UKenGB
Posts: 49
Joined: 04 Jan 2017, 09:26

Re: Command line FileBot and tagging

Post by UKenGB »

Specifically, I need to establish which files in a large folder do not have renamed symlinks in any one of up to half a dozen other folders. I can easily do this with a script, but how can I then pass that list of files to filebot? That's why I asked if it makes use of unix stdin as that makes passing information from process to process (as I need to do here) very easy. But does filebot abide by these conventions and accept a list of files via stdin?

File and -exec can be part of the process to create the list, but are you suggesting to -exec filebot {} (as appropriate)? So that would run a new instance of filebot on every file, which could still be hundreds. Not often and usually just a very few, but I have to allow for all eventualities and running a find -exec filebot like that would likely take longer than just running filebot on every file.

Stdin would the normal practice for this sort of thing. Failing that, could I tell it to read a list of files from a text file?
User avatar
rednoah
The Source
Posts: 22984
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Command line FileBot and tagging

Post by rednoah »

I have never seen any command-line tool accept file paths line by line via stdin. Passing multiple file arguments is the obvious and only proper way to do this kind of stuff (i.e. that's how touch, cat, rm and all it's friends work).

manpage wrote:-exec command {} +
This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of '{}' is allowed within the command. The command is executed in the starting directory.
@see https://linux.die.net/man/1/find


e.g. one call, many file arguments:

Code: Select all

find . -iname "*.mp4" -exec filebot -script fn:sysenv {} + | grep args

Code: Select all

args[0] = -script
args[1] = fn:sysenv
args[2] = ./1.mp4
args[3] = ./2.mp4
args[4] = ./3.mp4
args[5] = ./4.mp4
args[6] = ./5.mp4

e.g. many calls, one for each file:

Code: Select all

find . -iname "*.mp4" -exec filebot -script fn:sysenv {} \; | grep args

Code: Select all

args[0] = -script
args[1] = fn:sysenv
args[2] = ./1.mp4
args[0] = -script
args[1] = fn:sysenv
args[2] = ./2.mp4
args[0] = -script
args[1] = fn:sysenv
args[2] = ./3.mp4
args[0] = -script
args[1] = fn:sysenv
args[2] = ./4.mp4
args[0] = -script
args[1] = fn:sysenv
args[2] = ./5.mp4
:idea: Please read the FAQ and How to Request Help.
UKenGB
Posts: 49
Joined: 04 Jan 2017, 09:26

Re: Command line FileBot and tagging

Post by UKenGB »

I hadn't seen anywhere that indicated filebot could accept multiple file arguments so that kinda changes things.

Most unix utilities can be passed input using '|', but this is what I'm understanding is not possible with filebot.

My file selection is not just a simple find command, so using {} + is no good. But I should able to use xargs instead, unless you know of any reason why that wouldn't work?
User avatar
rednoah
The Source
Posts: 22984
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Command line FileBot and tagging

Post by rednoah »

xargs is what allows you to take standard output and pass it on as arguments, so it's the generic alternative to find -exec.

Make sure to read the manpage for the -d and -n options carefully though.
:idea: Please read the FAQ and How to Request Help.
Post Reply