Help to simplify my script.

All about user-defined episode / movie / file name format expressions
Post Reply
antisgae
Posts: 13
Joined: 29 Aug 2018, 22:48

Help to simplify my script.

Post by antisgae »

Hi i have a so complex script, that in fact is so big that i reached the MAX CHARS limit, and sometimes is not working properly, can you help me to simplify it? I did it getting parts from forums here and there, is a strange collage.

Bests.

# I NEED HELP al these is to get bitrate....so often fails...

Code: Select all

{def V1=video.bitrate.toString(); def V=V1.substring(1,V1.indexOf("]"));def audbit=audio.bitrate.toString().minus("Unknown /").minus(" Unknown /").minus("[").minus("]").replaceAll("null","0"); def audnum=audio.streamcount.toString().substring(1,2).toInteger();def posaud1=audbit.indexOf(',',0);def posaud2=audbit.indexOf(',',posaud1+1); def posaud3=audbit.indexOf(',',posaud2+1);def posaud4=audbit.indexOf(',',posaud3+1);def posaud5=audbit.indexOf(',',posaud4+1);def posaud6=audbit.indexOf(',',posaud5+1); if ( V != "null") {(V.toFloat()/1e6d).round(1)} else{ ((bitrate-(audnum >= 1 ?( posaud1 == -1 ? audbit : audbit.substring(0,posaud1)) :0 ).toInteger() -(audnum >= 2 ?(posaud2 == -1 ? audbit.substring(posaud1+1):audbit.substring(posaud1+1,posaud2)):0).toInteger() -(audnum >= 3 ?(posaud3 == -1 ? audbit.substring(posaud2+1):audbit.substring(posaud2+1,posaud3)):0).toInteger() -(audnum >= 4 ?(posaud4 == -1 ? audbit.substring(posaud3+1):audbit.substring(posaud3+1,posaud4)):0).toInteger() -(audnum >= 5 ?(posaud5 == -1 ? audbit.substring(posaud4+1):audbit.substring(posaud4+1,posaud5)):0).toInteger() -(audnum >= 6 ?(posaud6 == -1 ? audbit.substring(posaud5+1):audbit.substring(posaud5+1,posaud6)):0).toInteger())/1e6d).round(1)}}Mbps{" "}

# I NEED HELP If there are more than 5 subs to just use "multisub" word.

Code: Select all

{def x = text.language.findResults{ it.toLocale().ISO3Language.upper().replaceAll("SPA","ESP") }.unique().sort().join(' '); if (x) "SUB $x"}
Last edited by antisgae on 04 Nov 2019, 19:51, edited 3 times in total.
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Help to simplify my script.

Post by rednoah »

1.
Sorry, I can't really help with "fix everything" requests. If you have specific questions on how to simplify / generalize specific pieces of logic, then I can have a look and see what I might come up with to solve the problem in a more elegant way.


2.
Newer versions of FileBot allow you to use @/path/to/format.groovy in the Format Editor GUI, so you can have an @format which just points to a text file with your format, which means no more MAX CHARS limit and you can use your favorite multi-line code editor too:

Code: Select all

@/path/to/format.groovy

:idea: That was added recently I think, so might need the latest beta for that: viewtopic.php?f=7&t=1609
:idea: Please read the FAQ and How to Request Help.
antisgae
Posts: 13
Joined: 29 Aug 2018, 22:48

Re: Help to simplify my script.

Post by antisgae »

Hi @rednoah i need help to simplify the bitrate (now it dont work sometimes), the rest more or less i did it. And also i need help with the subs if there are more than 6 languages change everything for just multisub. I hope somebody can help, bests.

Reading the forum i simplified the audio codec + channels, THANKS TO THIS:
viewtopic.php?f=5&t=5285&start=25

BEFORE:

Code: Select all

{def audlang=media.audiolanguagelist.toString().replace("Spanish","ESP").replace("Japanese","JAP").replace("English","ENG") .replace("Croatian","CRO").replace("Czech","CHE").replace("Danish","DAN").replace("Dutch","HOL").replace("Estonian","EST") .replace("Finnish","FIN").replace("French","FRA").replace("German","GER").replace("Greek","GRI").replace("Hebrew","HEB") .replace("Hungarian","HUN").replace("Icelandic","ISL").replace("Indonesian","IND").replace("Italian","ITA").replace("Norwegian","NOR") .replace("Persian","PER").replace("Polish","POL").replace("Portuguese","POR").replace("Romanian","RUM").replace("Russian","RUS") .replace("Slovenian","SLO").replace("Serbian","SER").replace("Swedish","SUE").replace("Korean","KOR").replace("Norwegian","NOR") .replace("Turkish","TUR").replace("Vietnamese","VIT").replace("Latin","LAT").replace("Catalan","CAT").toUpperCase(); def audcodec=media.audiocodeclist.toString().replace("AAC LC-SBR","AAC").replace("AAC LC","AAC").replace("MPEG-1 Audio layer 3","MP3").toLowerCase(); 
AFTER:

Code: Select all

{
	def codecList =
	[
	'MP3' : 'mp3',
	'FLAC' : 'flac',
	'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',
	'DTS' : 'dts',
	'DTS 96 24' : 'dts',
	'DTS ES' : 'dtses',
	'DTS ES XXCH' : 'dtses',
	'DTS XBR' : 'dts',
	'DTS ES XBR' : 'dtses',
	'DTS ES XXCH XBR' : 'dtses',
	'DTS XLL' : 'dts',
	'DTS ES XLL' : 'dtses',
	'DTS ES XXCH XLL' : 'dtses',
	'DTS XLL X' : 'dtsx',
	'MLP FBA' : 'truehd',
	'MLP FBA 16 ch' : 'truehd Atmos'
	]
	def filter = { [it.lang, it.codec, it.ch, it.objects] }

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},
		'objects' : any{'[' + au['NumberOfDynamicObjects'] + ' Objs]'}{null}, 'lang' : any{au.'LanguageString3'.upper().replaceAll("SPA","ESP")}{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(' ')
	[defaultStream, bestBitRate].unique().join(' ')
}
Last edited by antisgae on 04 Nov 2019, 20:00, edited 1 time in total.
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Help to simplify my script.

Post by rednoah »

1.
:?: I want the bitrate in a format such as this: 8.1 Mbps

Code: Select all

{mbps}

2.
:?: I want subtitle tracks in a format such as this: [ENG, DEU] using 3-letter language codes, or just abbreviate with [MultiSub] if there are 5+ subtitle tracks:

Code: Select all

{textLanguages.size() > 5 ? '[MultiSub]' : textLanguages.ISO3*.upper()}
:idea: Please read the FAQ and How to Request Help.
antisgae
Posts: 13
Joined: 29 Aug 2018, 22:48

Re: Help to simplify my script.

Post by antisgae »

THANKS A LOT! now i have space to code more things.

The subs I'm using this code:
{def x = text.language.findResults{ it.toLocale().ISO3Language.upper().replaceAll("SPA","ESP") }.unique().sort().join(' '); if (x) "SUB $x"}

I'm trying to use yours with this. Because i need if there are subs, use the word SUB before.
ej: SUB ESP ENG
And if there are more than 5, just the word multisub.
ej: multisub


I think i have it. No i dont... something like this but working :P

Code: Select all

{
def x = text.language.findResults{ it.toLocale().ISO3Language.upper().replaceAll("SPA","ESP") }.unique().sort().join(' ');
if (x.size() > 5) "multisub"
if (x.size() > 0) "SUB $x"

}
Thnks.
rednoah wrote: 04 Nov 2019, 19:56 1.
:?: I want the bitrate in a format such as this: 8.1 Mbps

Code: Select all

{mbps}

2.
:?: I want subtitle tracks in a format such as this: [ENG, DEU] using 3-letter language codes, or just abbreviate with [MultiSub] if there are 5+ subtitle tracks:

Code: Select all

{textLanguages.size() > 5 ? '[MultiSub]' : textLanguages.ISO3*.upper()}
antisgae
Posts: 13
Joined: 29 Aug 2018, 22:48

Re: Help to simplify my script.

Post by antisgae »

DID IT!

Code: Select all

{textLanguages.size() > 2 ? 'multisub' : 'SUB '+text.language.findResults{ it.toLocale().ISO3Language.upper().replaceAll("SPA","ESP") }.unique().sort().join(' ')}
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Help to simplify my script.

Post by rednoah »

This will take care of everything:

Code: Select all

textLanguages.ISO3B*.upper().unique().toSorted().joining(', ', 'SUBS [', ']')


EDIT:

ESP is not a valid language code for Spanish, neither according to ISO 639-2/T nor ISO 639-2/B
https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes
:idea: Please read the FAQ and How to Request Help.
antisgae
Posts: 13
Joined: 29 Aug 2018, 22:48

Re: Help to simplify my script.

Post by antisgae »

In Spain we preffer thhe ESP instead of SPA, spa is the english way, in fact the 2 chars iso is .es nop .sp, so we use ESP instead of SPA even iso says other thing. Bests and thanks, my script is now much better than yesterday.

i think was a big mistake to use SPA and it will be fixed one day, microsoft already did it and uses ESP
https://docs.microsoft.com/es-es/dotnet ... mework-4.8
and the iso ISO 639-3 is esp (well is not but they try and some places it says it is)
the iso-31661- alpha 3 is ESP, the reason we dont like spa....
https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
Post Reply