Skip to content

Commit

Permalink
Merge eb37d91 into 529d83e
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oakey committed Apr 18, 2017
2 parents 529d83e + eb37d91 commit ca8c0ed
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pysrt/srtfile.py
Expand Up @@ -166,6 +166,35 @@ def from_string(cls, source, **kwargs):
new_file = cls(**kwargs)
new_file.read(source.splitlines(True), error_handling=error_handling)
return new_file

def to_string(self):
"""
returns the subtitles as a string
"""
output = []
for sub in self:
output.append(str(sub))
return '\n'.join(output)

def remove_overlaps(self):
"""
Adjusts start times of overlapping subtitles to remove overlap
"""
i = 1
while i < len(self):
previous_end = self[i - 1].end.to_millis()
current_start = self[i].start.to_millis()
current_end = self[i].end.to_millis()

if current_start < previous_end:
current_start = previous_end
self[i].start.from_millis(current_start)

if current_start >= current_end:
self.pop(i)

else:
i += 1

def read(self, source_file, error_handling=ERROR_PASS):
"""
Expand Down
35 changes: 35 additions & 0 deletions pysrt/srttime.py
Expand Up @@ -175,3 +175,38 @@ def to_time(self):
"""
return time(self.hours, self.minutes, self.seconds,
self.milliseconds * 1000)

def to_millis(self):
"""
Returns SubRipTime corresponding to a total count of milliseconds
"""
total_millis = sum([
self.milliseconds,
self.seconds * 1000,
self.minutes * 60 * 1000,
self.hours * 60 * 60 * 1000
])
return total_millis

def from_millis(self, total_millis):
"""
Sets the hours, minutes, seconds, & milliseconds from total
milliseconds
"""
millis_per_hour = 60 * 60 * 100
millis_per_min = 60 * 1000
millis_per_sec = 1000

hours = int(total_millis / millis_per_hour)
remainder = total_millis % millis_per_hour

minutes = int(remainder / millis_per_min)
remainder = remainder % millis_per_min

seconds = int(remainder / millis_per_sec)
milliseconds = remainder % millis_per_sec

self.hours = hours
self.minutes = minutes
self.seconds = seconds
self.milliseconds = milliseconds
17 changes: 17 additions & 0 deletions tests/static/overlapping-dialogues.srt
@@ -0,0 +1,17 @@
1
00:00:0,000 --> 00:00:01,000
I am the 1st dialogue

2
00:00:00,500 --> 00:00:02,500
I am the 2nd dialogue
I am overlapping the 1st and 3rd

3
00:00:00,250 --> 00:00:02,300
I am the 3rd dialogue, but I'm completely overlapped by the 2nd
I should be removed

4
00:00:02,000 --> 00:00:03,000
I am the 4th dialogue
18 changes: 18 additions & 0 deletions tests/test_srtfile.py
Expand Up @@ -253,5 +253,23 @@ def test_missing_indexes(self):
items = pysrt.open(os.path.join(self.base_path, 'no-indexes.srt'))
self.assertEquals(len(items), 7)

class TestRemoveOverlaps(unittest.TestCase):
"""
Test the remove_overlap function
"""

def setUp(self):
self.base_path = os.path.join(file_path, 'tests', 'static')

def test_remove_overlaps(self):
path = os.path.join(self.base_path, 'overlapping-dialogues.srt')
file = pysrt.open(path)
file.remove_overlaps()

for i in range(1, len(file)):
previous_end = file[i - 1].end.to_millis()
current_start = file[i].start.to_millis()
self.assertTrue(previous_end <= current_start)

if __name__ == '__main__':
unittest.main()

0 comments on commit ca8c0ed

Please sign in to comment.