File Renaming

Questions and answers on how to get the most out of FFAStrans
tmhlegolas
Posts: 8
Joined: Tue Jul 09, 2019 12:46 am

File Renaming

Post by tmhlegolas »

I'm certain this is a dumb question but I've been struggling with it.

I have a workflow that will take various formats in and create different encodes based on them.

I'd like to replace some values in the original filename for the resulting encodes but am not sure the best way to to go about it.
The input names have values like:
FilmsMan_TestingNames_203_RUS_f2398_UHD_239.mov
NewPalPost_HistoryOfTheShowName_ENG_f25_UHD_1237.mov
OldPalShows_OurVideoShow_ep12_f25_SD_404_1986.mov

The output filenames will need to be changed according to the encodes performed_(f\d+)_ to f2997 for example. _(UHD)_ to HD etc. This wouldn't be so bad with append and using variables, the issue i'm having is that I need to capture the number string at the end first, and replace it afterwards. _(239).mov, _(1237).mov etc.

Any ideas what nodes I should investigate for this?
Any help pointing me down the right path is greatly appreciated.
emcodem
Posts: 1631
Joined: Wed Sep 19, 2018 8:11 am

Re: File Renaming

Post by emcodem »

Hey TMH,
welcome to the forum!
your Announcement thread is great, i like it :-)

Your question is a little confusing for me as you already do lots of regex, so i don't understand what exactly is your problem: are you having problems getting the regex done or do you have totally different problems. However, here how i understood what you want to do:
Take a Populate Variables processor, create a new User variable to hold the calculated target filename (in my example "new_filename") and do your regext like that:

newfilename.png
newfilename.png (15.35 KiB) Viewed 10275 times

Code: Select all

$replace("%s_source%",$regext("%s_source%","(\d+)\...."),"1111")

Code: Select all

$replace("%s_new_filename%",$regext("%s_source%","_(f\d+)_"),"29.9")

Code: Select all

$replace("%s_new_filename%",$regext("%s_source%","_(UHD)_|_(HD)_|_(SD)_"),"SD")
The first regex with the "1111" at the end will take any number before a dot followed by three characters (i hope your extensions always have 3 characters)

The last part, "1111", "29.9" and "SD" sure needs to be replaced by some other text/variable from you, it represents the stuff that is inserted into the new filename.
emcodem, wrapping since 2009 you got the rhyme?
tmhlegolas
Posts: 8
Joined: Tue Jul 09, 2019 12:46 am

Re: File Renaming

Post by tmhlegolas »

I see now that the root of my problem is being ignorant of the syntax of functions.

Your examples look like they'll help a lot!

I can't wait to try this out.
momocampo
Posts: 592
Joined: Thu Jun 08, 2017 12:36 pm
Location: France-Paris

Re: File Renaming

Post by momocampo »

Hello encodem,

I'm trying to understand this Regext function but I must admit it's a little bit difficult. I understand the first part ($regext("%s_source%") "catch" the source file name but the second _(f\d+)_ or (\d+)\.... is quiet drak for me :D
I know $regext is a FFastrans Function but there is only 2 examples in help file so I believe I don't understand all the "subtleties."
Can you give me more example please ? I think I could use these great function .
Thank you very much.

Cheers.
emcodem
Posts: 1631
Joined: Wed Sep 19, 2018 8:11 am

Re: File Renaming

Post by emcodem »

@thmlegolas great, let me know if it works out for you :-)
@momocampo

Regex is very mighty and very complex. It is like it's own little programming language for finding stuff in text. The good thing about it is that a developer can use it in almost every programming language. So even if one changes the used language, finding text always works the same.
Even tough i believe it is easier to understand for you if you find some french language tutorial for regex in the internet, i'll try to explain a little:

i believe the biggest problem using regex is that you need to be able to "see" or recognize what part of the regex is "looking for a plain character" and what part is a function in regex.
The first thing that i do when looking at a regex is to get out what are actual plain text characters and what are regex functions.

Above regex explained, let us remove the parantheses first, i'll come to that part as last thing.

Code: Select all

Regex: _f\d+_
Explained:
-) The first 2 characterst "_f" are looking for "plain text", so the first occurence of "_f"
-) \d is not "plain text", it is a regex function to find a number. \d+ means "one or more numbers", e.g. 13834
-) The last character _ is "plain text" again.
-) when you put all that together, the regex is looking for something like "_f43352_" In case the text looks different like "_fABC_" or "_f1234ABC", the regex would not match anything.

Now with parantheses

Code: Select all

Regex: _(f\d+)_
In Words: please find "f" followed by one or more numbers, but only if before "f" there is at least one "_" and after the number also there is at least one "_"
-) Still exactly the same as above, but the parantheses define that we do not want the "_" at start and end in the match result.
-) without the parantheses, the match result for "_f1234_" would be "_f1234_" but with parantheses the match result is "f1234"
-) Other Examples:
-) ABC_f5432_DEF --> regex finds: f5432
-) 123_f1_6542 --> regex finds: f1
-) 123_f12_6542 --> regex finds: f12
-) 123_f54ABC32_6542 --> regex finds: nothing

