Renaming Multiple Same Language Subtitles

Running FileBot from the console, Groovy scripting, shell scripts, etc
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Hi

Is it possible to keep all the language versions of subtitles?
Some files arrive with english_2, english_3, english_9 etc
I would be very happy is it could be named accordingly....

Code: Select all

Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay]
Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].eng(1)
Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].eng(2)
Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].fre(1)
Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].fre(2)
and named accordingly as I currently get errors and only a single one will be named when using {subt}

I use emby and it will happily work with this format unless anyone know a better way.

Thank you in advance for any help


My current format....

Code: Select all

\\192.168.0.160\Multimedia/Movies/{f =~ /(?i)3D|3\.D|3\-D/ ? '3D Movies' : vf =~ /2160p/ ? '4K Movies' : 'Movies'}/{AZ}/
{ny}
/{ny} - [{vf} {vc} {hdr} 
{
	def codecList =
	[
	'MP3' : 'MP3',
	'PCM' : 'PCM',
	'AAC LC' : 'AAC',
	'AAC LC SBR' : 'AAC',
	'AC 3' : 'AC3',
	'AC 3 Dep' : 'EAC3',
	'E AC 3' : 'EAC3',
	'E AC 3 JOC' : 'EAC3 Atmos',
	'E-AC-3 JOC' : 'EAC3 Atmos',
	'DTS' : 'DTS',
	'DTS 96 24' : 'DTS 96-24',
	'DTS ES' : 'DTS-ES',
	'DTS ES XXCH' : 'DTS-ES',
	'DTS XBR' : 'DTS-HD HRA',
	'DTS ES XBR' : 'DTS-HD HRA',
	'DTS ES XXCH XBR' : 'DTS-HD HRA',
	'DTS XLL' : 'DTS-HD MA',
	'DTS ES XLL' : 'DTS-HD MA',
	'DTS ES XXCH XLL' : 'DTS-HD MA',
	'DTS XLL X' : 'DTS X',
	'MLP FBA' : 'Dolby TrueHD',
	'MLP FBA 16 ch' : 'Dolby Atmos'
	]
	def filter = { [it.codec, it.ch, it.objects, it.lang] }

	def audioStreams = []
	def audioClean = { it.replaceAll(/[\p{Pd}\p{Space}]/, ' ').replaceAll(/\p{Space}{2,}/, ' ').slash(' ') }
	def channelClean = { it.replaceAll(/Debug.+|Object\sBased\s?\/?|(\d+)?\sobjects\s\/\s|0.(?=\d.\d)|20/).replaceAll(/6.0/,'5.1').replaceAll(/8.0/,'7.1')}
	def oneStream = { it.collect{ filter(it) }*.minus(null).unique().flatten().join(' ') }
	def dString = { it.toDouble().toString() }
	def toInt = { it.toInteger() }

	audio.collect{ au ->
		def codec = audioClean(any{ au['CodecID/Hint'] }{ au['Format'] })
		def format_profile = any{ audioClean(au['Format_AdditionalFeatures'])}{}
		def String ch = any{ channelClean(au.ChannelPositionsString2).tokenize('\\/')*.toDouble().toString().sum() }
			{ channelClean(dString(au.ChannelsOriginal)) } { channelClean(dString(au.Channels)) }

		def chFilter =	(( ( (ac == 'AAC'||ac == 'MP3') && ch != '2.0') || ( (ac == 'AC3'||ac == 'EAC3'||ac == 'DTS'||ac == 'TrueHD'||ac == 'MLPFBA') && ch != '5.1' ) ) ? ch : null)

		def combined = allOf{codec}{format_profile}.join(' ')
		audioStreams << ['index' : codecList.findIndexOf {it.key == combined}, 'default' : au['default'][0].toBoolean(),
		'codec' : codecList.get(combined, 'UNKNOWN_FORMAT'), 'combined' : combined, 'ch' : ch, 
		'bitrate' : any{toInt(au.BitRate)}{toInt(au.BitRate_Maximum)}{au.FrameRate.toDouble()}{null}]
		return audioStreams
	}

	def allStreams = audioStreams.collect{ filter(it) }*.minus(null).unique()*.join(' ')
	def preferredStream = oneStream(audioStreams.findAll{ it.index == audioStreams.index.max() })
	def bestBitRate = oneStream(audioStreams.findAll{ it.bitrate == audioStreams.bitrate.max() })
	def defaultStream = oneStream(audioStreams.findAll{it.default == true})

	allStreams.join('  ').space(' ')
	preferredStream.space(',')
	defaultStream.space(' ')
	bestBitRate.space(' ')
	[bestBitRate, preferredStream].unique().join('  ')	
}] {[source]}{subt}
User avatar
rednoah
The Source
Posts: 22898
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Renaming Multiple Same Language Subtitles

Post by rednoah »

You'd have to match the number for the original file path:
viewtopic.php?t=12643


e.g. if you have subtitles named like so:

Code: Select all

english_2.srt
english_3.srt
english_9.srt
then some format code such as this would add (2) and (3) and (9) respectively to the target file path:

Code: Select all

{ if (f.subtitle) '(' + fn.match(/_(\d+)$/) + ')' }

:!: Note that there is little to no standard how to to name multiple subtitles per language, so if you add an extra number somewhere to make the file name unique, that may or may not make it more difficult player software to automatically connect video files to subtitle files.
:idea: Please read the FAQ and How to Request Help.
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Hi
I tried but it just gives the same name using the code above

Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].eng
Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].eng
Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].eng

it does not use the number???
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Renaming Multiple Same Language Subtitles

Post by kim »

did you put

Code: Select all

{ if (f.subtitle) '(' + fn.match(/_(\d+)$/) + ')' } 
after ?

Code: Select all

{[source]}{subt}
was the input file e.g. ?:

Code: Select all

Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].eng_2.srt

Code: Select all

_(\d+)$/)
=
_1digitnumOrMore and nothing after this

try:

Code: Select all

{ if (f.subtitle) '(' + fn.match(/_(\d{1,2})$|\((\d{1,2})\)$/) + ')'  }
support:
eng(1)
english_2
User avatar
rednoah
The Source
Posts: 22898
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Renaming Multiple Same Language Subtitles

Post by rednoah »

CHA0SENG7NE wrote: 13 Nov 2021, 22:39 I tried but it just gives the same name using the code above

Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].eng
Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].eng
Movie Name (2021) - [1080p x265 AAC 5.1] [BluRay].eng
:?: What is the original file path?

:?: What format are you using?

:idea: The solution above is tailored specifically to the subtitle file name patterns listed above, i.e. english_2.srt, and may not work for other naming and numbering patterns, and thus may need to be customized for the file paths that you actually have.
:idea: Please read the FAQ and How to Request Help.
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Hi
Its working! sorry Kim & Rednoah I placed it in the wrong location. I put it before {[source]}{subt}
Switched around and its worked perfectly.
My bad
Thank you both yet again
Super happy with the format I use :)
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Also this format works just great in Emby
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Hi
I have just come across another format
Is it possible to add another option to resolve this..?

2_English
3_English
4_English

Any help appreciated as always :)
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Renaming Multiple Same Language Subtitles

Post by kim »

Learn Regular Expression
I made this, all you need to do is add the last one ;)
https://regexr.com/69j81
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Hi Kim
Thank you I'm having good fun not getting anywhere!
I have tried but just cant get it to work.
Appreciate the time setting the file up :)
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Renaming Multiple Same Language Subtitles

Post by kim »

What have you got (tried) now ?

https://regexr.com/69l3u
HINT: mouse over for info ;)

(group) = output

[quote=kim post_id=56051 time=1636847555 user_id=1543]

Code: Select all

_(\d+)$/)
=
_1digitnumOrMore and nothing after this
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Hi Kim
SO i find this very confusing

Code: Select all

_(\d{1,2})$|\((\d{1,2})\)$
Why does this repeat?

Code: Select all

_(\d{1,2})$   and then again  (\d{1,2})\) 
im trying but i think i have become even more confused if that possible!
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

this is as far as i got

Code: Select all

_(\d{1,2})$|\((\d{1,2})\)$|\_(\d+)$\)
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Renaming Multiple Same Language Subtitles

