forked from IgorRidanovic/smpte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMPTE.py
executable file
·82 lines (63 loc) · 2.2 KB
/
SMPTE.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Converts frames to SMPTE timecode of arbitrary frame rate and back.
# Copyright 2018, Igor Riđanović, [email protected], Meta Fide
# adapted by mivk, 2020
class SMPTE(object):
'''Frames to SMPTE timecode converter and reverse.'''
def __init__(self, *args):
self.fps = args[0] if len(args) > 0 else 24
self.df = args[1] if len(args) > 1 else False
def df_check(self):
if self.df and ( self.fps == int(self.fps) or int(self.fps) not in (29, 59) ):
import sys
sys.stderr.write('Ignoring invalid drop-frame option with ' + str(self.fps) + " fps!\n")
return False
def getframes(self, tc):
'''Converts SMPTE timecode to frame count.'''
# Always round fractional frame rate.
self.fps = int(round(self.fps))
# Validate timecode
if len(tc) != 11:
raise ValueError ('Malformed SMPTE timecode', tc)
if int(tc[9:]) >= self.fps:
raise ValueError ('SMPTE timecode to frame rate mismatch.', tc, self.fps)
# Calculate timecode to frames for fps time base
min = int(tc[:2])*60 + int(tc[3:5])
sec = int(tc[6:8])
frm = int((min*60 + sec) * self.fps + int(tc[9:]))
# Drop frame adjustment
if self.df and self.df_check():
# Subtract 2 frames for each minute...
dffrm = frm - min * 2
# ...except every 10 minutes.
tens = min / 10
dffrm += tens * 2
return dffrm
return frm
def gettc(self, frames):
'''Converts frame count to SMPTE timecode.'''
# Always round fractional frame rate.
self.fps = int(round(self.fps))
frames = abs(frames)
spacer = spacer2 = ':'
# Drop frame adjustment
if self.df and self.df_check():
spacer2 = ';'
minfactor = self.fps * 60
tenminfactor = self.fps * 600
# Add 2 frames for each minute...
frames += frames/minfactor * 2
# ...except every ten minutes
frames -= frames/tenminfactor * 2
frHour = self.fps * 3600
frSec = self.fps * 60
hr = int(frames // frHour)
mn = int((frames - hr * frHour) // frSec)
sc = int((frames - hr * frHour - mn * frSec) // self.fps)
fr = int(round(frames - hr * frHour - mn * frSec - sc * self.fps))
return(
str(hr).zfill(2) + spacer +
str(mn).zfill(2) + spacer +
str(sc).zfill(2) + spacer2 +
str(fr).zfill(2))