Shared functions/AdvancedMultiTrim

From Avisynth wiki
Jump to: navigation, search
/*
* ADVANCED MULTI TRIM
*
* syntax:
*     clip.advancedmultitrim("frame1,frame2,...")
* or
*     advancedmultitrim(clip,"frame1,frame2,...")
* you can use as many frames as you like but the number of frames in the list must be even as they are processed in pairs
*
* clip.advancedmultitrim("0,100,200,300")
* will return frames 0 to 100 and 200 to 300
*
* the '+' flag can be used on any second frame, if present the multitrim function reads that frame number as being a forward offset from the previous frame number in the list
* clip.advancedmultitrim("250,+100")
* will return frames 250 to 350
*
* the '-' flag can be used of any frame number, if present it reads that frame number as being an offset from the end of the clip
* clip.advancedmultitrim("250,-100")
* will return frames 250 to (clip.framecount-100)
*
* clip.advancedmultitrim("-1000,-500")
* will return frames (clip.framecount-1000) to (clip.framecount-500)
*
* clip.advancedmultitrim("-1000,+200")
* will return frames (clip.framecount-1000) to ((clip.framecount-1000)+200)
*
* the '~' flag can be used on any second frame, it functions similarly to the '+' flag but offsets backwards rather than forward and will return a reversed portion of the clip
* clip.advancedmultitrim("1000,~200")
* will return frames 800 to 1000 in reverse
*
* Todo:
* this really needs cleaning up and optimizing, i suck at avisynth so if anyone is bored, let loose
*
*/

global _advancedmultitrim = false
function AdvancedMultiTrim(input,string frames) {
	tstring = frames /*string of frames, must have an even amount*/
	toffset = FindStr(tstring,",") /*offset of the first ',' character*/

	trimstart = LeftStr(tstring,toffset-1) /*get the first frame number*/
	flag = LeftStr(trimstart,1) /*get the first character*/
	trimstart = (flag=="-"||flag=="+"||flag=="~") ? RightStr(trimstart,StrLen(trimstart)-1): trimstart /* if a flag is present, remove it from the frame string */
	trimstart = StrToInt(trimstart) /*convert to integer*/
	trimstart = flag=="-" ? (input.framecount-trimstart) : trimstart /* process frame for '-' flag */

	tstring = RightStr(tstring,StrLen(tstring)-toffset) /*remove the first frame and seperator (return everything right of the first ',')*/
	toffset = FindStr(tstring,",") /*offset of the first ',' character*/

	trimend = toffset==0 ? tstring : LeftStr(tstring,toffset-1) /*find the second frame number*/
	flag = LeftStr(trimend,1) /*get the first character*/
	trimend = (flag=="-"||flag=="+"||flag=="~") ? RightStr(trimend,StrLen(trimend)-1) : trimend /* if a flag is present, remove it from the frame string */
	trimend = StrToInt(trimend) /*convert to integer*/
	trimend = flag=="-" ? (input.framecount-trimend) : trimend /* process frame for '-' flag */
	trimend = flag=="+" ? (trimstart+trimend) : trimend /* process frame for '+' flag */
	trimend = flag=="~" ? (trimstart-trimend) : trimend /* process frame for '~' flag */

	tstring = toffset==0 ? "" : RightStr(tstring,StrLen(tstring)-toffset)

	segment = flag=="~" ? input.trim(trimend,trimstart).reverse() : input.trim(trimstart,trimend) /* find segment to trim, process reversing for '~' flag here */

	global _advancedmultitrim = IsClip(_advancedmultitrim) ? _advancedmultitrim++segment : segment

	return toffset==0 ? _advancedmultitrim : AdvancedMultiTrim(input,tstring)
}

function StrToInt(string n){
	return isint(eval(n)) ? int(eval(n)) : 0
}

Back to Shared functions.

Personal tools