Post by kim »

Here you go ;)
https://regexr.com/69lo0

play around with it and learn ;)



^abc$ start / end of the string

() = Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.

\d = Matches any digit character (0-9). Equivalent to [0-9].

+ = Matches 1 or more of the preceding token.

Matches the specified quantity of the previous token. {1,3} will match 1 to 3. {3} will match exactly 3. {3,} will match 3 or more.

Acts like a boolean OR. Matches the expression before or after the |.
It can operate within a group, or on a whole expression. The patterns will be tested in order.
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

So this is the full string??

Code: Select all

{ if (f.subtitle) '(' + fn.match(/_(\d{1,2})$|\((\d{1,2})\)$|^(\d+)_$/) + ')' }
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

So trying to break this down to understand better to prove out with just subtitles like

2_English
3_English

Code: Select all

{ if (f.subtitle) '(' + fn.match(/^(\d+)_/) + ')' }
It will only rename 1 file not multiples
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Renaming Multiple Same Language Subtitles

Post by kim »

Test in GUI:

Code: Select all

{ '2_English'.match(/_(\d{1,2})$|\((\d{1,2})\)$|^(\d+)_$/) }
Why put _$ at the end ?
_$ = match e.g. 2_English_.srt

Is the number always just single number ?

Code: Select all

{ '2_English'.match(/_(\d)$ | \((\d)\)$ | ^(\d)/) }
*remove the spaces

Code: Select all

{ 'eng(1)'.match(/\((\d)\)$/) }

Code: Select all

{ 'english_2'.match(/_(\d)$/) }

Code: Select all

{ '2_English'.match(/^(\d)/) }
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Renaming Multiple Same Language Subtitles

Post by kim »

CHA0SENG7NE wrote: 16 Nov 2021, 20:59 So trying to break this down to understand better to prove out with just subtitles like

2_English
3_English

Code: Select all

{ if (f.subtitle) '(' + fn.match(/^(\d+)_/) + ')' }
It will only rename 1 file not multiples
? you need rednoah for that
works ok for me in [Plain File Mode] Batch Rename any type of file
viewtopic.php?t=2072
User avatar
rednoah
The Source
Posts: 22898
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Renaming Multiple Same Language Subtitles

Post by rednoah »

Why not just match all the numbers from the file path, and then pick the last one? That should work for all the sample file paths discussed above and generalize well for everything else as well:

Code: Select all

{ fn.matchAll(/\d+/).last() }
:idea: Please read the FAQ and How to Request Help.
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Rednoah I added this to the format.

{subt}
{if (f.subtitle) '(' + { fn.matchAll(/\d+/).last() } + ')' }

If i try and rename any version of the subititle with the movie it works perfectly. If its only 1 subititle.
If i try and rename anymore than 1 with the movie it does not see them, does the code require something else to name multiples?

Thanks in advance

I use the windows version of the program and have it set to strict. Just tried opportunistic which sees the other subtitle but tries to name them to random non matching episodes. It does name them correctly i.e eng(2)
User avatar
rednoah
The Source
Posts: 22898
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Renaming Multiple Same Language Subtitles

Post by rednoah »

The code simply matches the last number from file name. No more. No less.


If you have trouble with matching, then that's an entirely different issue unrelated to subtitle formatting. As always, screenshots and file paths are absolutely required for any kind of guess as to why something might not be working as expected.
:idea: Please read the FAQ and How to Request Help.
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Hi Rednoah, might seem a strange question but Im unsure how to upload a screenshot?
User avatar
rednoah
The Source
Posts: 22898
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Renaming Multiple Same Language Subtitles

Post by rednoah »

Please click on the Screenshots link on the How to Request Help page, then upload your screenshot and copy & paste the image link back here.
:idea: Please read the FAQ and How to Request Help.
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Image
CHA0SENG7NE
Posts: 70
Joined: 19 Apr 2017, 22:13

Re: Renaming Multiple Same Language Subtitles

Post by CHA0SENG7NE »

Thanks for the info Rednoah that was easy.
The format is naming how I want it is pickng up the wrong episode.
This is set on opportunistic using this format
The files are in the f=same folder called Friends

