python-ass
A library for parsing and manipulating Advanced SubStation Alpha subtitle files.
Note this is a (hopefully temporary) fork by ItMightBeKaraoke adding some minor improvements. If these or similar updates are upstreamed, this will be removed.
Documents
Example file: tests/test.ass
You can parse the file:
>>> import ass
>>> with open("tests/test.ass", encoding='utf_8_sig') as f:
... doc = ass.parse(f)
...
Access its meta info:
>>> doc.info
ScriptInfoSection('Script Info', OrderedDict([('ScriptType', 'v4.00+'), ('PlayResX', 500), ('PlayResY', 500)]))
>>> doc.info['PlayResX']
500
Access its styles:
>>> doc.styles
StylesSection('V4+ Styles', [Style(name='Default', fontname='Arial', fontsize=20.0, primary_color=Color(r=0xff, g=0xff, b=0xff, a=0x00), secondary_color=Color(r=0xff, g=0x00, b=0x00, a=0x00), outline_color=Color(r=0x00, g=0x00, b=0x00, a=0x00), back_color=Color(r=0x00, g=0x00, b=0x00, a=0x00), bold=False, italic=False, underline=False, strike_out=False, scale_x=100.0, scale_y=100.0, spacing=0.0, angle=0.0, border_style=1, outline=1.0, shadow=2.0, alignment=5, margin_l=10, margin_r=10, margin_v=10, encoding=1)])
>>> doc.styles[0].fontname
'Arial'
>>> doc.styles[0].primary_color # "color", not "colour"
Color(r=0xff, g=0xff, b=0xff, a=0x00)
Access its event lines:
>>> doc.events
EventsSection('Events', [Dialogue(layer=0, start=datetime.timedelta(0), end=datetime.timedelta(seconds=5), style='Default', name='', margin_l=0, margin_r=0, margin_v=0, effect='', text='{\\3c&H0000FF}this is a test\\N{\\3c&H00FF00}this is a test\\N{\\3c&HFF0000}this is a test'), ...])
>>> doc.events[0].text
'{\\3c&H0000FF}this is a test\\N{\\3c&H00FF00}this is a test\\N{\\3c&HFF0000}this is a test'
Or any other section data:
>>> list(doc.sections.keys())
['Script Info', 'Aegisub Project Garbage', 'Custom Section', 'V4+ Styles', 'Events', 'Aegisub Extradata']
>>> doc.sections['Aegisub Project Garbage']['Scroll Position']
'30'
You can dump everything out into ASS format, too:
>>> doc.events[0].dump()
'0,0:00:00.00,0:00:05.00,Default,,0,0,0,,{\\3c&H0000FF}this is a test\\N{\\3c&H00FF00}this is a test\\N{\\3c&HFF0000}this is a test'
Or maybe the whole file:
>>> with open("out.ass", "w", encoding='utf_8_sig') 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/
(on GitHub).
Rendering
The following has been unmaintained for years.
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)
Metadata
Release files for ass-imbk 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ass_imbk-1.0.4.tar.gz | 12.9 kB | Without provenance | Details for ass_imbk-1.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ass_imbk-1.0.4-py3-none-any.whl | Python 3 | none | any | Details for ass_imbk-1.0.4-py3-none-any.whl |
Total release size: 28.6 kB
Release files / ass_imbk-1.0.4.tar.gz
| Download URL | ass_imbk-1.0.4.tar.gz |
|---|---|
| Size | 12.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dec4b47c9e8958f13ca4333be61097970ce8c2bdf9dd686923068a021f241806
|
|
BLAKE2b-256 checksum How to use checksums |
51b83e3b19931801c70e8023e09fd9531d21cff96895b6695e7ba80d6a2879fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / ass_imbk-1.0.4-py3-none-any.whl
| Download URL | ass_imbk-1.0.4-py3-none-any.whl |
|---|---|
| Size | 15.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3b5e8cd7b14c8a0b87aee1fea74b10bc22369e9f7842fc4469a6ffb9b56d78d0
|
|
BLAKE2b-256 checksum How to use checksums |
8cddbd065fb7cfe53b3d50dc738c06737c3ccc008c4d2bc0a791da8c71b06025
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Security for ass-imbk 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.