비디오의 프레임 번호를 시간으로(HMSF) 변환하는 함수
아래 코드는 C# 코드이다.
Code From : .... (I'm sorry. lost original reference web site link)
public static string FramesToTimeCode(int framenumber, double framerate)
{
//CONVERT A FRAME NUMBER TO DROP FRAME TIMECODE
//Code by David Heidelberger, adapted from Andrew Duncan
//Given an int called framenumber and a double called framerate
//Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
int d;
int m;
int dropFrames = (int)Math.Round(framerate * (double)0.066666); //Number of frames to drop on the minute marks is the nearest integer to 6% of the framerate
int framesPerHour = (int)Math.Round(framerate * 60 * 60); //Number of frames in an hour
int framesPer24Hours = framesPerHour * 24; //Number of frames in a day - timecode rolls over after 24 hours
int framesPer10Minutes = (int)Math.Round(framerate * 60 * 10); //Number of frames per ten minutes
int framesPerMinute = (int)Math.Round(framerate) * 60 - dropFrames; //Number of frames per minute is the Math.Round of the framerate * 60 minus the number of dropped frames
if (framenumber < 0) //Negative time. Add 24 hours.
{
framenumber = framesPer24Hours + framenumber;
}
//If framenumber is greater than 24 hrs, next operation will rollover clock
framenumber = framenumber % framesPer24Hours; //% is the modulus operator, which returns a remainder. a % b = the remainder of a/b
d = framenumber / framesPer10Minutes; // \ means integer division, which is a/b without a remainder. Some languages you could use floor(a/b)
m = framenumber % framesPer10Minutes;
//In the original post, the next line read m>1, which only worked for 29.97. Jean-Baptiste Mardelle correctly pointed out that m should be compared to dropFrames.
if (m > dropFrames)
{
framenumber = framenumber + (dropFrames * 9 * d) + dropFrames * ((m - dropFrames) / framesPerMinute);
}
else
{
framenumber = framenumber + dropFrames * 9 * d;
}
int frRound = (int)Math.Round(framerate);
int frames = framenumber % frRound;
int seconds = (framenumber / frRound) % 60;
int minutes = ((framenumber / frRound) / 60) % 60;
int hours = (((framenumber / frRound) / 60) / 60);
return string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D2}", hours, minutes, seconds, frames);
}