Code: Select all

\\192.168.0.160\Multimedia\TV Shows/{n}/Season {s00}/{episode}{" [$vf $vc "}{hdr} 
{
	def codecList =
	[
	'MP3' : 'MP3',
	'PCM' : 'PCM',
	'AAC LC' : 'AAC',
	'AAC LC SBR' : 'AAC',
	'AC 3' : 'AC3',
	'AC 3 Dep' : 'EAC3',
	'E AC 3' : 'EAC3',
	'E AC 3 JOC' : 'EAC3 Atmos',
	'E-AC-3 JOC' : 'EAC3 Atmos',
	'DTS' : 'DTS',
	'DTS 96 24' : 'DTS 96-24',
	'DTS ES' : 'DTS-ES',
	'DTS ES XXCH' : 'DTS-ES',
	'DTS XBR' : 'DTS-HD HRA',
	'DTS ES XBR' : 'DTS-HD HRA',
	'DTS ES XXCH XBR' : 'DTS-HD HRA',
	'DTS XLL' : 'DTS-HD MA',
	'DTS ES XLL' : 'DTS-HD MA',
	'DTS ES XXCH XLL' : 'DTS-HD MA',
	'DTS XLL X' : 'DTS X',
	'MLP FBA' : 'Dolby TrueHD',
	'MLP FBA 16 ch' : 'Dolby Atmos'
	]
	def filter = { [it.codec, it.ch, it.objects, it.lang] }

	def audioStreams = []
	def audioClean = { it.replaceAll(/[\p{Pd}\p{Space}]/, ' ').replaceAll(/\p{Space}{2,}/, ' ').slash(' ') }
	def channelClean = { it.replaceAll(/Debug.+|Object\sBased\s?\/?|(\d+)?\sobjects\s\/\s|0.(?=\d.\d)|20/).replaceAll(/6.0/,'5.1').replaceAll(/8.0/,'7.1')}
	def oneStream = { it.collect{ filter(it) }*.minus(null).unique().flatten().join(' ') }
	def dString = { it.toDouble().toString() }
	def toInt = { it.toInteger() }

	audio.collect{ au ->
		def codec = audioClean(any{ au['CodecID/Hint'] }{ au['Format'] })
		def format_profile = any{ audioClean(au['Format_AdditionalFeatures'])}{}
		def String ch = any{ channelClean(au.ChannelPositionsString2).tokenize('\\/')*.toDouble().toString().sum() }
			{ channelClean(dString(au.ChannelsOriginal)) } { channelClean(dString(au.Channels)) }

		def chFilter =	(( ( (ac == 'AAC'||ac == 'MP3') && ch != '2.0') || ( (ac == 'AC3'||ac == 'EAC3'||ac == 'DTS'||ac == 'TrueHD'||ac == 'MLPFBA') && ch != '5.1' ) ) ? ch : null)

		def combined = allOf{codec}{format_profile}.join(' ')
		audioStreams << ['index' : codecList.findIndexOf {it.key == combined}, 'default' : au['default'][0].toBoolean(),
		'codec' : codecList.get(combined, 'UNKNOWN_FORMAT'), 'combined' : combined, 'ch' : ch, 
		'bitrate' : any{toInt(au.BitRate)}{toInt(au.BitRate_Maximum)}{au.FrameRate.toDouble()}{null}]
		return audioStreams
	}

	def allStreams = audioStreams.collect{ filter(it) }*.minus(null).unique()*.join(' ')
	def preferredStream = oneStream(audioStreams.findAll{ it.index == audioStreams.index.max() })
	def bestBitRate = oneStream(audioStreams.findAll{ it.bitrate == audioStreams.bitrate.max() })
	def defaultStream = oneStream(audioStreams.findAll{it.default == true})

	allStreams.join('  ').space(' ')
	preferredStream.space(',')
	defaultStream.space(' ')
	bestBitRate.space(' ')
	[bestBitRate, preferredStream].unique().join('  ')	
}]
 {[source]}{subt}{if (f.subtitle) '(' + { fn.matchAll(/\d+/).last() } + ')' }
Post Reply