sprocket-systems.coda.sdk 1.2.2__tar.gz → 1.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sprocket_systems_coda_sdk-1.2.2 → sprocket_systems_coda_sdk-1.3.1}/PKG-INFO +1 -1
- {sprocket_systems_coda_sdk-1.2.2 → sprocket_systems_coda_sdk-1.3.1}/pyproject.toml +1 -1
- {sprocket_systems_coda_sdk-1.2.2 → sprocket_systems_coda_sdk-1.3.1}/src/coda/__init__.py +1 -1
- {sprocket_systems_coda_sdk-1.2.2 → sprocket_systems_coda_sdk-1.3.1}/src/coda/sdk.py +8 -1
- sprocket_systems_coda_sdk-1.3.1/src/coda/tc_tools.py +531 -0
- sprocket_systems_coda_sdk-1.2.2/src/coda/tc_tools.py +0 -857
- {sprocket_systems_coda_sdk-1.2.2 → sprocket_systems_coda_sdk-1.3.1}/LICENSE +0 -0
- {sprocket_systems_coda_sdk-1.2.2 → sprocket_systems_coda_sdk-1.3.1}/PYPI_README.md +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sprocket-systems.coda.sdk
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.1
|
|
4
4
|
Summary: The Coda SDK provides a Python interface to define Coda workflows, create jobs and run them.
|
|
5
5
|
Keywords: python,coda,sdk
|
|
6
6
|
Author-Email: Sprocket Systems <support@sprocket.systems>
|
|
@@ -6,6 +6,7 @@ import requests
|
|
|
6
6
|
import copy
|
|
7
7
|
import shutil
|
|
8
8
|
import time
|
|
9
|
+
import urllib3
|
|
9
10
|
|
|
10
11
|
from .tc_tools import time_seconds_to_vid_frames,vid_frames_to_tc, tc_to_time_seconds
|
|
11
12
|
|
|
@@ -18,7 +19,13 @@ def make_request(func,port,route,payload=None):
|
|
|
18
19
|
auth = None
|
|
19
20
|
if os.getenv('CODA_API_TOKEN'):
|
|
20
21
|
auth= {'Authorization': f"Bearer {os.getenv('CODA_API_TOKEN')}"}
|
|
21
|
-
|
|
22
|
+
|
|
23
|
+
verify = True
|
|
24
|
+
if os.getenv('CODA_API_INSECURE_SKIP_VERIFY'):
|
|
25
|
+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
26
|
+
verify = False
|
|
27
|
+
|
|
28
|
+
ret = func(url,json=payload,headers=auth,verify=verify)
|
|
22
29
|
return ret
|
|
23
30
|
|
|
24
31
|
def timingInfo(inputs,venue=None,fps=None,ffoa=None,lfoa=None,start_time=None):
|
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
###########################################################
|
|
2
|
+
# Application Name : tc_tools.py
|
|
3
|
+
# Author : Skywalker Dev Labs
|
|
4
|
+
# Date : September 4th, 2025
|
|
5
|
+
# Description : tools for calculations with timecode
|
|
6
|
+
# Location : Original is kept in 1031-tc_tools
|
|
7
|
+
#
|
|
8
|
+
# Frame rate tools:
|
|
9
|
+
# string float_to_v_fr_string(double v_fr) <- does not yet exist (29.97df can not be distinguished from 29.97)
|
|
10
|
+
# double v_fr_string_to_float(v_fr_string)
|
|
11
|
+
# double v_fr_string_to_non_pulldown_float(v_fr_string)
|
|
12
|
+
# Conversion to/from:
|
|
13
|
+
# double vid_frames_to_audio_frames(v_fs, v_fr_string, a_fr)
|
|
14
|
+
# double audio_frames_to_vid_frames(a_fs, v_fr_string, a_fr)
|
|
15
|
+
# double vid_frames_to_time_seconds(v_fs, v_fr_string)
|
|
16
|
+
# double time_seconds_to_vid_frames(time_s, v_fr_string)
|
|
17
|
+
# double vid_frames_to_timecode_seconds(v_fs, v_fr_string)
|
|
18
|
+
# double timecode_seconds_to_vid_frames(timecode_s, v_fr_string)
|
|
19
|
+
# Specialty:
|
|
20
|
+
# double vid_frames_align_to_audio_frames(v_fs, v_fr_string, a_fr, pad_or_trim)
|
|
21
|
+
# Timecode/frame rate tools:
|
|
22
|
+
# General:
|
|
23
|
+
# bool tc_is_whole_video_frames(tc, v_fr_string)
|
|
24
|
+
# string tc_round(tc, partial_v_fr_digits, v_fr_string)
|
|
25
|
+
# Conversion to/from:
|
|
26
|
+
# double tc_to_vid_frames(tc, v_fr_string)
|
|
27
|
+
# string vid_frames_to_tc(v_fs, v_fr_string)
|
|
28
|
+
# double tc_to_time_seconds(tc, v_fr_string)
|
|
29
|
+
# string time_seconds_to_tc(time_s, v_fr_string)
|
|
30
|
+
# double tc_to_audio_frames(tc, v_fr_string, a_fr)
|
|
31
|
+
# string audio_frames_to_tc(a_fs, v_fr_string, a_fr)
|
|
32
|
+
# double tc_add(tc_addend_a, tc_addend_b, v_fr_string)
|
|
33
|
+
# double tc_sub(tc_minuend, tc_subtrahend, v_fr_string)
|
|
34
|
+
#
|
|
35
|
+
# Utilites
|
|
36
|
+
# string tc_tools_version()
|
|
37
|
+
# tc_hr, tc_min, tc_sec, tc_frames, tc_partial_frames parse_tc_string(tc, v_fr_string)
|
|
38
|
+
#
|
|
39
|
+
# Abbreviations used in this file:
|
|
40
|
+
# v_fr video frame rate as a double "(v)ideo (f)rame (r)ate"
|
|
41
|
+
# v_fs video frame count as a double "(v)ideo (f)rame(s)"
|
|
42
|
+
# a_fr audio frame rate as a double "(a)udio (f)rame (r)ate"
|
|
43
|
+
# a_fs audio frame count as a double "(a)udio (f)rame(s)"
|
|
44
|
+
# tc timecode as a string in the form "HR:MN:SC:FR.partial_frames"
|
|
45
|
+
# time_s time in real seconds (not timecode seconds)
|
|
46
|
+
# timecode_s time in timecode seconds
|
|
47
|
+
# v_fr_string video frame rate as a string, one of:
|
|
48
|
+
# VID_FRAME_RATE_STRING_2397
|
|
49
|
+
# VID_FRAME_RATE_STRING_2400
|
|
50
|
+
# VID_FRAME_RATE_STRING_2500
|
|
51
|
+
# VID_FRAME_RATE_STRING_2997
|
|
52
|
+
# VID_FRAME_RATE_STRING_2997df
|
|
53
|
+
# VID_FRAME_RATE_STRING_3000
|
|
54
|
+
# pad_or_trim add or trim to align video frames to audio frames. one of:
|
|
55
|
+
# 'pad'
|
|
56
|
+
# 'trim'
|
|
57
|
+
#
|
|
58
|
+
# Notes:
|
|
59
|
+
# partial_frames: is a fraction of the given frame rate.
|
|
60
|
+
# For example: "00:00:00:03.141" @ 24.00 fps is equal to 3.141 video frames.
|
|
61
|
+
#
|
|
62
|
+
# timecode_seconds: the number of seconds at the timecode rate... not the clock on the wall.
|
|
63
|
+
# timecode_seconds will differ from time_s for pull-down rates (23, 29, and 29DF)
|
|
64
|
+
# 29.97df is a special case where timecode_seconds is equal to the number of video frames at the timecode rate.
|
|
65
|
+
# And so:
|
|
66
|
+
# 00:01:00;02 @29.97df is equal to 60.0000000 timecode_seconds.
|
|
67
|
+
# 00:01:00:00 @29.97 is equal to 60.0000000 timecode_seconds.
|
|
68
|
+
# 00:01:00:02 @29.97 is equal to 60.0666667 timecode_seconds.
|
|
69
|
+
# 00:01:00:00 @30.00 is equal to 60.0000000 timecode_seconds.
|
|
70
|
+
# partial_v_fr_digits: an int, the number of decimal places to round to
|
|
71
|
+
#
|
|
72
|
+
# As of 2023-05-09:
|
|
73
|
+
# vid_frames_to_tc will round to the nearest thousandth of a partial frame (using const PARTIAL_FRAME_NUMBER_OF_DIGITS for precision)
|
|
74
|
+
# time_seconds_to_tc will round to the nearest thousandth of a partial frame (using const PARTIAL_FRAME_NUMBER_OF_DIGITS for precision)
|
|
75
|
+
###########################################################
|
|
76
|
+
|
|
77
|
+
from decimal import Decimal
|
|
78
|
+
|
|
79
|
+
def tc_tools_version():
|
|
80
|
+
return '1.3.2'
|
|
81
|
+
|
|
82
|
+
# @abstract Video frame rates as strings use by tool
|
|
83
|
+
# @discussion VID_FRAME_RATE_STRING_2997df is not fully supported
|
|
84
|
+
VID_FRAME_RATE_STRING_2397 = '23'
|
|
85
|
+
VID_FRAME_RATE_STRING_2400 = '24'
|
|
86
|
+
VID_FRAME_RATE_STRING_2500 = '25'
|
|
87
|
+
VID_FRAME_RATE_STRING_2997 = '29'
|
|
88
|
+
VID_FRAME_RATE_STRING_2997df = '29DF'
|
|
89
|
+
VID_FRAME_RATE_STRING_3000 = '30'
|
|
90
|
+
VID_FRAME_RATE_STRING_4800 = '48'
|
|
91
|
+
VID_FRAME_RATE_STRING_5000 = '50'
|
|
92
|
+
VID_FRAME_RATE_STRING_6000 = '60'
|
|
93
|
+
|
|
94
|
+
# delimeter to use when creating a 2997df tc string
|
|
95
|
+
# this has no effect on reading a tc string (when reading, it can be either : or ;);
|
|
96
|
+
TC_TOOLS_29DF_SEC_FRAME_DELIM = ':' # traditionally, this is a ;
|
|
97
|
+
|
|
98
|
+
PARTIAL_FRAME_NUMBER_OF_DIGITS = 7
|
|
99
|
+
ALIGN_VFS_ROUND_DIGITS = 9
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
valid_v_fr_strings = [
|
|
103
|
+
VID_FRAME_RATE_STRING_2397,
|
|
104
|
+
VID_FRAME_RATE_STRING_2400,
|
|
105
|
+
VID_FRAME_RATE_STRING_2500,
|
|
106
|
+
VID_FRAME_RATE_STRING_2997,
|
|
107
|
+
VID_FRAME_RATE_STRING_2997df,
|
|
108
|
+
VID_FRAME_RATE_STRING_3000,
|
|
109
|
+
VID_FRAME_RATE_STRING_4800,
|
|
110
|
+
VID_FRAME_RATE_STRING_5000,
|
|
111
|
+
VID_FRAME_RATE_STRING_6000,
|
|
112
|
+
]
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def v_fr_string_validate(v_fr_string):
|
|
116
|
+
"""Validate v_fr_string and return an upper() version"""
|
|
117
|
+
v_fr_str_upper = v_fr_string.upper()
|
|
118
|
+
v_fr_str_okay = False
|
|
119
|
+
for v in valid_v_fr_strings:
|
|
120
|
+
if v == v_fr_str_upper:
|
|
121
|
+
v_fr_str_okay = True
|
|
122
|
+
break
|
|
123
|
+
if not v_fr_str_okay:
|
|
124
|
+
err_msg = ''.join(['tc_tools: frame rate not recognized = ', v_fr_string])
|
|
125
|
+
print(err_msg)
|
|
126
|
+
raise Exception(err_msg)
|
|
127
|
+
return v_fr_str_upper
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def v_fr_string_to_non_pulldown_float(v_fr_string):
|
|
131
|
+
"""Convert from one of the enum frame rate strings to the non-pulldown float value."""
|
|
132
|
+
v_fr_str_upper = v_fr_string_validate(v_fr_string)
|
|
133
|
+
if v_fr_str_upper == VID_FRAME_RATE_STRING_2397:
|
|
134
|
+
v_fr = 24.0
|
|
135
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_2400:
|
|
136
|
+
v_fr = 24.0
|
|
137
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_2500:
|
|
138
|
+
v_fr = 25.0
|
|
139
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_2997:
|
|
140
|
+
v_fr = 30.0
|
|
141
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_2997df:
|
|
142
|
+
v_fr = 30.0
|
|
143
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_3000:
|
|
144
|
+
v_fr = 30.0
|
|
145
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_4800:
|
|
146
|
+
v_fr = 48.0
|
|
147
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_5000:
|
|
148
|
+
v_fr = 50.0
|
|
149
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_6000:
|
|
150
|
+
v_fr = 60.0
|
|
151
|
+
else:
|
|
152
|
+
err_msg = ''.join(['tc_tools: frame rate not recognized = ', v_fr_string, ' This should never happen.'])
|
|
153
|
+
print(err_msg)
|
|
154
|
+
raise Exception(err_msg)
|
|
155
|
+
return v_fr
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def v_fr_string_to_float(v_fr_string):
|
|
159
|
+
"""Convert from one of the enum frame rate strings to the actual float value."""
|
|
160
|
+
v_fr_str_upper = v_fr_string_validate(v_fr_string)
|
|
161
|
+
if v_fr_str_upper == VID_FRAME_RATE_STRING_2397:
|
|
162
|
+
v_fr = 24.0/1.001
|
|
163
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_2400:
|
|
164
|
+
v_fr = 24.0
|
|
165
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_2500:
|
|
166
|
+
v_fr = 25.0
|
|
167
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_2997:
|
|
168
|
+
v_fr = 30/1.001
|
|
169
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_2997df:
|
|
170
|
+
v_fr = 30/1.001
|
|
171
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_3000:
|
|
172
|
+
v_fr = 30.0
|
|
173
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_4800:
|
|
174
|
+
v_fr = 48.0
|
|
175
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_5000:
|
|
176
|
+
v_fr = 50.0
|
|
177
|
+
elif v_fr_str_upper == VID_FRAME_RATE_STRING_6000:
|
|
178
|
+
v_fr = 60.0
|
|
179
|
+
else:
|
|
180
|
+
err_msg = ''.join(['tc_tools: frame rate not recognized = ', v_fr_string, ' This should never happen.'])
|
|
181
|
+
print(err_msg)
|
|
182
|
+
raise Exception(err_msg)
|
|
183
|
+
return v_fr
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def vid_frames_to_audio_frames(v_fs, v_fr_string, a_fr):
|
|
187
|
+
"""Convert from number of video frames to audio frames."""
|
|
188
|
+
v_fr = v_fr_string_to_float(v_fr_string)
|
|
189
|
+
a_fs = v_fs * ( a_fr / v_fr )
|
|
190
|
+
return a_fs
|
|
191
|
+
|
|
192
|
+
def audio_frames_to_vid_frames(a_fs, v_fr_string, a_fr):
|
|
193
|
+
"""Convert from number of audio frames to video frames."""
|
|
194
|
+
v_fr = v_fr_string_to_float(v_fr_string)
|
|
195
|
+
v_fs = a_fs * (v_fr / a_fr)
|
|
196
|
+
return v_fs
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def vid_frames_to_time_seconds(v_fs, v_fr_string):
|
|
200
|
+
"""Convert from number of video frames to time in seconds."""
|
|
201
|
+
v_fr = v_fr_string_to_float(v_fr_string)
|
|
202
|
+
return v_fs / v_fr
|
|
203
|
+
|
|
204
|
+
def time_seconds_to_vid_frames(time_s, v_fr_string):
|
|
205
|
+
"""Convert from time in seconds to number of video frames."""
|
|
206
|
+
v_fr = v_fr_string_to_float(v_fr_string)
|
|
207
|
+
return v_fr * time_s
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def vid_frames_to_timecode_seconds(v_fs, v_fr_string):
|
|
211
|
+
"""Convert from number of video frames to time in timcode seconds."""
|
|
212
|
+
v_fr = v_fr_string_to_non_pulldown_float(v_fr_string)
|
|
213
|
+
return v_fs / v_fr
|
|
214
|
+
|
|
215
|
+
def timecode_seconds_to_vid_frames(time_s, v_fr_string):
|
|
216
|
+
"""Convert from time in timecode seconds to number of video frames."""
|
|
217
|
+
v_fr = v_fr_string_to_non_pulldown_float(v_fr_string)
|
|
218
|
+
return v_fr * time_s
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
############################################################################################
|
|
222
|
+
# Local utility functions
|
|
223
|
+
############################################################################################
|
|
224
|
+
|
|
225
|
+
def is_float_whole_number(f):
|
|
226
|
+
if f == round(f): return True
|
|
227
|
+
return False
|
|
228
|
+
|
|
229
|
+
def tc_components_to_tc(tc_hr, tc_min, tc_sec, tc_fr, tc_partial_frames, v_fr_string):
|
|
230
|
+
"""Assemble timecode components into a timecode string."""
|
|
231
|
+
# 2023-11-30 DRJ: need to accomodate 2997df
|
|
232
|
+
# if tc_partial_frames + tc_fr + tc_sec ends up adding to minutes, we need to make adjustments.
|
|
233
|
+
# v_fr_string = VID_FRAME_RATE_STRING_2997df
|
|
234
|
+
# tc_hr = 0
|
|
235
|
+
# tc_min = 0
|
|
236
|
+
# tc_sec = 59
|
|
237
|
+
# tc_fr = 30
|
|
238
|
+
# tc_partial_frames = 0
|
|
239
|
+
# the resulting tc would be 00:01:00:02
|
|
240
|
+
# 2024-05-10 DRJ: when tc_partial_frames was less than 0.0001, it was being written in exp notation.
|
|
241
|
+
# And so, I added the Decimal bits.
|
|
242
|
+
# 2025-01-07 DRJ: Decimal does some mangled rounding! 0.6 is printed ad 0.599999999999999999999
|
|
243
|
+
v_fr = v_fr_string_to_non_pulldown_float(v_fr_string)
|
|
244
|
+
sec_fr_delimiter = ':'
|
|
245
|
+
if v_fr_string == VID_FRAME_RATE_STRING_2997df:
|
|
246
|
+
sec_fr_delimiter = TC_TOOLS_29DF_SEC_FRAME_DELIM
|
|
247
|
+
|
|
248
|
+
while tc_partial_frames >= 1.0:
|
|
249
|
+
tc_fr += 1
|
|
250
|
+
tc_partial_frames -= 1.0
|
|
251
|
+
while tc_fr >= v_fr:
|
|
252
|
+
tc_sec += 1
|
|
253
|
+
tc_fr -= int(v_fr)
|
|
254
|
+
while tc_sec >= 60:
|
|
255
|
+
tc_min += 1
|
|
256
|
+
tc_sec -= 60
|
|
257
|
+
while tc_min >= 60:
|
|
258
|
+
tc_hr += 1
|
|
259
|
+
tc_min -= 60
|
|
260
|
+
|
|
261
|
+
tc = ''.join([ str(tc_hr).zfill(2), ':', str(tc_min).zfill(2), ':',str(tc_sec).zfill(2), sec_fr_delimiter, str(tc_fr).zfill(2)])
|
|
262
|
+
if tc_partial_frames > 0.0:
|
|
263
|
+
tc_partial_frames_string = str(tc_partial_frames).lstrip('0')
|
|
264
|
+
if tc_partial_frames < 0.0001:
|
|
265
|
+
tc_partial_frames_string = str(Decimal(tc_partial_frames)).lstrip('0')
|
|
266
|
+
if (len(tc_partial_frames_string) + 1) > PARTIAL_FRAME_NUMBER_OF_DIGITS:
|
|
267
|
+
tc_partial_frames_string = tc_partial_frames_string[0:PARTIAL_FRAME_NUMBER_OF_DIGITS + 1].rstrip('0')
|
|
268
|
+
tc = ''.join([tc,tc_partial_frames_string ])
|
|
269
|
+
return tc
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
############################################################################################
|
|
273
|
+
# specialty and general conversion
|
|
274
|
+
############################################################################################
|
|
275
|
+
|
|
276
|
+
#
|
|
277
|
+
# returns the number of video frames you must add or trim so that video and audio frames are aligned
|
|
278
|
+
# v_fs the current number of video frames
|
|
279
|
+
# v_fr_string the video frame rate
|
|
280
|
+
# a_fr the audio frame rate
|
|
281
|
+
# pad_or_trim either 'pad' or 'trim'
|
|
282
|
+
def vid_frames_align_to_audio_frames(v_fs, v_fr_string, a_fr, pad_or_trim):
|
|
283
|
+
#booger
|
|
284
|
+
v_fr_str_upper = v_fr_string_validate(v_fr_string)
|
|
285
|
+
|
|
286
|
+
rounded_v_fs = round(v_fs, ALIGN_VFS_ROUND_DIGITS)
|
|
287
|
+
if pad_or_trim == 'pad':
|
|
288
|
+
v_fs_to_add = 0.0
|
|
289
|
+
if not is_float_whole_number(rounded_v_fs):
|
|
290
|
+
v_fs_to_add += int(v_fs + 1) - v_fs
|
|
291
|
+
v_fs_to_add = round(v_fs_to_add, ALIGN_VFS_ROUND_DIGITS)
|
|
292
|
+
if (v_fr_str_upper == VID_FRAME_RATE_STRING_2997) or (v_fr_str_upper == VID_FRAME_RATE_STRING_2997df):
|
|
293
|
+
if a_fr != 48000.0:
|
|
294
|
+
err_msg = ''.join(['tc_tools: align_to_audio_frames: 29.97 and 29DF currently only audio frame rate of 48000.0 is supported. a_fr = ', str(a_fr)])
|
|
295
|
+
print(err_msg)
|
|
296
|
+
raise Exception(err_msg)
|
|
297
|
+
v_fs_whole_vid_frames = round(v_fs + v_fs_to_add, ALIGN_VFS_ROUND_DIGITS)
|
|
298
|
+
v_fs_whole_vid_frames_mod_5 = v_fs_whole_vid_frames % 5.0
|
|
299
|
+
if v_fs_whole_vid_frames_mod_5:
|
|
300
|
+
v_fs_to_add += 5 - v_fs_whole_vid_frames_mod_5
|
|
301
|
+
return v_fs_to_add
|
|
302
|
+
if pad_or_trim == 'trim':
|
|
303
|
+
v_fs_to_trim = 0.0
|
|
304
|
+
if not is_float_whole_number(rounded_v_fs):
|
|
305
|
+
v_fs_to_trim += v_fs - int(v_fs)
|
|
306
|
+
if (v_fr_str_upper == VID_FRAME_RATE_STRING_2997) or (v_fr_str_upper == VID_FRAME_RATE_STRING_2997df):
|
|
307
|
+
if a_fr != 48000.0:
|
|
308
|
+
err_msg = ''.join(['tc_tools: align_to_audio_frames: 29.97 and 29DF currently only audio frame rate of 48000.0 is supported. a_fr = ', str(a_fr)])
|
|
309
|
+
print(err_msg)
|
|
310
|
+
raise Exception(err_msg)
|
|
311
|
+
v_fs_whole_vid_frames = round(v_fs - v_fs_to_trim, ALIGN_VFS_ROUND_DIGITS)
|
|
312
|
+
v_fs_whole_vid_frames_mod_5 = v_fs_whole_vid_frames % 5.0
|
|
313
|
+
if v_fs_whole_vid_frames_mod_5:
|
|
314
|
+
v_fs_to_trim += v_fs_whole_vid_frames_mod_5
|
|
315
|
+
return v_fs_to_trim
|
|
316
|
+
err_msg = ''.join(['tc_tools: align_to_audio_frames: mode must be one of trim or pad'])
|
|
317
|
+
print(err_msg)
|
|
318
|
+
raise Exception(err_msg)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def parse_tc_string(tc, v_fr_string):
|
|
322
|
+
"""
|
|
323
|
+
parse tc into tc_hr, tc_min, tc_sec, tc_frames, tc_partial_frames
|
|
324
|
+
TODO: 2025-09-26: use frame rate to only allow ';' in the case of VID_FRAME_RATE_STRING_2997df
|
|
325
|
+
"""
|
|
326
|
+
parse_ok = True
|
|
327
|
+
tc_splits = tc.rstrip().split(":")
|
|
328
|
+
try:
|
|
329
|
+
if 3 == len(tc_splits):
|
|
330
|
+
tc_splits_df = tc_splits[2].split(";")
|
|
331
|
+
if 2 == len(tc_splits_df):
|
|
332
|
+
tc_sec = int(tc_splits_df[0])
|
|
333
|
+
tc_fr_and_partial = tc_splits_df[1]
|
|
334
|
+
else:
|
|
335
|
+
parse_ok = False
|
|
336
|
+
elif 4 == len(tc_splits):
|
|
337
|
+
tc_sec = int(tc_splits[2])
|
|
338
|
+
tc_fr_and_partial = tc_splits[3]
|
|
339
|
+
else:
|
|
340
|
+
parse_ok = False
|
|
341
|
+
if parse_ok:
|
|
342
|
+
tc_hr = int(tc_splits[0])
|
|
343
|
+
tc_min = int(tc_splits[1])
|
|
344
|
+
tc_frames = int(tc_fr_and_partial.split(".")[0])
|
|
345
|
+
if len(tc_fr_and_partial) > 2:
|
|
346
|
+
tc_partial_frames = float(''.join(['0.', tc_fr_and_partial.split(".")[1]]))
|
|
347
|
+
else:
|
|
348
|
+
tc_partial_frames = 0.0
|
|
349
|
+
except:
|
|
350
|
+
parse_ok = False
|
|
351
|
+
if not parse_ok:
|
|
352
|
+
err_msg = ''.join(['tc is not valid form HR:MN:SC:FR.partial tc = ', tc])
|
|
353
|
+
print(err_msg)
|
|
354
|
+
raise Exception(err_msg)
|
|
355
|
+
return tc_hr, tc_min, tc_sec, tc_frames, tc_partial_frames
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def tc_is_whole_video_frames(tc, v_fr_string):
|
|
359
|
+
"""Return true if timecode string is a whole number of video frames."""
|
|
360
|
+
try:
|
|
361
|
+
tc_hr, tc_min, tc_sec, tc_frames, tc_partial_frames = parse_tc_string(tc, v_fr_string)
|
|
362
|
+
except Exception as e:
|
|
363
|
+
err_msg = ''.join(['tc_tools: tc_is_whole_video_frames: ', str(e)])
|
|
364
|
+
print(err_msg)
|
|
365
|
+
raise Exception(err_msg)
|
|
366
|
+
if tc_partial_frames != 0.0:
|
|
367
|
+
return False
|
|
368
|
+
return True
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def tc_round(tc, partial_v_fr_digits, v_fr_string):
|
|
372
|
+
v_fs = tc_to_vid_frames(tc, v_fr_string)
|
|
373
|
+
v_fs = round(v_fs, partial_v_fr_digits)
|
|
374
|
+
return vid_frames_to_tc(v_fs, v_fr_string)
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
############################################################################################
|
|
378
|
+
# Conversion to/from timecode
|
|
379
|
+
############################################################################################
|
|
380
|
+
|
|
381
|
+
def tc_to_vid_frames(tc, v_fr_string):
|
|
382
|
+
"""Get the number of video frames from timecode."""
|
|
383
|
+
v_fr_str_upper = v_fr_string_validate(v_fr_string)
|
|
384
|
+
|
|
385
|
+
v_fr = v_fr_string_to_non_pulldown_float(v_fr_str_upper)
|
|
386
|
+
tc_hr, tc_min, tc_sec, tc_frames, tc_partial_frames = parse_tc_string(tc, v_fr_str_upper)
|
|
387
|
+
frame_count = tc_frames
|
|
388
|
+
# 29.97 Drop-Frame Time Code eliminates 2 frames every minute except for minutes 00, 10, 20, 30, 40, and 50
|
|
389
|
+
if v_fr_str_upper == VID_FRAME_RATE_STRING_2997df:
|
|
390
|
+
total_minutes = tc_min + (tc_hr * 60)
|
|
391
|
+
frames_due_to_minutes = v_fr * total_minutes * 60
|
|
392
|
+
frames_due_to_minutes -= (int(total_minutes) * 2)
|
|
393
|
+
frames_due_to_minutes += (int(total_minutes / 10) * 2)
|
|
394
|
+
frames_due_to_seconds = v_fr * tc_sec
|
|
395
|
+
frame_count += frames_due_to_seconds
|
|
396
|
+
frame_count += frames_due_to_minutes
|
|
397
|
+
else:
|
|
398
|
+
frame_count += v_fr * tc_sec
|
|
399
|
+
frame_count += v_fr * tc_min * 60
|
|
400
|
+
frame_count += v_fr * tc_hr * 60 * 60
|
|
401
|
+
return frame_count + tc_partial_frames
|
|
402
|
+
|
|
403
|
+
def vid_frames_to_tc(v_fs, v_fr_string):
|
|
404
|
+
"""Get timecode from the number of video frames ."""
|
|
405
|
+
v_fr_str_upper = v_fr_string_validate(v_fr_string)
|
|
406
|
+
v_fr = v_fr_string_to_non_pulldown_float(v_fr_str_upper)
|
|
407
|
+
# if we are negative, wrap around
|
|
408
|
+
v_fs_24hr = tc_to_vid_frames('24:00:00:00', v_fr_str_upper)
|
|
409
|
+
v_fs = round(v_fs, PARTIAL_FRAME_NUMBER_OF_DIGITS)
|
|
410
|
+
if v_fs < 0:
|
|
411
|
+
v_fs = v_fs + v_fs_24hr
|
|
412
|
+
if v_fs > v_fs_24hr:
|
|
413
|
+
v_fs = v_fs - v_fs_24hr
|
|
414
|
+
|
|
415
|
+
_tc = str("00:00:00:00")
|
|
416
|
+
_vid_frames = v_fs
|
|
417
|
+
if v_fr_str_upper == VID_FRAME_RATE_STRING_2997df:
|
|
418
|
+
if True:
|
|
419
|
+
# for every 1800 video frames, add two video frames
|
|
420
|
+
minutes_fr_to_add = int(v_fs/1800) * 2
|
|
421
|
+
# for every 18000 video frames, take away two video frames
|
|
422
|
+
minutes_fr_to_remove = int(v_fs/18000) * 2
|
|
423
|
+
v_fs += minutes_fr_to_add
|
|
424
|
+
v_fs -= minutes_fr_to_remove
|
|
425
|
+
total_seconds = v_fs / v_fr
|
|
426
|
+
total_minutes = total_seconds / 60.0
|
|
427
|
+
total_minutes_int = int(total_minutes)
|
|
428
|
+
_vid_frames += (total_minutes_int * 2)
|
|
429
|
+
_vid_frames -= (int(total_minutes_int / 10) * 2)
|
|
430
|
+
if True:
|
|
431
|
+
_tc_hr = int(_vid_frames / (v_fr * 60 * 60))
|
|
432
|
+
_vid_frames = _vid_frames - v_fr * _tc_hr * 60 * 60
|
|
433
|
+
_tc_min = int(_vid_frames / (v_fr * 60))
|
|
434
|
+
_vid_frames = _vid_frames - v_fr * _tc_min * 60
|
|
435
|
+
_tc_sec = int(_vid_frames / (v_fr))
|
|
436
|
+
_vid_frames = _vid_frames - v_fr * _tc_sec
|
|
437
|
+
_tc_fr = int(_vid_frames)
|
|
438
|
+
_vid_frames = _vid_frames - _tc_fr
|
|
439
|
+
_tc_subfr = _vid_frames
|
|
440
|
+
_subframes = round(_tc_subfr, PARTIAL_FRAME_NUMBER_OF_DIGITS)
|
|
441
|
+
_tc = tc_components_to_tc(_tc_hr, _tc_min, _tc_sec, _tc_fr, _subframes, v_fr_str_upper)
|
|
442
|
+
_tc_v_vs = tc_to_vid_frames(_tc, v_fr_string)
|
|
443
|
+
if _tc_v_vs > v_fs_24hr:
|
|
444
|
+
# should never happen
|
|
445
|
+
err_msg = ''.join(['tc_tools: vid_frames_to_tc() result exceeds 24hr. This should never happen.'])
|
|
446
|
+
print(err_msg)
|
|
447
|
+
raise Exception(err_msg)
|
|
448
|
+
return _tc
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def time_seconds_to_tc(time_s, v_fr_string):
|
|
452
|
+
v_fs = time_s * v_fr_string_to_float(v_fr_string)
|
|
453
|
+
return vid_frames_to_tc(v_fs,v_fr_string)
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
def tc_to_time_seconds(tc, v_fr_string):
|
|
457
|
+
v_fs = tc_to_vid_frames(tc, v_fr_string)
|
|
458
|
+
time_s = v_fs / v_fr_string_to_float(v_fr_string)
|
|
459
|
+
return time_s
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
def tc_to_audio_frames(tc, v_fr_string, a_fr):
|
|
463
|
+
v_fs = tc_to_vid_frames(tc, v_fr_string)
|
|
464
|
+
return vid_frames_to_audio_frames(v_fs, v_fr_string, a_fr)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def audio_frames_to_tc(a_fs, v_fr_string, a_fr):
|
|
468
|
+
v_fs = audio_frames_to_vid_frames(a_fs, v_fr_string, a_fr)
|
|
469
|
+
return vid_frames_to_tc(v_fs, v_fr_string)
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def tc_sub(tc_minuend, tc_subtrahend, v_fr_string):
|
|
473
|
+
"""Get the timecode difference in video frames."""
|
|
474
|
+
dif_vid_frames = tc_to_vid_frames(tc_minuend, v_fr_string) - tc_to_vid_frames(tc_subtrahend, v_fr_string)
|
|
475
|
+
return dif_vid_frames
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def tc_add(tc_addend_a, tc_addend_b, v_fr_string):
|
|
479
|
+
"""Get the timecode sum in video frames."""
|
|
480
|
+
sum_vid_frames = tc_to_vid_frames(tc_addend_a, v_fr_string) + tc_to_vid_frames(tc_addend_b, v_fr_string)
|
|
481
|
+
return sum_vid_frames
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
############################################################################################
|
|
485
|
+
#
|
|
486
|
+
# for direct testing
|
|
487
|
+
#
|
|
488
|
+
############################################################################################
|
|
489
|
+
|
|
490
|
+
if __name__ == "__main__":
|
|
491
|
+
print('tc_tools: top of __main__')
|
|
492
|
+
|
|
493
|
+
vid_frame_rate = VID_FRAME_RATE_STRING_2397
|
|
494
|
+
tc = '00:00:01:15'
|
|
495
|
+
tc_v_fs = tc_to_vid_frames(tc, vid_frame_rate)
|
|
496
|
+
print('__main__: tc = ',tc)
|
|
497
|
+
print('__main__: tc_v_fs = ',tc_v_fs)
|
|
498
|
+
print('__main__: timecode_seconds = ',vid_frames_to_timecode_seconds(tc_v_fs, vid_frame_rate))
|
|
499
|
+
|
|
500
|
+
vid_frame_rate = VID_FRAME_RATE_STRING_2997
|
|
501
|
+
tc_v_fs = 2589411
|
|
502
|
+
tc = vid_frames_to_tc(tc_v_fs,vid_frame_rate)
|
|
503
|
+
print('__main__: tc_v_fs = ',tc_v_fs)
|
|
504
|
+
print('__main__: tc = ',tc)
|
|
505
|
+
tc_v_fs = 757473
|
|
506
|
+
|
|
507
|
+
tc = vid_frames_to_tc(tc_v_fs,vid_frame_rate)
|
|
508
|
+
print('__main__: tc_v_fs = ',tc_v_fs)
|
|
509
|
+
print('__main__: tc = ',tc)
|
|
510
|
+
|
|
511
|
+
vid_frame_rate = VID_FRAME_RATE_STRING_2400
|
|
512
|
+
tc = '00:00:01:15'
|
|
513
|
+
tc_v_fs = tc_to_vid_frames(tc, vid_frame_rate)
|
|
514
|
+
print('__main__: tc = ',tc)
|
|
515
|
+
print('__main__: tc_v_fs = ',tc_v_fs)
|
|
516
|
+
print('__main__: timecode_seconds = ',vid_frames_to_timecode_seconds(tc_v_fs, vid_frame_rate))
|
|
517
|
+
|
|
518
|
+
tc = '00:00:01:20'
|
|
519
|
+
tc_v_fs = tc_to_vid_frames(tc, vid_frame_rate)
|
|
520
|
+
print('__main__: tc = ',tc)
|
|
521
|
+
print('__main__: tc_v_fs = ',tc_v_fs)
|
|
522
|
+
print('__main__: timecode_seconds = ',vid_frames_to_timecode_seconds(tc_v_fs, vid_frame_rate))
|
|
523
|
+
|
|
524
|
+
vid_frame_rate = VID_FRAME_RATE_STRING_2500
|
|
525
|
+
tc = '00:00:01:14'
|
|
526
|
+
tc_v_fs = tc_to_vid_frames(tc, vid_frame_rate)
|
|
527
|
+
print('__main__: tc = ',tc)
|
|
528
|
+
print('__main__: tc_v_fs = ',tc_v_fs)
|
|
529
|
+
print('__main__: timecode_seconds = ',vid_frames_to_timecode_seconds(tc_v_fs, vid_frame_rate))
|
|
530
|
+
|
|
531
|
+
exit(0)
|