Last active
June 5, 2018 20:05
-
-
Save shidarin/11091783 to your computer and use it in GitHub Desktop.
Constructs an SMPTE timecode segment with in, out and duration. Timecode in can be provided or generated randomly. 100% Coverage Tests included.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #/usr/bin/python | |
| # SMPTE TimeCode Segement | |
| # By Sean Wallitsch, 2014/04/19 | |
| """ | |
| Contains a TimeCodeSegment class that allows input of SMPTE hours, | |
| minutes, seconds, frames, fps and desired duration. | |
| The class will then contain a start, end, and duration SMPTE | |
| timecode values, stored as strings such as: | |
| '12:23:15:10' | |
| Drop frame FPS is not supported. | |
| Tests are included, and will execute if the file is called directly. | |
| ## License | |
| The MIT License (MIT) | |
| Copyright (c) 2014 Sean Wallitsch | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| ## Classes | |
| TimeCodeSegment | |
| Constructs an SMPTE timecode segment with in, out and duration. | |
| Timecode in can be provided or generated randomly. | |
| TestTimeCodeSegment | |
| 100% coverage tests for the above class. | |
| """ | |
| #=============================================================================== | |
| # IMPORTS | |
| #=============================================================================== | |
| # Standard Imports | |
| import datetime | |
| from random import randrange | |
| import unittest | |
| #=============================================================================== | |
| # CLASSES | |
| #=============================================================================== | |
| class TimeCodeSegment(object): | |
| """Generates an SMPTE timecode segment for in, out, duration | |
| If no args are provided, random timecode is generated and 24p assumed. | |
| If a duration of over 24 hours is provided, timecode will roll over. | |
| Args: | |
| hour : (int) | |
| The hour to start the timecode at | |
| minute : (int) | |
| The minute to start the timecode at | |
| second : (int) | |
| The second to start the timecode at | |
| frame : (int) | |
| The frame to start the timecode at | |
| duration : (int) | |
| Duration in frames (assuming 24p) If not provided, this will be | |
| a random number of frames up to 5 minutes. | |
| fps : (int) | |
| Frames per second as an int. Drop frame timecode is not supported. | |
| Attributes: | |
| start : (str) | |
| Timecode in, in the form of 10:52:55:20 | |
| Hours, minutes, seconds, frames | |
| end : (str) | |
| Timecode out | |
| dur : (str) | |
| Timecode duration | |
| durFrames : (int) | |
| Frame duration as an int | |
| """ | |
| def __init__(self, hour=None, minute=None, second=None, frame=None, | |
| duration=None, fps=24): | |
| # We compare against None so that 00 times are preserved. | |
| if hour is None: | |
| hour = randrange(00, 24) | |
| if minute is None: | |
| minute = randrange(00, 60) | |
| if second is None: | |
| second = randrange(00, 60) | |
| if frame is None: | |
| frame = randrange(00, 24) | |
| if duration is None: | |
| # We'll make these clips anything short of 5 minutes | |
| duration = randrange(00, fps * 300) | |
| # I'd like to use a time object here instead of datetime, but | |
| # timedelta oddly won't do any math operations with time. | |
| startTime = datetime.datetime(1, 1, 1, hour, minute, second) | |
| durSeconds = duration / fps | |
| durFrames = duration % fps | |
| frameRollover = False # Keep track of if frames roll over. | |
| if durFrames + frame >= fps: | |
| # If our leftover frames and our starting frame add up to more than | |
| # 24 seconds, we need to add a second to our duration calculation. | |
| durSeconds += 1 | |
| frameRollover = True | |
| endFrames = (frame + duration) % fps | |
| timeDelta = datetime.timedelta(seconds=durSeconds) | |
| endTime = startTime + timeDelta | |
| # If we adjusted duration up to get an accurate end time, we'll adjust | |
| # it back down now. | |
| if frameRollover: | |
| timeDelta = timeDelta - datetime.timedelta(seconds=1) | |
| durationTime = datetime.datetime(1, 1, 1, 0, 0, 0) + timeDelta | |
| # Because we had to use datetime objects, isoformat() will be returning | |
| # a string formatted as: '0001-01-01T12:10:25' | |
| # So we'll split on the T | |
| self.start = "{time}:{frames}".format( | |
| time=startTime.isoformat().split('T')[-1], | |
| frames=str(frame).rjust(2, '0') | |
| ) | |
| self.end = "{time}:{frames}".format( | |
| time=endTime.isoformat().split('T')[-1], | |
| frames=str(endFrames).rjust(2, '0') | |
| ) | |
| self.dur = "{time}:{frames}".format( | |
| time=durationTime.isoformat().split('T')[-1], | |
| frames=str(durFrames).rjust(2, '0') | |
| ) | |
| self.durFrames = duration | |
| class TestTimeCodeSegment(unittest.TestCase): | |
| """Tests TimeCodeSegment class for correct functionality""" | |
| #=========================================================================== | |
| # TESTS | |
| #=========================================================================== | |
| def testWholeSeconds(self): | |
| """Tests timecode where seconds are an even multiple of frames""" | |
| tc = TimeCodeSegment(12, 0, 0, 0, 24) | |
| self.assertEqual( | |
| '12:00:00:00', | |
| tc.start | |
| ) | |
| self.assertEqual( | |
| '12:00:01:00', | |
| tc.end | |
| ) | |
| self.assertEqual( | |
| '00:00:01:00', | |
| tc.dur | |
| ) | |
| #=========================================================================== | |
| def testLessThanOneSecond(self): | |
| """Tests timecode where there's less than a second of duration""" | |
| tc = TimeCodeSegment(12, 0, 0, 0, 4) | |
| self.assertEqual( | |
| '12:00:00:04', | |
| tc.end | |
| ) | |
| self.assertEqual( | |
| '00:00:00:04', | |
| tc.dur | |
| ) | |
| #=========================================================================== | |
| def testMoreThanOneSecond(self): | |
| """Tests timecode where there's more than a second of duration""" | |
| tc = TimeCodeSegment(12, 0, 0, 0, 25) | |
| self.assertEqual( | |
| '12:00:01:01', | |
| tc.end | |
| ) | |
| self.assertEqual( | |
| '00:00:01:01', | |
| tc.dur | |
| ) | |
| #=========================================================================== | |
| def testUnevenCombination(self): | |
| """Tests timecode where there's less than a second of duration""" | |
| tc = TimeCodeSegment(12, 0, 0, 15, 10) | |
| self.assertEqual( | |
| '12:00:00:15', | |
| tc.start | |
| ) | |
| self.assertEqual( | |
| '12:00:01:01', | |
| tc.end | |
| ) | |
| self.assertEqual( | |
| '00:00:00:10', | |
| tc.dur | |
| ) | |
| #=========================================================================== | |
| def testAddManyHours(self): | |
| """Tests adding many hours, minutes and seconds of duration""" | |
| tc = TimeCodeSegment(0, 0, 0, 1, 1016356) | |
| self.assertEqual( | |
| '00:00:00:01', | |
| tc.start | |
| ) | |
| self.assertEqual( | |
| '11:45:48:05', | |
| tc.end | |
| ) | |
| self.assertEqual( | |
| '11:45:48:04', | |
| tc.dur | |
| ) | |
| #=========================================================================== | |
| def testRollover(self): | |
| """Tests timecode that rolls over 24 hours""" | |
| tc = TimeCodeSegment(23, 59, 59, 23, 2) | |
| self.assertEqual( | |
| '23:59:59:23', | |
| tc.start | |
| ) | |
| self.assertEqual( | |
| '00:00:00:01', | |
| tc.end | |
| ) | |
| self.assertEqual( | |
| '00:00:00:02', | |
| tc.dur | |
| ) | |
| #=========================================================================== | |
| def test30Fps(self): | |
| """Tests timecode with 30 fps""" | |
| tc = TimeCodeSegment(12, 0, 0, 15, 10, fps=30) | |
| self.assertEqual( | |
| '12:00:00:15', | |
| tc.start | |
| ) | |
| self.assertEqual( | |
| '12:00:00:25', | |
| tc.end | |
| ) | |
| self.assertEqual( | |
| '00:00:00:10', | |
| tc.dur | |
| ) | |
| #=========================================================================== | |
| def test60Fps(self): | |
| """Tests timecode with 60 fps""" | |
| tc = TimeCodeSegment(12, 0, 0, 15, 40, fps=60) | |
| self.assertEqual( | |
| '12:00:00:15', | |
| tc.start | |
| ) | |
| self.assertEqual( | |
| '12:00:00:55', | |
| tc.end | |
| ) | |
| self.assertEqual( | |
| '00:00:00:40', | |
| tc.dur | |
| ) | |
| #=========================================================================== | |
| def testAllRandomSetDuration(self): | |
| """Tests random timecode with set long duration""" | |
| tc = TimeCodeSegment(fps=60, duration=5183999) | |
| self.assertEqual( | |
| '23:59:59:59', | |
| tc.dur | |
| ) | |
| self.assertEqual( | |
| 5183999, | |
| tc.durFrames | |
| ) | |
| #=========================================================================== | |
| def testDurationUnderFiveMinutes(self): | |
| """Tests that duration when not provided is kept under 5 minutes""" | |
| tc = TimeCodeSegment(fps=24) | |
| self.assertLess( | |
| tc.durFrames, | |
| 7200 | |
| ) | |
| #=============================================================================== | |
| # MAIN | |
| #=============================================================================== | |
| if __name__ == '__main__': | |
| unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment