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, double framesPerSecond, bool dropFrame) :
24 m_dropFrame(dropFrame),
25 m_displayedFramesPerSecond(framesPerSecond + 0.5),
26 m_realFps(framesPerSecond)
34 int Timecode::fps() const
36 return m_displayedFramesPerSecond;
40 int Timecode::getFrameCount(const QString duration) const
43 // calculate how many frames need to be dropped every minute.
45 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - m_realFps) + 0.5);
47 int perMinute = toDrop / 9;
48 int tenthMinute = toDrop % 9;
50 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
51 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
52 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
54 // Number of actual frames in a 10 minute interval :
55 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
56 frames = 6 * duration.section(':', 0, 0).toInt() * tenMinutes;
57 int minutes = duration.section(':', 1, 1).toInt();
58 frames += ((int) minutes / 10) * tenMinutes;
59 int mins = minutes % 10;
61 frames += tenthMinuteFrames;
63 if (mins > 0) frames += mins * normalMinuteFrames;
65 if (minutes % 10 > 0) frames -= perMinute;
66 frames += duration.section(':', 2, 2).toInt() * m_displayedFramesPerSecond + duration.section(':', 3, 3).toInt();
69 return (int)((duration.section(':', 0, 0).toInt()*3600.0 + duration.section(':', 1, 1).toInt()*60.0 + duration.section(':', 2, 2).toInt()) * m_realFps + duration.section(':', 3, 3).toInt());
72 QString Timecode::getTimecode(const GenTime & time) const
76 return getTimecodeHH_MM_SS_FF(time);
79 return getTimecodeHH_MM_SS_HH(time);
82 return getTimecodeFrames(time);
85 return getTimecodeSeconds(time);
89 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
91 return getTimecodeHH_MM_SS_FF(time);
95 const QString Timecode::getTimecodeFromFrames(int frames) const
97 return getTimecodeHH_MM_SS_FF(frames);
102 QString Timecode::getStringTimecode(int frames, const double &fps)
104 // Returns the timecode in an hh:mm:ss format
105 int seconds = frames / (int) floor(fps + 0.5);
106 int minutes = seconds / 60;
107 seconds = seconds % 60;
108 int hours = minutes / 60;
109 minutes = minutes % 60;
111 text.append(QString::number(hours).rightJustified(2, '0', false));
113 text.append(QString::number(minutes).rightJustified(2, '0', false));
115 text.append(QString::number(seconds).rightJustified(2, '0', false));
121 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
123 // Returns the timecode in an easily read display, like 3 min. 5 sec.
124 int frames = (int)time.frames(fps);
125 int seconds = frames / (int) floor(fps + 0.5);
126 frames = frames % ((int) fps);
128 int minutes = seconds / 60;
129 seconds = seconds % 60;
130 int hours = minutes / 60;
131 minutes = minutes % 60;
137 text.append(QString::number(hours).rightJustified(2, '0', false));
138 text.append(' ' + i18n("hour") + ' ');
141 if (minutes != 0 || trim) {
143 text.append(QString::number(minutes));
145 text.append(QString::number(minutes).rightJustified(2, '0', false));
146 text.append(' ' + i18n("min.") + ' ');
149 if (seconds != 0 || trim) {
151 text.append(QString::number(seconds));
153 text.append(QString::number(seconds).rightJustified(2, '0', false));
154 text.append(' ' + i18n("sec."));
158 text.append(QString::number(frames));
159 text.append(' ' + i18n("frames"));
166 const QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time) const
169 return getTimecodeDropFrame(time);
171 return getTimecodeHH_MM_SS_FF((int) time.frames(m_realFps));
174 const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
177 return getTimecodeDropFrame(frames);
179 int seconds = frames / m_displayedFramesPerSecond;
180 frames = frames % m_displayedFramesPerSecond;
182 int minutes = seconds / 60;
183 seconds = seconds % 60;
184 int hours = minutes / 60;
185 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(frames).rightJustified(2, '0', false));
199 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
201 int hundredths = (int)(time.seconds() * 100);
202 int seconds = hundredths / 100;
203 hundredths = hundredths % 100;
204 int minutes = seconds / 60;
205 seconds = seconds % 60;
206 int hours = minutes / 60;
207 minutes = minutes % 60;
211 text.append(QString::number(hours).rightJustified(2, '0', false));
213 text.append(QString::number(minutes).rightJustified(2, '0', false));
215 text.append(QString::number(seconds).rightJustified(2, '0', false));
217 text.append(QString::number(hundredths).rightJustified(2, '0', false));
222 QString Timecode::getTimecodeFrames(const GenTime & time) const
224 return QString::number(time.frames(m_realFps));
227 QString Timecode::getTimecodeSeconds(const GenTime & time) const
229 return QString::number(time.seconds());
232 QString Timecode::getTimecodeDropFrame(const GenTime & time) const
234 return getTimecodeDropFrame((int)time.frames(m_realFps));
237 QString Timecode::getTimecodeDropFrame(int frames) const
239 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
240 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
242 // calculate how many frames need to be dropped every minute.
243 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - m_realFps) + 0.5);
245 int perMinute = toDrop / 9;
246 int tenthMinute = toDrop % 9;
248 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
249 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
250 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
252 // Number of actual frames in a 10 minute interval :
253 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
255 int tenMinuteIntervals = frames / tenMinutes;
256 frames = frames % tenMinutes;
258 int hours = tenMinuteIntervals / 6;
259 tenMinuteIntervals = tenMinuteIntervals % 6;
261 // At the point, we have figured out HH:M?:??:??
265 if (frames < tenthMinuteFrames) {
266 // tenth minute logic applies.
269 // normal minute logic applies.
270 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
271 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
272 frames += tenthMinute + perMinute;
274 // We now have HH:MM:??:??
276 int seconds = frames / m_displayedFramesPerSecond;
277 frames = frames % m_displayedFramesPerSecond;
279 // We now have HH:MM:SS:FF
281 // THANK FUCK FOR THAT.
284 text.append(QString::number(hours).rightJustified(2, '0', false));
286 text.append(QString::number(tenMinuteIntervals));
287 text.append(QString::number(numMinutes));
289 text.append(QString::number(seconds).rightJustified(2, '0', false));
291 text.append(QString::number(frames).rightJustified(2, '0', false));