Created
November 15, 2019 18:31
-
-
Save dvdbng/8890defff55c13205823a92dd62a63b9 to your computer and use it in GitHub Desktop.
Merge two or more subtitles
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/python3 | |
""" | |
LPT: If your original subtitles are not .srt, convert them using ffmpeg | |
""" | |
import sys | |
import pysrt | |
def merge_subtitles(srts): | |
head = """[Script Info] | |
ScriptType: v4.00+ | |
Collisions: Normal | |
PlayDepth: 0 | |
Timer: 100,0000 | |
Video Aspect Ratio: 0 | |
WrapStyle: 0 | |
ScaledBorderAndShadow: no | |
[V4+ Styles] | |
Format: Name,Fontname,Fontsize,PrimaryColour,SecondaryColour,OutlineColour,BackColour,Bold,Italic,Underline,StrikeOut,ScaleX,ScaleY,Spacing,Angle,BorderStyle,Outline,Shadow,Alignment,MarginL,MarginR,MarginV,Encoding | |
Style: Default,Arial,16,&H00FFFFFF,&H00FFFFFF,&H80000000,&H80000000,-1,0,0,0,100,100,0,0,1,3,0,2,10,10,10,0 | |
Style: Top,Arial,16,&H00F9FFFF,&H00FFFFFF,&H80000000,&H80000000,-1,0,0,0,100,100,0,0,1,3,0,8,10,10,10,0 | |
Style: Mid,Arial,16,&H0000FFFF,&H00FFFFFF,&H80000000,&H80000000,-1,0,0,0,100,100,0,0,1,3,0,5,10,10,10,0 | |
Style: Bot,Arial,16,&H00F9FFF9,&H00FFFFFF,&H80000000,&H80000000,-1,0,0,0,100,100,0,0,1,3,0,2,10,10,10,0 | |
[Events] | |
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text""" | |
parts = [head.replace("\n", "\r\n")] | |
items = [] | |
for i, file in enumerate(srts): | |
srt = pysrt.open(file) | |
for item in srt : | |
item.style = "Top" if i==0 else "Bot" | |
items.append(item) | |
items.sort(key=lambda x: x.start.ordinal) | |
for item in items: | |
parts.append("Dialogue: 0,{start},{end},{style},,0000,0000,0000,,{txt}".format( | |
start=str(item.start)[:-1].replace(",", "."), | |
end=str(item.end)[:-1].replace(",", "."), | |
style=item.style, | |
txt=item.text.replace("\n", "\\N") | |
)) | |
print("\r\n".join(parts)) | |
def usage(): | |
print('./merge-subtitles sub1_file sub2_file ... > out.ass') | |
def main(): | |
if len(sys.argv) < 3: | |
usage() | |
else: | |
merge_subtitles(sys.argv[1:]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment