Skip to content

Commit 7755f31

Browse files
authored
Add implementation of to_nearest_timecode (#1717)
Add implementation of to_nearest_timecode which makes approximate conversion of timecode from a rate using the closest valid time code rate. Signed-off-by: Anton Marini <vade@vade.info>
1 parent 1ad891d commit 7755f31

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

src/opentime/rationalTime.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,26 @@ RationalTime::to_timecode(
570570
frames);
571571
}
572572

573+
std::string
574+
RationalTime::to_nearest_timecode(
575+
double rate,
576+
IsDropFrameRate drop_frame,
577+
ErrorStatus* error_status) const
578+
{
579+
std::string result = to_timecode(rate, drop_frame, error_status);
580+
581+
if (error_status)
582+
{
583+
*error_status = ErrorStatus();
584+
585+
double nearest_rate = nearest_valid_timecode_rate(rate);
586+
587+
return to_timecode(nearest_rate, drop_frame, error_status);
588+
}
589+
590+
return result;
591+
}
592+
573593
std::string
574594
RationalTime::to_time_string() const
575595
{

src/opentime/rationalTime.h

+11
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ class RationalTime
190190
return to_timecode(_rate, IsDropFrameRate::InferFromRate, error_status);
191191
}
192192

193+
std::string to_nearest_timecode(
194+
double rate,
195+
IsDropFrameRate drop_frame,
196+
ErrorStatus* error_status = nullptr) const;
197+
198+
std::string to_nearest_timecode(ErrorStatus* error_status = nullptr) const
199+
{
200+
return to_nearest_timecode(_rate, IsDropFrameRate::InferFromRate, error_status);
201+
}
202+
203+
193204
// produce a string in the form
194205
// hours:minutes:seconds
195206
// which may have a leading negative sign. seconds may have up to

src/py-opentimelineio/opentime-bindings/opentime_rationalTime.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ For example, the duration of a clip from frame 10 to frame 15 is 6 frames. Resul
131131
IsDropFrameRate::InferFromRate,
132132
ErrorStatusConverter());
133133
})
134+
.def("to_nearest_timecode", [](RationalTime rt, double rate, std::optional<bool> drop_frame) {
135+
return rt.to_nearest_timecode(
136+
rate,
137+
df_enum_converter(drop_frame),
138+
ErrorStatusConverter()
139+
);
140+
}, "rate"_a, "drop_frame"_a, "Convert to nearest timecode (``HH:MM:SS;FRAME``)")
141+
.def("to_nearest_timecode", [](RationalTime rt, double rate) {
142+
return rt.to_nearest_timecode(
143+
rate,
144+
IsDropFrameRate::InferFromRate,
145+
ErrorStatusConverter()
146+
);
147+
}, "rate"_a)
148+
.def("to_nearest_timecode", [](RationalTime rt) {
149+
return rt.to_nearest_timecode(
150+
rt.rate(),
151+
IsDropFrameRate::InferFromRate,
152+
ErrorStatusConverter());
153+
})
134154
.def("to_time_string", &RationalTime::to_time_string)
135155
.def_static("from_timecode", [](std::string s, double rate) {
136156
return RationalTime::from_timecode(s, rate, ErrorStatusConverter());

src/py-opentimelineio/opentimelineio/opentime.py

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'from_time_string',
1717
'from_seconds',
1818
'to_timecode',
19+
'to_nearest_timecode',
1920
'to_frames',
2021
'to_seconds',
2122
'to_time_string',
@@ -47,6 +48,15 @@ def to_timecode(rt, rate=None, drop_frame=None):
4748
)
4849

4950

51+
def to_nearest_timecode(rt, rate=None, drop_frame=None):
52+
"""Convert a :class:`~RationalTime` into a timecode string."""
53+
return (
54+
rt.to_nearest_timecode()
55+
if rate is None and drop_frame is None
56+
else rt.to_nearest_timecode(rate, drop_frame)
57+
)
58+
59+
5060
def to_frames(rt, rate=None):
5161
"""Turn a :class:`~RationalTime` into a frame number."""
5262
return rt.to_frames() if rate is None else rt.to_frames(rate)

0 commit comments

Comments
 (0)