View Full Version : timecode converter... may be useful to edit monkeys
Mug Funky
9th March 2005, 15:10
hiyah.
i'm doing some segment re-encodes in Maestro (gah!), and decided i was sick of poking at a calculator all day, so i wrote a little avisynth function to make things nicer.
it inputs a string with a 00:00:00:00 style timecode, and outputs an integer with the corresponding frame number. useful for trims.
function tc (string "timecode")
{
frames=value(rightstr(timecode,2))
secs=value(rightstr(timecode,5).leftstr(2))*25
mins=value(rightstr(timecode,8).leftstr(5))*60*25
hours=value(rightstr(timecode,11).leftstr(8))*60*60*25
return int(hours+mins+secs+frames)
}
it's pretty basic at the moment. i'd like to adapt it to take timecodes of a variety of formats (this'll bork if you feed it a timecode without hours on it). so far this only works on PAL. i might adapt it to adjust itself for whatever framerate the clip is, but i can't be bothered now.
basic usage:
tc("00:00:00:12") will return "12" as an integer.
to trim to two timecodes, dropping a frame at the end:
trim(tc("00:18:42:18"),tc("00:23:24:12")-1)
hope somebody finds this useful :)
joshbm
10th March 2005, 15:45
Yes, it is useful... Already created a function like that... Except auto-detects framerate. Check it out here (http://forum.doom9.org/showthread.php?s=&threadid=82852&highlight=TimeCode) :
function TimeCode(clip a, string timecode, float "fps")
{
fps=default(fps,a.framerate)
hours=round(int(Value(LeftStr(timecode,FindStr(timecode,":")-1)))*fps*60*60)
timecode=RightStr(timecode,StrLen(timecode)-FindStr(timecode,":"))
minutes=round(int(Value(LeftStr(timecode,FindStr(timecode,":")-1)))*fps*60)
timecode=RightStr(timecode,StrLen(timecode)-FindStr(timecode,":"))
seconds=round(int(Value(LeftStr(timecode,FindStr(timecode,":")-1)))*fps)
timecode=RightStr(timecode,StrLen(timecode)-FindStr(timecode,":"))
frames=int(Value(timecode))
return hours+minutes+seconds+frames
}
Use it just like your script:
TimeCode("00:00:05:00")
# will be 5 seconds no matter what fps it is. You can also change the fps.
I will probably adapt it so that something like:
TimeCode("05:00")
Will work as well, and some other adjustments, but I can't right at the very moment. I'll do it when I get home.
Regards,
joshbm
stickboy
10th March 2005, 18:53
Such functions probably should handle dropframe vs. non-dropframe timecodes too.
joshbm
11th March 2005, 06:15
function TimeCode(clip a, string timecode, float "fps")
{
fps=default(fps,a.framerate)
rev=RevStr(timecode)
frames=(FindStr(rev,":")>0) ?
\ int(Value(RevStr(LeftStr(rev,FindStr(rev,":")-1)))) : int(Value(RevStr(rev)))
rev=(FindStr(rev,":")>0) ? RightStr(rev,StrLen(rev)-FindStr(rev,":")) : ""
seconds=(FindStr(rev,":")>0) ?
\ int(Value(RevStr(LeftStr(rev,FindStr(rev,":")-1))))*fps : int(Value(RevStr(rev)))*fps
rev=(FindStr(rev,":")>0) ? RightStr(rev,StrLen(rev)-FindStr(rev,":")) : ""
minutes=(FindStr(rev,":")>0) ?
\ int(Value(RevStr(LeftStr(rev,FindStr(rev,":")-1))))*fps*60 : int(Value(RevStr(rev)))*fps*60
rev=(FindStr(rev,":")>0) ? RightStr(rev,StrLen(rev)-FindStr(rev,":")) : ""
hours=(FindStr(rev,":")>0) ?
\ int(Value(RevStr(LeftStr(rev,FindStr(rev,":")-1))))*fps*60*60 : int(Value(RevStr(rev)))*fps*60*60
rev=(FindStr(rev,":")>0) ? RightStr(rev,StrLen(rev)-FindStr(rev,":")) : ""
return round(frames+seconds+minutes)
}
function TrimTime(clip a, string b, string c)
{
return a.Trim(a.TimeCode(b),a.TimeCode(c))
}
Implemented everything.
TimeCode("2:00")
# is the same as
TimeCode("00:00:02:00")
#Adapts to the current fps, or can specify using the fps attribute.
TimeCode("52:35:21",fps=23.976)
#You can also Trim using the new TrimTime function.
TrimTime("2:00","55:05:11")
Mug Funky
13th March 2005, 13:49
@ stickboy: how exactly does drop-frame work? i've read a few sites that mention it, but haven't got a conclusive, foolproof way to handle it. it's a real pain in the buttock because if you're going NTSC to PAL and feeding the encoder app with PAL frames but non-drop timecode, everything cuts off short. usually it's enough to just add 10 seconds for a movie-length encode and cutting later, sometimes there's a need for a more accurate approach.
AFAIK it's something like "drop the first 2 frames of every minute except tens-of minutes in the bottom half of the hour" or something like that.
stupid NTSC :devil:
[edit]
thanks for the scripts, joshbm :)
Wilbert
13th March 2005, 18:12
"drop the first 2 frames of every minute except tens-of minutes"
Yup.
from text-overlay.cpp
int c;
if (dropframe) {
c = off_min + 60*off_our; // number of drop events
c -= c/10; // less non-drop events on 10 minutes
c *=2; // drop 2 frames per drop event
} else {
c = 0;
}
offset_f = off_f + rate*(off_sec + 60*off_min + 3600*off_our) - c;
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.