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)
33 int Timecode::getFrameCount(const QString duration, double fps) const
36 // calculate how many frames need to be dropped every minute.
38 int toDrop = (int) floor (600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
40 int perMinute = toDrop / 9;
41 int tenthMinute = toDrop % 9;
43 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
44 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
45 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
47 // Number of actual frames in a 10 minute interval :
48 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
49 frames = 6 * duration.section(":",0,0).toInt() * tenMinutes;
50 int minutes = duration.section(":",1,1).toInt();
51 frames += ((int) minutes / 10) * tenMinutes;
52 int mins = minutes % 10;
54 frames += tenthMinuteFrames;
56 if (mins > 0) frames += mins * normalMinuteFrames;
58 if (minutes % 10 > 0) frames -= perMinute;
59 frames += duration.section(":",2,2).toInt() * m_displayedFramesPerSecond + duration.section(":",3,3).toInt();
62 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());
65 QString Timecode::getTimecode(const GenTime & time, double fps) const
69 return getTimecodeHH_MM_SS_FF(time, fps);
72 return getTimecodeHH_MM_SS_HH(time);
75 return getTimecodeFrames(time, fps);
78 return getTimecodeSeconds(time);
82 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
84 return getTimecodeHH_MM_SS_FF(time, fps);
88 QString Timecode::getTimecodeFromFrames(int frames)
90 return getTimecodeHH_MM_SS_FF(frames);
94 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
96 // Returns the timecode in an easily read display, like 3 min. 5 sec.
97 int frames = (int)time.frames(fps);
98 int seconds = frames / (int) floor(fps + 0.5);
99 frames = frames % ((int) fps);
101 int minutes = seconds / 60;
102 seconds = seconds % 60;
103 int hours = minutes / 60;
104 minutes = minutes % 60;
110 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
111 text.append(" " + i18n("hour") + " ");
114 if (minutes!= 0 || trim) {
116 text.append(QString::number(minutes));
119 text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
120 text.append(" " + i18n("min.") + " ");
123 if (seconds!= 0 || trim) {
125 text.append(QString::number(seconds));
128 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
129 text.append(" " + i18n("sec."));
133 text.append(QString::number(frames));
134 text.append(" " + i18n("frames"));
141 QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time, double fps) const
144 return getTimecodeDropFrame(time, fps);
146 return getTimecodeHH_MM_SS_FF((int)time.frames(fps));
149 QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
151 int seconds = frames / m_displayedFramesPerSecond;
152 frames = frames % m_displayedFramesPerSecond;
154 int minutes = seconds / 60;
155 seconds = seconds % 60;
156 int hours = minutes / 60;
157 minutes = minutes % 60;
161 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
163 text.append(QString::number(minutes).rightJustified(2, '0', FALSE));
165 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
167 text.append(QString::number(frames).rightJustified(2, '0', FALSE));
172 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
174 int hundredths = (int)(time.seconds() * 100);
175 int seconds = hundredths / 100;
176 hundredths = hundredths % 100;
177 int minutes = seconds / 60;
178 seconds = seconds % 60;
179 int hours = minutes / 60;
180 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(hundredths).rightJustified(2, '0', FALSE));
195 QString Timecode::getTimecodeFrames(const GenTime & time, double fps) const
197 return QString::number(time.frames(fps));
200 QString Timecode::getTimecodeSeconds(const GenTime & time) const
202 return QString::number(time.seconds());
205 QString Timecode::getTimecodeDropFrame(const GenTime & time, double fps) const
207 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
208 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
209 int frames = (int)time.frames(fps);
211 // calculate how many frames need to be dropped every minute.
212 int toDrop = (int) floor (600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
214 int perMinute = toDrop / 9;
215 int tenthMinute = toDrop % 9;
217 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
218 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
219 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
221 // Number of actual frames in a 10 minute interval :
222 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
224 int tenMinuteIntervals = frames / tenMinutes;
225 frames = frames % tenMinutes;
227 int hours = tenMinuteIntervals / 6;
228 tenMinuteIntervals = tenMinuteIntervals % 6;
230 // At the point, we have figured out HH:M?:??:??
234 if (frames < tenthMinuteFrames) {
235 // tenth minute logic applies.
238 // normal minute logic applies.
239 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
240 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
241 frames += tenthMinute + perMinute;
243 // We now have HH:MM:??:??
245 int seconds = frames / m_displayedFramesPerSecond;
246 frames = frames % m_displayedFramesPerSecond;
248 // We now have HH:MM:SS:FF
250 // THANK FUCK FOR THAT.
253 text.append(QString::number(hours).rightJustified(2, '0', FALSE));
255 text.append(QString::number(tenMinuteIntervals));
256 text.append(QString::number(numMinutes));
258 text.append(QString::number(seconds).rightJustified(2, '0', FALSE));
260 text.append(QString::number(frames).rightJustified(2, '0', FALSE));