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)
32 int Timecode::getFrameNumber(const QString duration, double fps) const
35 // calculate how many frames need to be dropped every minute.
37 int toDrop = (int) floor (600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
39 int perMinute = toDrop / 9;
40 int tenthMinute = toDrop % 9;
42 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
43 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
44 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
46 // Number of actual frames in a 10 minute interval :
47 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
48 frames = 6 * duration.section(":",0,0).toInt() * tenMinutes;
49 int minutes = duration.section(":",1,1).toInt();
50 frames += ((int) minutes / 10) * tenMinutes;
51 int mins = minutes % 10;
53 frames += tenthMinuteFrames;
55 if (mins > 0) frames += mins * normalMinuteFrames;
57 if (minutes % 10 > 0) frames -= perMinute;
58 frames += duration.section(":",2,2).toInt() * m_displayedFramesPerSecond + duration.section(":",3,3).toInt();
61 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());
64 QString Timecode::getTimecode(const GenTime & time, double fps) const
68 return getTimecodeHH_MM_SS_FF(time, fps);
71 return getTimecodeHH_MM_SS_HH(time);
74 return getTimecodeFrames(time, fps);
77 return getTimecodeSeconds(time);
81 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
83 return getTimecodeHH_MM_SS_FF(time, fps);
87 QString Timecode::getTimecodeFromFrames(int frames)
89 return getTimecodeHH_MM_SS_FF(frames);
93 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
95 // Returns the timecode in an easily read display, like 3 min. 5 sec.
96 int frames = (int)time.frames(fps);
97 int seconds = frames / (int) floor(fps + 0.5);
98 frames = frames % ((int) fps);
100 int minutes = seconds / 60;
101 seconds = seconds % 60;
102 int hours = minutes / 60;
103 minutes = minutes % 60;
109 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
110 text.append(" " + i18n("hour") + " ");
113 if (minutes!= 0 || trim) {
115 text.append(QString::number(minutes));
118 text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
119 text.append(" " + i18n("min.") + " ");
122 if (seconds!= 0 || trim) {
124 text.append(QString::number(seconds));
127 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
128 text.append(" " + i18n("sec."));
132 text.append(QString::number(frames));
133 text.append(" " + i18n("frames"));
140 QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time, double fps) const
143 return getTimecodeDropFrame(time, fps);
145 return getTimecodeHH_MM_SS_FF((int)time.frames(fps));
148 QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
150 int seconds = frames / m_displayedFramesPerSecond;
151 frames = frames % m_displayedFramesPerSecond;
153 int minutes = seconds / 60;
154 seconds = seconds % 60;
155 int hours = minutes / 60;
156 minutes = minutes % 60;
160 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
162 text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
164 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
166 text.append(QString::number(frames).rightJustified(2, '0', FALSE));
171 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
173 int hundredths = (int)(time.seconds() * 100);
174 int seconds = hundredths / 100;
175 hundredths = hundredths % 100;
176 int minutes = seconds / 60;
177 seconds = seconds % 60;
178 int hours = minutes / 60;
179 minutes = minutes % 60;
183 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
185 text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
187 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
189 text.append(QString::number(hundredths).rightJustified(2, '0', FALSE));
194 QString Timecode::getTimecodeFrames(const GenTime & time, double fps) const
196 return QString::number(time.frames(fps));
199 QString Timecode::getTimecodeSeconds(const GenTime & time) const
201 return QString::number(time.seconds());
204 QString Timecode::getTimecodeDropFrame(const GenTime & time, double fps) const
206 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
207 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
208 int frames = (int)time.frames(fps);
210 // calculate how many frames need to be dropped every minute.
211 int toDrop = (int) floor (600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
213 int perMinute = toDrop / 9;
214 int tenthMinute = toDrop % 9;
216 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
217 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
218 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
220 // Number of actual frames in a 10 minute interval :
221 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
223 int tenMinuteIntervals = frames / tenMinutes;
224 frames = frames % tenMinutes;
226 int hours = tenMinuteIntervals / 6;
227 tenMinuteIntervals = tenMinuteIntervals % 6;
229 // At the point, we have figured out HH:M?:??:??
233 if (frames < tenthMinuteFrames) {
234 // tenth minute logic applies.
237 // normal minute logic applies.
238 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
239 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
240 frames += tenthMinute + perMinute;
242 // We now have HH:MM:??:??
244 int seconds = frames / m_displayedFramesPerSecond;
245 frames = frames % m_displayedFramesPerSecond;
247 // We now have HH:MM:SS:FF
249 // THANK FUCK FOR THAT.
252 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
254 text.append(QString::number(tenMinuteIntervals));
255 text.append(QString::number(numMinutes));
257 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
259 text.append(QString::number(frames).rightJustified(2, '0', FALSE));