Exemple #1
0
    def apply(self):
        syncpoints = _syncPoints(self._current.model)
        if len(syncpoints) == 0:
            return

        ts = TimeSync(self._current.data.subtitles)  # in-place sync
        ts.sync(syncpoints)
        command = ChangeData(self._current.editor.filePath, self._current.data,
                             _("Subtitles synchronization"))
        self._subtitleData.execute(command)
Exemple #2
0
    def setUp(self):
        self._subs = SubManager()
        self._subs.append(self.createSubtitle(1, 2))
        self._subs.append(self.createSubtitle(25, 26))
        self._subs.append(self.createSubtitle(21, 25))
        self._subs.append(self.createSubtitle(22, 23))
        self._subs.append(self.createSubtitle(25, 28))
        self._subs.append(self.createSubtitle(30, 30))

        self._testedTimeSync = TimeSync(self._subs)
Exemple #3
0
    def createSubData(self, subFile, outputFormat):
        data = SubtitleData()
        data.fps = self.getFps(subFile)
        data.outputFormat = outputFormat
        data.inputEncoding = self.getInputEncoding(subFile)
        data.outputEncoding = self.getOutputEncoding(data.inputEncoding)

        self.printData(subFile.path, data)

        try:
            data.subtitles = self.parseFile(subFile, data.inputEncoding, data.fps)
            if self._args.sync:
                syncpoints = syncparse.parse(self._args.sync, data.subtitles)
                ts = TimeSync(data.subtitles)
                ts.sync(syncpoints)
        except SubException as msg:
            log.error(str(msg))
            return None

        # shouldn't throw as all common checks are performed by getters
        data.verifyAll()
        return data
Exemple #4
0
class TestOffset(unittest.TestCase):
    """Offset Unit Tests"""

    def setUp(self):
        self._subs = SubManager()
        self._subs.append(self.createSubtitle(1, 2))
        self._subs.append(self.createSubtitle(25, 26))
        self._subs.append(self.createSubtitle(21, 25))
        self._subs.append(self.createSubtitle(22, 23))
        self._subs.append(self.createSubtitle(25, 28))
        self._subs.append(self.createSubtitle(30, 30))

        self._testedTimeSync = TimeSync(self._subs)

    def createFrameTime(self, secs):
        return FrameTime(fps = 25, seconds = secs)

    def createSubtitle(self, secStart, secEnd):
        start = self.createFrameTime(secStart)
        end = self.createFrameTime(secEnd)
        text = "Test subtitle, %d - %d" % (secStart, secEnd)
        return Subtitle(start, end, text)

    def createSyncPoint(self, subNo, secStart, secEnd):
        start = self.createFrameTime(secStart)
        end = self.createFrameTime(secEnd)
        return SyncPoint(subNo, start, end)

    def test_syncWithoutSyncPointsChangesNothing(self):
        syncList = []
        self._testedTimeSync.sync(syncList)

        self.assertEqual(self._subs[0].start, self.createFrameTime(1))
        self.assertEqual(self._subs[0].end, self.createFrameTime(2))

        self.assertEqual(self._subs[3].start, self.createFrameTime(22))
        self.assertEqual(self._subs[3].end, self.createFrameTime(23))

    def test_syncWithAFirstSyncPoint(self):
        syncList = [self.createSyncPoint(0, 5, 7)]
        self._testedTimeSync.sync(syncList)

        self.assertEqual(self._subs[0].start, self.createFrameTime(5))
        self.assertEqual(self._subs[0].end, self.createFrameTime(7))

        self.assertNotEqual(self._subs[1].start, self.createFrameTime(25))
        self.assertNotEqual(self._subs[1].end,  self.createFrameTime(26))

        self.assertEqual(self._subs[-1].start, self.createFrameTime(30))
        self.assertEqual(self._subs[-1].end, self.createFrameTime(30))

    def test_syncWithALastPoint(self):
        syncList = [self.createSyncPoint(5, 5, 7)]
        self._testedTimeSync.sync(syncList)

        self.assertEqual(self._subs[0].start, self.createFrameTime(1))
        self.assertEqual(self._subs[0].end, self.createFrameTime(2))

        self.assertTrue(self._subs[1].start < self.createFrameTime(5))
        self.assertTrue(self._subs[1].end < self.createFrameTime(7))

        self.assertEqual(self._subs[-1].start, self.createFrameTime(5))
        self.assertEqual(self._subs[-1].end, self.createFrameTime(7))

    def test_syncWithASingleSyncPointNotAtStartOrEnd(self):
        syncList = [self.createSyncPoint(3, 5, 7)]
        self._testedTimeSync.sync(syncList)

        self.assertEqual(self._subs[0].start, self.createFrameTime(1))
        self.assertEqual(self._subs[0].end, self.createFrameTime(2))

        self.assertTrue(self._subs[1].start < self.createFrameTime(25))
        self.assertTrue(self._subs[1].end < self.createFrameTime(26))
        self.assertTrue(self._subs[1].start > self.createFrameTime(5))
        self.assertTrue(self._subs[1].end > self.createFrameTime(7))

        self.assertEqual(self._subs[3].start, self.createFrameTime(5))
        self.assertEqual(self._subs[3].end, self.createFrameTime(7))

        self.assertEqual(self._subs[-1].start, self.createFrameTime(30))
        self.assertEqual(self._subs[-1].end, self.createFrameTime(30))

    def test_syncWithTwoSyncPointsAtStartAndEnd(self):
        syncList = [
            self.createSyncPoint(0, 7, 8),
            self.createSyncPoint(5, 50, 60)
        ]
        self._testedTimeSync.sync(syncList)

        self.assertEqual(self._subs[0].start, self.createFrameTime(7))
        self.assertEqual(self._subs[0].end, self.createFrameTime(8))

        self.assertTrue(self._subs[2].start > self.createFrameTime(21))
        self.assertTrue(self._subs[2].end > self.createFrameTime(25))

        self.assertEqual(self._subs[-1].start, self.createFrameTime(50))
        self.assertEqual(self._subs[-1].end, self.createFrameTime(60))

    def test_syncWithTwoSyncPointNotAtStartOrEnd(self):
        syncList = [
            self.createSyncPoint(2, 7, 8),
            self.createSyncPoint(4, 50, 60)
        ]
        self._testedTimeSync.sync(syncList)

        # BEWARE! OGRES!
        # If some conditions are not clear, take a look at setUp method. We fill subs with some
        # silly values on purpose, sometimes lower than the previous ones. Syncing subtitles should
        # preserve these dependencies.
        self.assertEqual(self._subs[0].start, self.createFrameTime(1))
        self.assertEqual(self._subs[0].end, self.createFrameTime(2))

        self.assertTrue(self._subs[1].start > self.createFrameTime(7))
        self.assertTrue(self._subs[1].end > self.createFrameTime(8))

        self.assertEqual(self._subs[2].start, self.createFrameTime(7))
        self.assertEqual(self._subs[2].end, self.createFrameTime(8))

        self.assertTrue(self._subs[3].start > self.createFrameTime(7))
        self.assertEqual(self._subs[3].end, self.createFrameTime(8)) # see _calculateTime() comments
        self.assertTrue(self._subs[3].start < self.createFrameTime(50))

        self.assertEqual(self._subs[4].start, self.createFrameTime(50))
        self.assertEqual(self._subs[4].end, self.createFrameTime(60))

    def test_syncWithNotSortedSyncPoints(self):
        syncList = [
            self.createSyncPoint(2, 2, 3),
            self.createSyncPoint(1, 1, 2),
            self.createSyncPoint(5, 5, 6),
            self.createSyncPoint(4, 4, 5),
            self.createSyncPoint(3, 3, 4),
            self.createSyncPoint(0, 0, 1)
        ]

        self._testedTimeSync.sync(syncList)

        self.assertEqual(self._subs[0].start, self.createFrameTime(0))
        self.assertEqual(self._subs[0].end, self.createFrameTime(1))

        self.assertEqual(self._subs[1].start, self.createFrameTime(1))
        self.assertEqual(self._subs[1].end, self.createFrameTime(2))

        self.assertEqual(self._subs[2].start, self.createFrameTime(2))
        self.assertEqual(self._subs[2].end, self.createFrameTime(3))

        self.assertEqual(self._subs[3].start, self.createFrameTime(3))
        self.assertEqual(self._subs[3].end, self.createFrameTime(4))

        self.assertEqual(self._subs[4].start, self.createFrameTime(4))
        self.assertEqual(self._subs[4].end, self.createFrameTime(5))

        self.assertEqual(self._subs[5].start, self.createFrameTime(5))
        self.assertEqual(self._subs[5].end, self.createFrameTime(6))