Timecode parsing for user input


i have script allows users enter timecode string, , wanted perform same timecode inputs in rest of program: if type "1..." want register 1:00:00:00 without them having type correctly formatted string.

 

this. took. ages. descended deep regex underworld, i'm happy managed placate js gods mad skillz on lyre , returned victorious. other young traveller preparing take journey, here's worked out. if have suggestions, please, let me know.

 

function parsetimestring(thestring, comp) {     comp = app.project.activeitem; //need active comp in order frameduration     if (comp) {         thestring = "" + thestring;         //allows user lazily enter timecode, eg. enter 1 minute type 1.. rather 0:1:0:0         //this took ages work out..         var hrsstr = thestring.match(/(\d+)\d\d*\d\d*\d\d*$/); //matches  "0:1:2:3" , "0..." , returns '0'         var minstr = thestring.match(/(\d+)\d\d*\d\d*$/); //matches "0:1:2:3" , "1,2.3" , "1.." , returns 1         var secstr = thestring.match(/(\d+)\d\d*$/); //and on..         var frmstr = thestring.match(/(\d+)$/);         //convert strings time values         var hrs = hrsstr             ? parseint(hrsstr)             : 0;         var min = minstr             ? parseint(minstr)             : 0;         var sec = secstr             ? parseint(secstr)             : 0;         var frm = frmstr             ? parseint(frmstr)             : 0;         //return result time, measured in seconds         return 3600 * hrs + 60 * min + sec + comp.frameduration * frm;     }     return false; } 

one tip, base 10 integers, use parseint(x, 10) or surprises, eg: parseint('019")

and way, in ae, non integer entries allowed in comp duration dialog!!

also make function return, in situations, false instead of number can source of mistake imo.

 

another possibility without regexp:

 

function parsetimecode(str, comp){     str = string(str);         if (!(comp instanceof compitem)) throw "comp not comp";         var seconds = 0;     var splitstr = str.split(":").reverse();     var n, n=splitstr.length;     var x;      if (n>4) throw "too many ':' !!! : hh:mm:ss:ff supported";     (n=0; n<n; n++){         x = +splitstr[n];         if (isnan(x)) throw "invalid  number input : " + splitstr[n];         splitstr[n] = x;         };         seconds += splitstr[0]* comp.frameduration;     if (n>1) seconds += splitstr[1];     if (n>2) seconds += splitstr[2]*60;     if (n>3) seconds += splitstr[3]*3600;         return seconds;     }; try{mytime = parsetimecode("10.5 : 019", app.project.activeitem)}catch(e){/*alert(e);*/}; 

 

xavier



More discussions in After Effects Scripting


adobe

Comments

Popular posts from this blog

Illustrator CS6 Ocurrío un error E/S en el archivo.

Why is os_ViewContainer running?

Animate - problem with duplicating scripts after loop