Code: Select all

Regex: _(\d+)\....
In Words: give me the number where before the number there is a "_" and after the number one "dot (.)" followed by 3 charcters minimum
-) ABC123_456.mov --> regex finds 456
-) ABC123_456.m4 --> regex finds nothing
-) ABC123_456.mxfmxf --> regex finds 456
-) ___1.mp4 --> regex finds 1

Code: Select all

Without parantheses: _\d+\...
.
-) ABC123_456.mov --> regex finds _456.mov
-) ABC123_456.m4 --> regex finds nothing
-) ABC123_456.mxfmxf --> regex finds _456.mxf
-) ___1.mp4 --> regex finds _1.mp4


In parts:

Code: Select all

Regex: .
-) ABC123 --> regex finds A (the first occurence of exactly one character)

Code: Select all

Regex: ..
-) ABC123 --> regex finds AB (the first occurence of exactly two characters)

Code: Select all

Regex: \.
-) ABC123 --> regex finds nothing
-) ABC123.mxf --> regex finds the "."

Code: Select all

Regex: \.mxf
-) ABC123.mxf --> regex finds .mxf

Code: Select all

Regex: \d
-) ABC123 --> regex finds 1 (the first occurence of a number)

Code: Select all

Regex: \d\d
-) ABC123 --> regex finds 12 (the first occurence of 2 numbers in a row)

Code: Select all

Regex: \d+
-) ABC123 --> regex finds 123 (the first occurence of a series of numbers)

Code: Select all

Regex: \d+
-) ABC123 --> regex finds 123 (the first occurence of a series of numbers)

Code: Select all

Regex: \w
-) ABC123 --> regex finds A (the first occurence of a letter character [a-z])
emcodem, wrapping since 2009 you got the rhyme?
momocampo
Posts: 592
Joined: Thu Jun 08, 2017 12:36 pm
Location: France-Paris

Re: File Renaming

Post by momocampo »

My god :shock: :shock: :shock:
This is a real tutorial !! It's so kind from you ;)
I have to read that very carefully but I can't wait to try all of this :D
Thank you so much for the time you spent to teach us ;)

cheers.
tmhlegolas
Posts: 8
Joined: Tue Jul 09, 2019 12:46 am

Re: File Renaming

Post by tmhlegolas »

wow, encodem you are a true saint.

momocampo, if you want to practice some of the examples encodem gives, sites like this may help:
https://regexone.com/
https://regexr.com/
If you search "regex tester" in your native language you may find some that work better.
tmhlegolas
Posts: 8
Joined: Tue Jul 09, 2019 12:46 am

Re: File Renaming

Post by tmhlegolas »

Encodem. First off, thank you so much for helping, your method does work. However, I think I have a better handle now on the functions and my question to you was unclear.

I realize now that I want to Capture those name segments mentioned as variables. This way I can tear apart and rebuild as necessary. I am not sure If what i'm asking is possible.

Assuming %s_original_name%.%s_original_ext% is NewPalPost_HistoryOfTheShowName_ENG_f25_UHD_1237.mov

Imagine I want populate the variable %s_framerate% from the string %s_original_name%.%s_original_ext% using the regex ^.+_(f25)_.+$

Is that possible? I think you can see that I am more video editor than computer programmer:)
Again, thank you so much for your time, your previous method will work, but will not allow me to use the variables created from captured filename values for other things if i needed.
momocampo
Posts: 592
Joined: Thu Jun 08, 2017 12:36 pm
Location: France-Paris

Re: File Renaming

Post by momocampo »

Thanks Tmhlegolas, I found a good tutorial in French. It will help me to begin with regex.
About FFastrans, I usually use the functions and try to "aim" the good character to create a variable. Regex seems to be more specific :)
Cheers.
emcodem
Posts: 1631
Joined: Wed Sep 19, 2018 8:11 am

Re: File Renaming

Post by emcodem »

tmhlegolas wrote: Wed Jul 31, 2019 6:40 pm Imagine I want populate the variable %s_framerate% from the string %s_original_name%.%s_original_ext% using the regex ^.+_(f25)_.+$
Sure, on the left side in the populate vars processor, you choose your s_framerate variable and on the right side something like that:
$regext("%s_original_name%.%s_original_ext%","_(f\d+)_")

Simplified (you don't need to use the extension variable for matching stuff in the original filename):

Code: Select all

$regext("%s_original_name%","_(f\d+)_")
In words: Take the original filename (without path and extension) as it was picked up by the monitor processor and apply the regex to it


I changed your regex to _(f\d+)_ because i did not understand why you would specify start and end with ^.+ .+$, it should work without that. Also \d+ in order to match all framerates, not only 25.

As you might recognize, the matched value will be like "f25". If you change the parantheses to _f(\d+)_, the matched value will be like 25. You always get the "first matchgroup" (first value in parantheses) as result. In case there are no parantheses you get the full match back, e.g. for _f\d+_ the matched value would be like _f25_
emcodem, wrapping since 2009 you got the rhyme?
Post Reply