Eine C# Implementierung des Timecode welcher bei der Videobearbeitung verwendet wird.
Methoden:
- ToString: Gibt den Timecode im Format HH:MM:SS:FF aus
- FromMilliseconds: Erstellt eine Timecode Instanz aus der Anzahl der Millisekunden und den FPS
- FromTimeSpan: Erstellt eine Timecode Instanz aus einem Timespan objekt und den FPS
- CalculateDuration: Berechnet die Dauer aus TcIn und TcOut
- Parse: Erstellt eine MediaTimeCode Instanz aus einem String im Format HH:MM:SS:FF
public struct MediaTimeCode {
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public int Frames { get; set; }
public static MediaTimeCode FromMilliseconds(long milliseconds, int framesPerSecond) {
TimeSpan timespan = TimeSpan.FromMilliseconds(milliseconds);
return FromTimeSpan(timespan, framesPerSecond);
}
public static MediaTimeCode FromTimeSpan(TimeSpan timespan, int framesPerSecond) {
double framesPerMillisecond = (double)framesPerSecond / (double)1000;
int frames = (int)(timespan.Milliseconds * framesPerMillisecond);
return new MediaTimeCode() {
Hours = timespan.Hours,
Minutes = timespan.Minutes,
Seconds = timespan.Seconds,
Frames = frames
};
}
public static MediaTimeCode CalculateDuration(MediaTimeCode tcIn, MediaTimeCode tcOut, int fps) {
double fpm = 60 * fps;
double fph = 60 * fpm;
double frameHours = tcIn.Hours * fph;
double frameMinutes = tcIn.Minutes * fpm;
double frameSeconds = tcIn.Seconds * fps;
double tcInFrames = frameHours + frameMinutes + frameSeconds + tcIn.Frames;
frameHours = tcOut.Hours * fph;
frameMinutes = tcOut.Minutes * fpm;
frameSeconds = tcOut.Seconds * fps;
double tcOutFrames = frameHours + frameMinutes + frameSeconds + tcOut.Frames;
double durationInFrames = tcOutFrames - tcInFrames;
int fRest = (int)durationInFrames % (int)fps;
int frames = (int)durationInFrames - fRest;
int s = frames / (int)fps;
int sRest = s % 60;
int minutes = ((s - sRest) / 60) % 60;
int hours = (100 - minutes) / 60;
return new MediaTimeCode() {
Hours = hours,
Minutes = minutes,
Seconds = sRest,
Frames = fRest
};
}
public override string ToString() {
return string.Format("{0:00}:{1:00}:{2:00}:{3:00}",
Hours,
Minutes,
Seconds,
Frames);
}
public static MediaTimeCode Parse(string tcString) {
string[] tcParts = tcString.Split(':');
int hours = int.Parse(tcParts[0]);
int minutes = int.Parse(tcParts[1]);
int seconds = int.Parse(tcParts[2]);
int frames = int.Parse(tcParts[3]);
return new MediaTimeCode() {
Hours = hours,
Minutes = minutes,
Seconds = seconds,
Frames = frames
};
}
}
Schlagwörter: Video Timecode, TcIn, TcOut, Struct