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)
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)
96 return getTimecodeHH_MM_SS_FF(frames);
100 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
102 // Returns the timecode in an easily read display, like 3 min. 5 sec.
103 int frames = (int)time.frames(fps);
104 int seconds = frames / (int) floor(fps + 0.5);
105 frames = frames % ((int) fps);
107 int minutes = seconds / 60;
108 seconds = seconds % 60;
109 int hours = minutes / 60;
110 minutes = minutes % 60;
116 text.append(QString::number(hours).rightJustified(2, '0', false));
117 text.append(' ' + i18n("hour") + ' ');
120 if (minutes != 0 || trim) {
122 text.append(QString::number(minutes));
124 text.append(QString::number(minutes).rightJustified(2, '0', false));
125 text.append(' ' + i18n("min.") + ' ');
128 if (seconds != 0 || trim) {
130 text.append(QString::number(seconds));
132 text.append(QString::number(seconds).rightJustified(2, '0', false));
133 text.append(' ' + i18n("sec."));
137 text.append(QString::number(frames));
138 text.append(' ' + i18n("frames"));
145 QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time, double fps) const
148 return getTimecodeDropFrame(time, fps);
150 return getTimecodeHH_MM_SS_FF((int)time.frames(fps));
153 QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
155 int seconds = frames / m_displayedFramesPerSecond;
156 frames = frames % m_displayedFramesPerSecond;
158 int minutes = seconds / 60;
159 seconds = seconds % 60;
160 int hours = minutes / 60;
161 minutes = minutes % 60;
165 text.append(QString::number(hours).rightJustified(2, '0', false));
167 text.append(QString::number(minutes).rightJustified(2, '0', false));
169 text.append(QString::number(seconds).rightJustified(2, '0', false));
171 text.append(QString::number(frames).rightJustified(2, '0', false));
176 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
178 int hundredths = (int)(time.seconds() * 100);
179 int seconds = hundredths / 100;
180 hundredths = hundredths % 100;
181 int minutes = seconds / 60;
182 seconds = seconds % 60;
183 int hours = minutes / 60;
184 minutes = minutes % 60;
188 text.append(QString::number(hours).rightJustified(2, '0', false));
190 text.append(QString::number(minutes).rightJustified(2, '0', false));
192 text.append(QString::number(seconds).rightJustified(2, '0', false));
194 text.append(QString::number(hundredths).rightJustified(2, '0', false));
199 QString Timecode::getTimecodeFrames(const GenTime & time, double fps) const
201 return QString::number(time.frames(fps));
204 QString Timecode::getTimecodeSeconds(const GenTime & time) const
206 return QString::number(time.seconds());
209 QString Timecode::getTimecodeDropFrame(const GenTime & time, double fps) const
211 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
212 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
213 int frames = (int)time.frames(fps);
215 // calculate how many frames need to be dropped every minute.
216 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
218 int perMinute = toDrop / 9;
219 int tenthMinute = toDrop % 9;
221 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
222 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
223 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
225 // Number of actual frames in a 10 minute interval :
226 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
228 int tenMinuteIntervals = frames / tenMinutes;
229 frames = frames % tenMinutes;
231 int hours = tenMinuteIntervals / 6;
232 tenMinuteIntervals = tenMinuteIntervals % 6;
234 // At the point, we have figured out HH:M?:??:??
238 if (frames < tenthMinuteFrames) {
239 // tenth minute logic applies.
242 // normal minute logic applies.
243 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
244 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
245 frames += tenthMinute + perMinute;
247 // We now have HH:MM:??:??
249 int seconds = frames / m_displayedFramesPerSecond;
250 frames = frames % m_displayedFramesPerSecond;
252 // We now have HH:MM:SS:FF
254 // THANK FUCK FOR THAT.
257 text.append(QString::number(hours).rightJustified(2, '0', false));
259 text.append(QString::number(tenMinuteIntervals));
260 text.append(QString::number(numMinutes));
262 text.append(QString::number(seconds).rightJustified(2, '0', false));
264 text.append(QString::number(frames).rightJustified(2, '0', false));