1 /***************************************************************************
4 begin : Wed Dec 17 2003
5 copyright : (C) 2003 by Jason Wood
6 email : jasonwood@blueyonder.co.uk
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
22 Timecode::Timecode(Formats format, int framesPerSecond,
23 bool dropFrame): m_format(format), m_dropFrame(dropFrame),
24 m_displayedFramesPerSecond(framesPerSecond) {
27 Timecode::~Timecode() {
31 int Timecode::getFrameCount(const QString duration, double fps) const {
33 // calculate how many frames need to be dropped every minute.
35 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
37 int perMinute = toDrop / 9;
38 int tenthMinute = toDrop % 9;
40 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
41 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
42 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
44 // Number of actual frames in a 10 minute interval :
45 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
46 frames = 6 * duration.section(":", 0, 0).toInt() * tenMinutes;
47 int minutes = duration.section(":", 1, 1).toInt();
48 frames += ((int) minutes / 10) * tenMinutes;
49 int mins = minutes % 10;
51 frames += tenthMinuteFrames;
53 if (mins > 0) frames += mins * normalMinuteFrames;
55 if (minutes % 10 > 0) frames -= perMinute;
56 frames += duration.section(":", 2, 2).toInt() * m_displayedFramesPerSecond + duration.section(":", 3, 3).toInt();
59 return (int)((duration.section(":", 0, 0).toInt()*3600.0 + duration.section(":", 1, 1).toInt()*60.0 + duration.section(":", 2, 2).toInt()) * fps + duration.section(":", 3, 3).toInt());
62 QString Timecode::getTimecode(const GenTime & time, double fps) const {
65 return getTimecodeHH_MM_SS_FF(time, fps);
68 return getTimecodeHH_MM_SS_HH(time);
71 return getTimecodeFrames(time, fps);
74 return getTimecodeSeconds(time);
78 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
80 return getTimecodeHH_MM_SS_FF(time, fps);
84 QString Timecode::getTimecodeFromFrames(int frames) {
85 return getTimecodeHH_MM_SS_FF(frames);
89 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps) {
90 // Returns the timecode in an easily read display, like 3 min. 5 sec.
91 int frames = (int)time.frames(fps);
92 int seconds = frames / (int) floor(fps + 0.5);
93 frames = frames % ((int) fps);
95 int minutes = seconds / 60;
96 seconds = seconds % 60;
97 int hours = minutes / 60;
98 minutes = minutes % 60;
104 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
105 text.append(" " + i18n("hour") + " ");
108 if (minutes != 0 || trim) {
110 text.append(QString::number(minutes));
112 text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
113 text.append(" " + i18n("min.") + " ");
116 if (seconds != 0 || trim) {
118 text.append(QString::number(seconds));
120 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
121 text.append(" " + i18n("sec."));
125 text.append(QString::number(frames));
126 text.append(" " + i18n("frames"));
133 QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time, double fps) const {
135 return getTimecodeDropFrame(time, fps);
137 return getTimecodeHH_MM_SS_FF((int)time.frames(fps));
140 QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const {
141 int seconds = frames / m_displayedFramesPerSecond;
142 frames = frames % m_displayedFramesPerSecond;
144 int minutes = seconds / 60;
145 seconds = seconds % 60;
146 int hours = minutes / 60;
147 minutes = minutes % 60;
151 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
153 text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
155 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
157 text.append(QString::number(frames).rightJustified(2, '0', FALSE));
162 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const {
163 int hundredths = (int)(time.seconds() * 100);
164 int seconds = hundredths / 100;
165 hundredths = hundredths % 100;
166 int minutes = seconds / 60;
167 seconds = seconds % 60;
168 int hours = minutes / 60;
169 minutes = minutes % 60;
173 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
175 text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
177 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
179 text.append(QString::number(hundredths).rightJustified(2, '0', FALSE));
184 QString Timecode::getTimecodeFrames(const GenTime & time, double fps) const {
185 return QString::number(time.frames(fps));
188 QString Timecode::getTimecodeSeconds(const GenTime & time) const {
189 return QString::number(time.seconds());
192 QString Timecode::getTimecodeDropFrame(const GenTime & time, double fps) const {
193 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
194 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
195 int frames = (int)time.frames(fps);
197 // calculate how many frames need to be dropped every minute.
198 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
200 int perMinute = toDrop / 9;
201 int tenthMinute = toDrop % 9;
203 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
204 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
205 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
207 // Number of actual frames in a 10 minute interval :
208 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
210 int tenMinuteIntervals = frames / tenMinutes;
211 frames = frames % tenMinutes;
213 int hours = tenMinuteIntervals / 6;
214 tenMinuteIntervals = tenMinuteIntervals % 6;
216 // At the point, we have figured out HH:M?:??:??
220 if (frames < tenthMinuteFrames) {
221 // tenth minute logic applies.
224 // normal minute logic applies.
225 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
226 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
227 frames += tenthMinute + perMinute;
229 // We now have HH:MM:??:??
231 int seconds = frames / m_displayedFramesPerSecond;
232 frames = frames % m_displayedFramesPerSecond;
234 // We now have HH:MM:SS:FF
236 // THANK FUCK FOR THAT.
239 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
241 text.append(QString::number(tenMinuteIntervals));
242 text.append(QString::number(numMinutes));
244 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
246 text.append(QString::number(frames).rightJustified(2, '0', FALSE));