Skip to content

Instantly share code, notes, and snippets.

@leplatrem
Created February 23, 2012 09:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leplatrem/1891989 to your computer and use it in GitHub Desktop.
Save leplatrem/1891989 to your computer and use it in GitHub Desktop.
ass2dcl : conversion of ASS subtitles to XML DCSubtitle
"""
``ass2dcl`` converts subtitles from ASS to DCSubtitle format.
It depends on python3 and `pysub <http://pypi.python.org/pypi/pysubs>`_.
INSTALL
::
python3
python3-minimal
python3-lxml
And follow ``pysub`` installation instructions.
USAGE
::
python3 ass2dcl.py --lang=English your_file.ass
THE BEER-WARE LICENSE (Revision 42):
"Mathieu Leplatre" <contact@mathieu-leplatre.info> wrote this file.
As long as you retain this notice you can do whatever you want with this stuff.
If we meet some day, and you think this stuff is worth it, you can buy me
a beer in return, Mathieu Leplatre.
"""
import re
import os
import sys
import uuid
import math
import hashlib
import base64
import logging
import datetime
from xml.etree.ElementTree import Element, SubElement, ElementTree, Comment
from optparse import OptionParser
from gettext import gettext as _
VERSION = '1.0'
CR = r"\N"
logger = logging.getLogger(__name__)
try:
import pysubs
except SyntaxError:
raise Exception("pysubs requires python 3")
class Converter(object):
def __init__(self):
self.assfile = None
self.mainstyle = None
def color(self, color):
""" Converts ASS color """
if isinstance(color, (str,bytes)):
return "FF%s%s%s" % (color[4:6], color[2:4], color[0:2]) # from b,g,r to r,g,b
return 'FF%s' % color.to_str('srt').replace('#', '')
def time(self, time):
""" Converts ASS time """
srtime, decimal = time.to_str('srt').split(',')
decimal = int(decimal) * 240 / 1000
return "%s:%s" % (srtime, int(decimal))
def fontsize(self, size):
""" Converts font size to percents according to resolution """
return math.ceil(size / int(self.assfile.info['PlayResY']) * 792.0)
def fontnode(self, parent, fontattrs=None):
""" Builds a <Font> element from attributes """
if not fontattrs:
fontattrs = self.fontattrs()
fontnode = SubElement(parent, "Font")
for name, value in fontattrs.items():
fontnode.set(name, value)
return fontnode
def fontattrs(self, text='', style=None, full=True):
""" Builds attributes from text ASS markup and style """
if not style:
style = self.mainstyle
attrs = {}
if full or self.mainstyle.Fontsize != style.Fontsize:
attrs['Size'] = "%s" % self.fontsize(style.Fontsize)
if full or style.Bold:
attrs['Weight'] = "bold" if style.Bold else "normal"
# event style or text has color ?
if full or self.mainstyle.PrimaryColour != style.PrimaryColour:
attrs['Color'] = self.color(style.PrimaryColour)
color = re.match(r"^.*\{[^}]*\\1c&H(([0-9a-zA-Z]{2}){3})&[^}]*\}", text)
if color:
bgr = color.group(1)
attrs['Color'] = self.color(bgr)
# style or text is italic ?
if full or style.Italic:
attrs['Italic'] = "yes" if style.Italic else "no"
italic = re.match(r".*\{[^}]*\\i1[^}]*\}(.+)\{[^}]*\\i0[^}]*\}", text)
if italic:
attrs['Italic'] = "yes"
# constant values
if full:
attrs['Id'] = style.name
attrs['Spacing'] = "0em"
attrs['Effect'] = "border"
attrs['EffectColor'] = "FF000000"
attrs['AspectAdjust'] = "1.0"
return attrs
def sublineattrs(self, i, total, event):
""" Builds <Text> attributes from current line, number of lines and ASS event """
style = self.assfile.styles.get(event.style)
if not style:
style = self.mainstyle
# events margins override style margins
marginv = event['MarginV'] if event['MarginV'] > style['MarginV'] else style['MarginV']
marginl = event['MarginL'] if event['MarginL'] > style['MarginL'] else style['MarginL']
marginr = event['MarginR'] if event['MarginR'] > style['MarginR'] else style['MarginR']
# convert margins to percents of screen
playresx = int(self.assfile.info['PlayResX'])
playresy = int(self.assfile.info['PlayResY'])
marginv = marginv / playresy * 100.0
marginl = marginl / playresx * 100.0
marginr = marginr / playresx * 100.0
# positioning according to ASS alignment
attrs = {}
attrs['Direction'] = "horizontal"
attrs['VAlign'] = "bottom"
lineoffset = (total-i) * self.fontsize(style['FontSize']) * 0.12
attrs['VPosition'] = '%.1f' % (marginv + lineoffset)
if style['Alignment'] == 1:
attrs['HAlign'] = "left"
attrs['HPosition'] = '%.1f' % marginl
elif style['Alignment'] == 2:
attrs['HAlign'] = "center"
attrs['HPosition'] = '%.1f' % (marginl / 2.0)
elif style['Alignment'] == 3:
attrs['HAlign'] = "right"
attrs['HPosition'] = '%.1f' % marginr
return attrs
def subtitlenode(self, parent, i, event):
""" Builds <Subtitle> element from position and ASS event """
subtitle = SubElement(parent, "Subtitle")
subtitle.set('SpotNumber', repr(i+1))
subtitle.set('TimeIn', self.time(event.start))
subtitle.set('TimeOut', self.time(event.end))
subtitle.set('FadeUpTime', "0")
subtitle.set('FadeDownTime', "0")
# iterate on text lines
lines = event.text.replace("\\n", CR).split(CR)
for i, line in enumerate(lines):
subline = SubElement(subtitle, "Text")
for name, value in self.sublineattrs(i+1, len(lines), event).items():
subline.set(name, value)
eventstyle = self.assfile.styles.get(event.style)
fontattrs = self.fontattrs(text=line, style=eventstyle, full=False)
# clear ASS tags
line = re.sub(r"\{[^}]*\}", "", line)
if fontattrs:
fontnode = self.fontnode(subline, fontattrs)
fontnode.text = line
else:
subline.text = line
return subtitle
def convert(self, inputfile, outputfile, options):
""" Reads inputfile and write DCSubtitle to outputfile """
self.assfile = pysubs.load(inputfile, encoding="utf-8")
self.mainstyle = self.assfile.styles.popitem(last=False)[1]
# main element
root = Element("DCSubtitle")
root.append(Comment(text="*** XML Subtitle File ***"))
root.append(Comment(text="*** %s ***" % datetime.date.today().isoformat()))
root.append(Comment(text="*** Created by ass2dcl ***"))
root.set("Version", "1.0")
rootchild = SubElement(root, "SubtitleID")
rootchild.text = str(uuid.uuid4())
rootchild = SubElement(root, "MovieTitle")
rootchild.text = self.assfile.info['Title']
rootchild = SubElement(root, "ReelNumber")
rootchild.text = repr(options.reel)
rootchild = SubElement(root, "Language")
rootchild.text = options.language
rootchild = SubElement(root, "LoadFont")
rootchild.set('Id', self.mainstyle.name)
rootchild.set('URI', '%s.ttf' % self.mainstyle.Fontname)
# add subtitle children nodes
fontnode = self.fontnode(root)
for i, event in enumerate(self.assfile):
self.subtitlenode(fontnode, i, event)
# wrap it in an ElementTree instance, and save as XML
tree = ElementTree(root)
tree.write(outputfile, encoding="UTF-8", xml_declaration=True)
logger.info(_("'%s' written successfully") % outputfile)
return tree
@classmethod
def hash(cls, outputfile):
""" Computes DCSubtitle hash of file """
content = ''
with open(outputfile, 'rb') as out:
content = out.read()
sha1 = hashlib.sha1(content).digest()
return base64.b64encode(sha1).decode("utf-8")
if __name__ == "__main__":
# Parse command-line arguments
parser = OptionParser(usage="usage: %prog [options] ASSFILE [...ASSFILE]", version="%prog " + VERSION)
parser.add_option("--reel",
dest="reel", type="int", metavar="INT", default=1,
help=_("ReelNumber (default: 1)"))
parser.add_option("--lang",
dest="language", type="string", default='French',
help=_("Language (default: French)"))
parser.add_option("--log-level",
dest="log_level", default=logging.INFO, type='int',
help=_("Logging level for messages (1:debug 2:info, 3:warning, 4:errors, 5:critical)"))
(options, args) = parser.parse_args(sys.argv)
logging.basicConfig(level=options.log_level)
if len(args) < 2:
parser.error(_("Specify a *.ass input file."))
sys.exit(1)
cc = Converter()
for filename in args[1:]:
name, ext = os.path.splitext(filename)
output = "%s.xml" % name
cc.convert(filename, output, options)
logger.info(_("Hash is: %s") % cc.hash(output))
logger.info(_("Done."))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment