Log in

View Full Version : Timer (kinda like ShowTime())


djcj
13th July 2013, 16:44
I thought that ShowTime() had not enough functions to it, so I created this script.
It has two functions, timer() and timer_cut().

Requires:
Gavino's GRunT plugin - http://forum.doom9.org/showthread.php?t=139337
stickboy's jdl-util - http://avisynth.org/stickboy/jdl-util.avsi

Usage:
timer(clip clp, string "mode", bool "reverse", int "color", float "size", int "x", int "y", int "align")
timer_cut(clip clp, string "mode", int "start", int "end", bool "reverse", bool "showstart", bool "showend", int "color", float "size", int "x", int "y", int "align")


timer() adds the timer and has the following options:
mode - chose the mode you want to use (whether you want only minutes and seconds, or hours, minutes, seconds and milliseconds, and so on):
"hmmss": 0:04:17
"mmss": 04:17
"mss": 4:17
"mssms1": 4:17.3
"mssms2": 4:17.37
"mssms3": 4:17.375
"mssms": 4:17.375
"mmssms1": 04:17.3
"mmssms2": 04:17.37
"mmssms3": 04:17.375
"mmssms": 04:17.375
"hmmssms1": 0:04:17.3
"hmmssms2": 0:04:17.37
"hmmssms3": 0:04:17.375
"hmmssms": 0:04:17.375

reverse - the timer runs backwards, starting with the final time
color - color, default is white
size - font size
x, y - the timer's positional x and y coordinates
align - timer position relatively to x, y

timer_cut() adds a timer starting at frame a and ending at frame b. Options are the same as in timer() plus these additional options:

start, end - start and end frame
showstart, showend - If set true the timer will be visible the whole time, starting at frame a and freezing at frame b. of course you can only add the timer at the start or end if you want.

Examples:
colorbars().trim(0,3000)
import("timer.avs")
timer()
timer(mode="mssms1",color=color_green,size=66.6)
timer_cut(mode="mss",start=500,end=2500,showstart=true,showend=false,size=99.9,x=150,y=200)
blankclip(length=15001,width=320,height=120,fps=1000)
import("timer.avs")
timer(mode="mssms3",size=85,y=15)


## Written by djcj
## Last modified: July 14th 2013
##
## Requires:
## Gavino's GRunT plugin - http://forum.doom9.org/showthread.php?t=139337
## stickboy's jdl-util - http://avisynth.org/stickboy/jdl-util.avsi
##
#
# mode =
# hmmss: 0:04:17
# mmss: 04:17
# mss: 4:17
# mssms1: 4:17.3
# mssms2: 4:17.37
# mssms3: 4:17.375
# mssms: 4:17.375
# mmssms1: 04:17.3
# mmssms2: 04:17.37
# mmssms3: 04:17.375
# mmssms: 04:17.375
# hmmssms1: 0:04:17.3
# hmmssms2: 0:04:17.37
# hmmssms3: 0:04:17.375
# hmmssms: 0:04:17.375

function formattime(int ms, string mode)
{
_s = ms / 1000
_ms = ms % 1000
_m = _s / 60
_s = _s % 60
_h = _m / 60
_m = _m % 60

h = string(_h)
m = string(_m, "%2.0f")
mm = string(_m, "%02.0f")
ss = string(_s, "%02.0f")
ms = string(_ms, "%03.0f")
ms1 = leftstr(string(_ms, "%03.0f"), 1)
ms2 = leftstr(string(_ms, "%03.0f"), 2)

formattime = \
mode=="hmmss" ? h+":"+mm+":"+ss : \
mode=="mmss" ? mm+":"+ss : \
mode=="mss" ? m+":"+ss : \
mode=="mssms1" ? m+":"+ss+"."+ms1 : \
mode=="mssms2" ? m+":"+ss+"."+ms2 : \
mode=="mssms3" ? m+":"+ss+"."+ms : \
mode=="mssms" ? m+":"+ss+"."+ms : \
mode=="mmssms1" ? mm+":"+ss+"."+ms1 : \
mode=="mmssms2" ? mm+":"+ss+"."+ms2 : \
mode=="mmssms3" ? mm+":"+ss+"."+ms : \
mode=="mmssms" ? mm+":"+ss+"."+ms : \
mode=="hmmssms1" ? h+":"+mm+":"+ss+"."+ms1 : \
mode=="hmmssms2" ? h+":"+mm+":"+ss+"."+ms2 : \
mode=="hmmssms3" ? h+":"+mm+":"+ss+"."+ms : \
mode=="hmmssms" ? h+":"+mm+":"+ss+"."+ms : nop()

return formattime
}

function timer(clip clp, string "mode", bool "reverse", int "color", float "size", int "x", int "y", int "align")
{
mode = default(mode, "hmmss")
reverse = default(reverse, false)
color = default(color, $ffffff)

reverse==true ? clp.reverse() : clp
scriptclip(last, """
subtitle(formattime(round((current_frame * 1000) / framerate), mode), text_color=color, size=size, x=x, y=y, align=align)
""", args="mode,color,size,x,y,align", local=true)
reverse==true ? last.reverse() : last

return audiodubex(last, clp)
}

function timer_cut(clip clp,
\ string "mode", int "start", int "end", bool "reverse", bool "showstart",
\ bool "showend", int "color", float "size", int "x", int "y", int "align")
{
mode = default(mode, "hmmss")
reverse = default(reverse, false)
showstart = default(showstart, false)
showend = default(showend, false)
color = default(color, $ffffff)

isint(start)==false || isint(end)==false ? eval("""
start = round(clp.framecount / 3)
end = 2*round(clp.framecount / 3)
clp = clp.subtitle("No value set for parameter START and/or END!", align=2, size=round(clp.width * 0.05))
""") : nop()

finaltime = round(((end-start) * 1000) / clp.framerate)
zero = reverse==true ? formattime(finaltime, mode) : formattime(0, mode)
time = reverse==true ? formattime(0, mode) : formattime(finaltime, mode)
zero = showstart==false ? "" : zero
time = showend==false ? "" : time

clp.trim2(0, length=start).subtitle(zero, text_color=color, size=size, x=x, y=y, align=align)++\
clp.trim(start, end).timer(mode=mode, reverse=reverse, color=color, size=size, x=x, y=y, align=align)++\
clp.trim2(end+1, clp.framecount).subtitle(time, text_color=color, size=size, x=x, y=y, align=align)

return last
}

Hope you like it.

Gavino
13th July 2013, 19:56
Hi djcj, welcome to the forum and thanks for your initial contributions.
I have a couple of comments on your timer functions.

I see you use global variables to pass function parameter values into ScriptClip.
This is undesirable as it prevents the function being used more than once (with different arguments) in the same script.
A better way is to use the 'args' parameter of the extended version of ScriptClip from the GRunT plugin.
I would also make the mode an explicit parameter of function formattime.

Because of the way Trim() is used, function timer_cut will not work properly if start is 0 or 1, or if end=framecount-1.
(See stickboy's classic post Caveats of using Trim in functions.)
This is easier to fix in Avisynth 2.60 as you can use the new length argument of Trim().

djcj
14th July 2013, 11:19
I see you use global variables to pass function parameter values into ScriptClip.
This is undesirable as it prevents the function being used more than once (with different arguments) in the same script.
A better way is to use the 'args' parameter of the extended version of ScriptClip from the GRunT plugin.
Thanks! It's fixed. Your plugin is super awesome. ;)

I would also make the mode an explicit parameter of function formattime.
Fixed.

Because of the way Trim() is used, function timer_cut will not work properly if start is 0 or 1, or if end=framecount-1.
(See stickboy's classic post Caveats of using Trim in functions.)
This is easier to fix in Avisynth 2.60 as you can use the new length argument of Trim().
Fixed by using stickboy's Trim2 function from his jdl-util.avsi script: http://avisynth.org/stickboy/jdl-util.avsi

kedautinh12
8th September 2021, 18:02
Thanks! It's fixed. Your plugin is super awesome. ;)


Fixed.


Fixed by using stickboy's Trim2 function from his jdl-util.avsi script: http://avisynth.org/stickboy/jdl-util.avsi

Link broken, can anyone have modified in 2013???

VoodooFX
8th September 2021, 18:10
Link broken, can anyone have modified in 2013???

http://www.avisynth.nl/users/stickboy/jdl-util.avsi

kedautinh12
8th September 2021, 18:16
http://www.avisynth.nl/users/stickboy/jdl-util.avsi

It's 2008 ver and I got. I seen him last modified 2013 fixed trim2 function

VoodooFX
8th September 2021, 18:21
It's 2008 ver and I got. I seen him last modified 2013 fixed trim2 function
What you mean? Those are two different scripts from different authors.

PS:
Maybe language barrier... let's rephrase:
djcj fixed his script by using function from stickboy's script, he didn't modified stickboy's script.

kedautinh12
9th September 2021, 00:03
What you mean? Those are two different scripts from different authors.

PS:
Maybe language barrier... let's rephrase:
djcj fixed his script by using function from stickboy's script, he didn't modified stickboy's script.

Yeah, fixed in 2013 but your link script was mod in 2008. I think it's old version

Reel.Deel
9th September 2021, 00:55
@kedautinh12

You're not understanding what VoodooFX said.... stickboy's jdl-util WAS NOT updated by djcj. The last time jdl-util was updated was in 2008. There is not an updated version.

djcj updated HIS Timer() script in 2013. He did not update stickboy's jdl-util script.

kedautinh12
9th September 2021, 01:36
@kedautinh12

You're not understanding what VoodooFX said.... stickboy's jdl-util WAS NOT updated by djcj. The last time jdl-util was updated was in 2008. There is not an updated version.

djcj updated HIS Timer() script in 2013. He did not update stickboy's jdl-util script.

Oh, my mistake, sr