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)
34 return m_displayedFramesPerSecond;
38 int Timecode::getFrameCount(const QString duration, double fps) const
41 // calculate how many frames need to be dropped every minute.
43 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
45 int perMinute = toDrop / 9;
46 int tenthMinute = toDrop % 9;
48 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
49 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
50 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
52 // Number of actual frames in a 10 minute interval :
53 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
54 frames = 6 * duration.section(':', 0, 0).toInt() * tenMinutes;
55 int minutes = duration.section(':', 1, 1).toInt();
56 frames += ((int) minutes / 10) * tenMinutes;
57 int mins = minutes % 10;
59 frames += tenthMinuteFrames;
61 if (mins > 0) frames += mins * normalMinuteFrames;
63 if (minutes % 10 > 0) frames -= perMinute;
64 frames += duration.section(':', 2, 2).toInt() * m_displayedFramesPerSecond + duration.section(':', 3, 3).toInt();
67 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());
70 QString Timecode::getTimecode(const GenTime & time, double fps) const
74 return getTimecodeHH_MM_SS_FF(time, fps);
77 return getTimecodeHH_MM_SS_HH(time);
80 return getTimecodeFrames(time, fps);
83 return getTimecodeSeconds(time);
87 "Unknown timecode format specified, defaulting to HH_MM_SS_FF"
89 return getTimecodeHH_MM_SS_FF(time, fps);
93 QString Timecode::getTimecodeFromFrames(int frames)
95 return getTimecodeHH_MM_SS_FF(frames);
99 QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
101 // Returns the timecode in an easily read display, like 3 min. 5 sec.
102 int frames = (int)time.frames(fps);
103 int seconds = frames / (int) floor(fps + 0.5);
104 frames = frames % ((int) fps);
106 int minutes = seconds / 60;
107 seconds = seconds % 60;
108 int hours = minutes / 60;
109 minutes = minutes % 60;
115 text.append(QString::number(hours).rightJustified(2, '0', false));
116 text.append(' ' + i18n("hour") + ' ');
119 if (minutes != 0 || trim) {
121 text.append(QString::number(minutes));
123 text.append(QString::number(minutes).rightJustified(2, '0', false));
124 text.append(' ' + i18n("min.") + ' ');
127 if (seconds != 0 || trim) {
129 text.append(QString::number(seconds));
131 text.append(QString::number(seconds).rightJustified(2, '0', false));
132 text.append(' ' + i18n("sec."));
136 text.append(QString::number(frames));
137 text.append(' ' + i18n("frames"));
144 QString Timecode::getTimecodeHH_MM_SS_FF(const GenTime & time, double fps) const
147 return getTimecodeDropFrame(time, fps);
149 return getTimecodeHH_MM_SS_FF((int)time.frames(fps));
152 QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
154 int seconds = frames / m_displayedFramesPerSecond;
155 frames = frames % m_displayedFramesPerSecond;
157 int minutes = seconds / 60;
158 seconds = seconds % 60;
159 int hours = minutes / 60;
160 minutes = minutes % 60;
164 text.append(QString::number(hours).rightJustified(2, '0', false));
166 text.append(QString::number(minutes).rightJustified(2, '0', false));
168 text.append(QString::number(seconds).rightJustified(2, '0', false));
170 text.append(QString::number(frames).rightJustified(2, '0', false));
175 QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
177 int hundredths = (int)(time.seconds() * 100);
178 int seconds = hundredths / 100;
179 hundredths = hundredths % 100;
180 int minutes = seconds / 60;
181 seconds = seconds % 60;
182 int hours = minutes / 60;
183 minutes = minutes % 60;
187 text.append(QString::number(hours).rightJustified(2, '0', false));
189 text.append(QString::number(minutes).rightJustified(2, '0', false));
191 text.append(QString::number(seconds).rightJustified(2, '0', false));
193 text.append(QString::number(hundredths).rightJustified(2, '0', false));
198 QString Timecode::getTimecodeFrames(const GenTime & time, double fps) const
200 return QString::number(time.frames(fps));
203 QString Timecode::getTimecodeSeconds(const GenTime & time) const
205 return QString::number(time.seconds());
208 QString Timecode::getTimecodeDropFrame(const GenTime & time, double fps) const
210 // Calculate the timecode using dropframes to remove the difference in fps. Note that this algorithm should work
211 // for NTSC times, but is untested for any others - it is in no way an "official" algorithm, unless it's by fluke.
212 int frames = (int)time.frames(fps);
214 // calculate how many frames need to be dropped every minute.
215 int toDrop = (int) floor(600.0 * (m_displayedFramesPerSecond - fps) + 0.5);
217 int perMinute = toDrop / 9;
218 int tenthMinute = toDrop % 9;
220 // calculate how many frames are in a normal minute, and how many are in a tenth minute.
221 int normalMinuteFrames = (m_displayedFramesPerSecond * 60) - perMinute;
222 int tenthMinuteFrames = (m_displayedFramesPerSecond * 60) - tenthMinute;;
224 // Number of actual frames in a 10 minute interval :
225 int tenMinutes = (normalMinuteFrames * 9) + tenthMinuteFrames;
227 int tenMinuteIntervals = frames / tenMinutes;
228 frames = frames % tenMinutes;
230 int hours = tenMinuteIntervals / 6;
231 tenMinuteIntervals = tenMinuteIntervals % 6;
233 // At the point, we have figured out HH:M?:??:??
237 if (frames < tenthMinuteFrames) {
238 // tenth minute logic applies.
241 // normal minute logic applies.
242 numMinutes = 1 + (frames - tenthMinuteFrames) / normalMinuteFrames;
243 frames = (frames - tenthMinuteFrames) % normalMinuteFrames;
244 frames += tenthMinute + perMinute;
246 // We now have HH:MM:??:??
248 int seconds = frames / m_displayedFramesPerSecond;
249 frames = frames % m_displayedFramesPerSecond;
251 // We now have HH:MM:SS:FF
253 // THANK FUCK FOR THAT.
256 text.append(QString::number(hours).rightJustified(2, '0', false));
258 text.append(QString::number(tenMinuteIntervals));
259 text.append(QString::number(numMinutes));
261 text.append(QString::number(seconds).rightJustified(2, '0', false));
263 text.append(QString::number(frames).rightJustified(2, '0', false));