Skip to main content

A library for parsing and manipulating Advanced SubStation Alpha subtitle files.

Project description

python-ass

A library for parsing and manipulating Advanced SubStation Alpha subtitle files.

Documents

test.ass

[Script Info]
ScriptType: v4.00+

[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,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:00.00,0:00:05.00,Default,,0,0,0,,hello!

You can parse the file:

>>> import ass
>>> with open("test.ass", "r") as f:
...     doc = ass.parse(f)
...

Access some of its styles:

>>> doc.styles
[<ass.document.Style object at ...>]
>>> doc.styles[0].fontname
'Arial'
>>> doc.styles[0].primary_color  # "color", not "colour"
Color(r=0xff, g=0xff, b=0xff, a=0x00)

Or its event lines:

>>> doc.events
[<ass.document.Dialogue object at ...>]
>>> doc.events[0].text
'hello!'

You can dump them back out into ASS format, too:

>>> doc.events[0].dump()
'0,0:00:00.00,0:00:05.00,Default,,0,0,0,,hello!'

Or maybe the whole file:

>>> with open("out.ass", "w") as f:
...     doc.dump_file(f)
...

Tags

For parsing ASS tags, you may want to consider ass-tag-parser: https://pypi.org/project/ass-tag-parser/

Rendering

python-ass can use libass for rendering.

First you need to allocate a libass context:

>>> ctx = ass.renderer.Context()

Then you need to convert the ass.document.Document to a ass.renderer.Track:

>>> t = ctx.make_track()
>>> t.populate(doc)

Then make a renderer to render the track:

>>> r = ctx.make_renderer()
>>> r.set_fonts(fontconfig_config="/usr/local/etc/fonts/fonts.conf")
>>> r.set_all_sizes((1280, 720))

You can render a frame at a given time:

>>> imgs = r.render_frame(t, timedelta(0))

Example using PIL to render to a bitmap:

>>> im_out = Image.new("RGB", (1280, 720))
>>> im_data = im_out.load()
>>> for img in imgs:
...     r, g, b, a = img.rgba
...     for y in range(img.h):
...         for x in range(img.w):
...             a_src = img[x, y] * (256 - a) // 256
...             r_dst, g_dst, b_dst = im_data[x + img.dst_x, y + img.dst_y]
...             r_out = ((r * a_src) + (r_dst * (256 - a_src))) // 256
...             g_out = ((g * a_src) + (g_dst * (256 - a_src))) // 256
...             b_out = ((b * a_src) + (b_dst * (256 - a_src))) // 256
...             im_data[x + img.dst_x, y + img.dst_y] = (r_out, g_out, b_out)
...
>>> im_out.show()

Sample Rendering (from renderer_test.py)

Test rendering

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ass-0.4.4.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

ass-0.4.4-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file ass-0.4.4.tar.gz.

File metadata

  • Download URL: ass-0.4.4.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2

File hashes

Hashes for ass-0.4.4.tar.gz
Algorithm Hash digest
SHA256 1a24e3b0877a850ea41e195e05d50cde8f7be428b9d71046226bd9cccb8e67d4
MD5 c80cbc03bd836d6c0cacbe200e1f0d7a
BLAKE2b-256 dd8611dc4eea3f08ea1e4ec08ab86b01f0b30b50de27355d15d421ccde2b5897

See more details on using hashes here.

File details

Details for the file ass-0.4.4-py3-none-any.whl.

File metadata

  • Download URL: ass-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2

File hashes

Hashes for ass-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bf420da2b15754b18a7d05324daabd151c2722c43fbcbb0dd675ff7ccc34fe98
MD5 284981308d2e6cee5e1b2a006c000116
BLAKE2b-256 4dc1211e6d5568643643e40918be8bc3e4948934ee75a83222008cf652adac94

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page