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, bool dropFrame) :
24 m_dropFrame(dropFrame),
25 m_displayedFramesPerSecond(framesPerSecond)
33 int Timecode::fps() const
35 return m_displayedFramesPerSecond;
39 int Timecode::getFrameCount(const QString duration, double fps) const
42 // calculate how many frames need to be dropped every minute.
44 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
46 int perMinute = toDrop / 9;
47 int tenthMinute = toDrop % 9;
49 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
50 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
51 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
53 // Number of actual frames in a 10 minute interval :
54 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
55 frames = 6 * duration.section(':', 0, 0).toInt() * tenMinutes;
56 int minutes = duration.section(':', 1, 1).toInt();
57 frames += ((int) minutes / 10) * tenMinutes;
58 int mins = minutes % 10;
60 frames += tenthMinuteFrames;
62 if (mins > 0) frames += mins * normalMinuteFrames;
64 if (minutes % 10 > 0) frames -= perMinute;
65 frames += duration.section(':', 2, 2).toInt() * m_displayedFramesPerSecond + duration.section(':', 3, 3).toInt();
68 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());
71 QString Timecode::getTimecode(const GenTime & time, double fps) const
75 return getTimecodeHH_MM_SS_FF(time, fps);
78 return getTimecodeHH_MM_SS_HH(time);
81 return getTimecodeFrames(time, fps);
84 return getTimecodeSeconds(time);
88 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
90 return getTimecodeHH_MM_SS_FF(time, fps);
94 QString Timecode::getTimecodeFromFrames(int frames) const
96 return getTimecodeHH_MM_SS_FF(frames);
101 QString Timecode::getStringTimecode(int frames, const double &fps)
103 // Returns the timecode in an hh:mm:ss format
104 int seconds = frames / (int) floor(fps + 0.5);
105 int minutes = seconds / 60;
106 seconds = seconds % 60;
107 int hours = minutes / 60;
108 minutes = minutes % 60;
110 text.append(QString::number(hours).rightJustified(2, '0', false));
112 text.append(QString::number(minutes).rightJustified(2, '0', false));
114 text.append(QString::number(seconds).rightJustified(2, '0', false));
120 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
122 // Returns the timecode in an easily read display, like 3 min. 5 sec.
123 int frames = (int)time.frames(fps);
124 int seconds = frames / (int) floor(fps + 0.5);
125 frames = frames % ((int) fps);
127 int minutes = seconds / 60;
128 seconds = seconds % 60;
129 int hours = minutes / 60;
130 minutes = minutes % 60;
136 text.append(QString::number(hours).rightJustified(2, '0', false));
137 text.append(' ' + i18n("hour") + ' ');
140 if (minutes != 0 || trim) {
142 text.append(QString::number(minutes));
144 text.append(QString::number(minutes).rightJustified(2, '0', false));
145 text.append(' ' + i18n("min.") + ' ');
148 if (seconds != 0 || trim) {
150 text.append(QString::number(seconds));
152 text.append(QString::number(seconds).rightJustified(2, '0', false));
153 text.append(' ' + i18n("sec."));
157 text.append(QString::number(frames));
158 text.append(' ' + i18n("frames"));
165 QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time, double fps) const
168 return getTimecodeDropFrame(time, fps);
170 return getTimecodeHH_MM_SS_FF((int)time.frames(fps));
173 QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
175 int seconds = frames / m_displayedFramesPerSecond;
176 frames = frames % m_displayedFramesPerSecond;
178 int minutes = seconds / 60;
179 seconds = seconds % 60;
180 int hours = minutes / 60;
181 minutes = minutes % 60;
184 text.append(QString::number(hours).rightJustified(2, '0', false));
186 text.append(QString::number(minutes).rightJustified(2, '0', false));
188 text.append(QString::number(seconds).rightJustified(2, '0', false));
190 text.append(QString::number(frames).rightJustified(2, '0', false));
195 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
197 int hundredths = (int)(time.seconds() * 100);
198 int seconds = hundredths / 100;
199 hundredths = hundredths % 100;
200 int minutes = seconds / 60;
201 seconds = seconds % 60;
202 int hours = minutes / 60;
203 minutes = minutes % 60;
207 text.append(QString::number(hours).rightJustified(2, '0', false));
209 text.append(QString::number(minutes).rightJustified(2, '0', false));
211 text.append(QString::number(seconds).rightJustified(2, '0', false));
213 text.append(QString::number(hundredths).rightJustified(2, '0', false));
218 QString Timecode::getTimecodeFrames(const GenTime & time, double fps) const
220 return QString::number(time.frames(fps));
223 QString Timecode::getTimecodeSeconds(const GenTime & time) const
225 return QString::number(time.seconds());
228 QString Timecode::getTimecodeDropFrame(const GenTime & time, double fps) const
230 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
231 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
232 int frames = (int)time.frames(fps);
234 // calculate how many frames need to be dropped every minute.
235 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
237 int perMinute = toDrop / 9;
238 int tenthMinute = toDrop % 9;
240 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
241 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
242 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
244 // Number of actual frames in a 10 minute interval :
245 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
247 int tenMinuteIntervals = frames / tenMinutes;
248 frames = frames % tenMinutes;
250 int hours = tenMinuteIntervals / 6;
251 tenMinuteIntervals = tenMinuteIntervals % 6;
253 // At the point, we have figured out HH:M?:??:??
257 if (frames < tenthMinuteFrames) {
258 // tenth minute logic applies.
261 // normal minute logic applies.
262 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
263 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
264 frames += tenthMinute + perMinute;
266 // We now have HH:MM:??:??
268 int seconds = frames / m_displayedFramesPerSecond;
269 frames = frames % m_displayedFramesPerSecond;
271 // We now have HH:MM:SS:FF
273 // THANK FUCK FOR THAT.
276 text.append(QString::number(hours).rightJustified(2, '0', false));
278 text.append(QString::number(tenMinuteIntervals));
279 text.append(QString::number(numMinutes));
281 text.append(QString::number(seconds).rightJustified(2, '0', false));
283 text.append(QString::number(frames).rightJustified(2, '0', false));