Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    193
    Thanks
    50
    Thanked 0 Times in 0 Posts

    Scripts for calculating dropframe video timecode

    Hi All,

    I didn't see anything like this on the site and I thought this might be useful for anyone developing for the television industry. There are 2 functions. CnvToFrames takes a text input (formatted as HH:MM:SS:FF) timecode address and converts it to a total count of frames equal to the address. It is for dropframe TC but the code should be easy to modify for NonDrop or PAL or any other framerate. CnvToTime does the reverse. I found this useful for, among other things performing calculations on timecode, and reformatting the results as readable text. e.g.

    CnvToFrames(end of segment) - ConvToFrames(start of segment) = segment length

    then

    CnvToTime(segment length)

    Hope this can be of some use.

    Regards,

    Ken

    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    
    function CnvToTime(FrameCount)
    {
    var FRate = 29.97;
    var TotalSecs = parseInt(FrameCount / FRate, 10);
    var hours = parseInt(TotalSecs / 3600, 10);
    var mins = parseInt((TotalSecs - hours * 3600) / 60, 10);
    var secs = TotalSecs - hours * 3600 - mins * 60;
    var frames = (FrameCount - TotalSecs * 30);
    var dropframes = (hours * 108) + ((mins - parseInt(mins / 10, 10)) * 2);
    
    if ((frames + dropframes) > 30) {secs =  secs + 1; frames = "1";}
    else if ((frames + dropframes) == 30) {secs = secs + 1; frames = "0";}
    else {frames = frames + dropframes;}
    
    //INSERT FOR EACH LOOP HERE TO FORMAT TIMECODE ARRAY
    
    var i;
    var timecode = new Array();
    timecode[0] = hours;
    timecode[1] = mins;
    timecode[2] = secs;
    timecode[3] = frames
    
    for (i=0;i<timecode.length;i++)
    {if(timecode[i] < 10) timecode[i] = "0" + timecode[i];}
    
    return timecode.join(':');
    }
    
    function CnvToFrames(timecode)
    {
    var FRate = 30;
    var hours = parseInt(timecode.slice(0,2),10);
    var mins = parseInt(timecode.slice(3,5),10);
    var secs = parseInt(timecode.slice(6,8),10);
    var frames = parseInt(timecode.slice(9,11),10);
    var dropframes = (hours * 108) + (mins * 2) - (parseInt(timecode.slice(3,4),10) * 2);
    
    var framecount = (hours * 3600 * FRate) + (mins * 60 * FRate) + (secs * FRate) + frames - dropframes;
    
    return framecount;
    }
    
    </script>
    
    </body>
    </html>
    Last edited by MaDmiX; 02-19-2012 at 02:51 PM. Reason: Corrected an error in code


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •