-
-
Save cbuctok/3bc36028ce0ed69507fac105d135597d to your computer and use it in GitHub Desktop.
Python Merge two subtitles
This file contains 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 | |
# -*- coding: utf8 -*- | |
# ---------------------------------------------------------------------------- | |
# Name: merge-subtitles | |
# Desc: Merge two subtitles | |
# Usage: merge-subtitles sub1_file sub2_file out.ass | |
# Example: merge_subtitles friends_s01e01_720p_bluray_x264-sujaidr.mp4en.srt friends_s01e01_720p_bluray_x264-sujaidr.mp4vi.srt friends_s01e01_720p_bluray_x264-sujaidr.mp4.ass | |
# Copy From: https://github.com/duyamin/standalone-scripts/blob/master/python/merge-subtitles.py | |
# ---------------------------------------------------------------------------- | |
import sys | |
import aeidon | |
header = """[Script Info] | |
ScriptType: v4.00+ | |
Collisions: Normal | |
[V4+ Styles] | |
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,WenQuanYi Micro Hei,16,&H00ffffff,&H00000000,&H00000000,0,0,2,1,1,0,20,20,20 | |
Style: Alternate,WenQuanYi Micro Hei,9,&H00ffffff,&H00000000,&H00000000,0,0,2,1,1,0,20,20,20 | |
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 | |
""" | |
def merge_subtitles(in_filename1, in_filename2, out_filename): | |
encoding1 = encoding2 = 'utf-8' | |
# create aeidon project | |
project1 = aeidon.Project() | |
project2 = aeidon.Project() | |
project1.open_main(in_filename1, encoding1) | |
project2.open_main(in_filename2, encoding2) | |
# setup output format | |
out_format = aeidon.files.new(aeidon.formats.ASS, out_filename, "utf_8") | |
out_format.header = header | |
# motify event entries | |
for subtitle in project1.subtitles: | |
subtitle.main_text = subtitle.main_text.replace('\n', ' ') | |
subtitle.ssa.style = 'Top' | |
for subtitle in project2.subtitles: | |
subtitle.main_text = subtitle.main_text.replace('\n', ' ') | |
subtitle.ssa.style = 'Bot' | |
project1.subtitles.extend(project2.subtitles) | |
project1.save_main(out_format) | |
def usage(): | |
print('./merge-subtitles sub1_file sub2_file out.ass') | |
def main(): | |
if len(sys.argv) != 4: | |
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