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/
(on GitHub)
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
)
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
Built Distribution
File details
Details for the file ass-0.4.5.tar.gz
.
File metadata
- Download URL: ass-0.4.5.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9243d00b74e663019e0ccfe8802c6081254443052d351ff9cc788009d1e26df2 |
|
MD5 | 87c631d3e3552cc7174efc9d50d8ead7 |
|
BLAKE2b-256 | 83bd97f9e0007306d635dbb042bca5ff2d41167fe02c8ffa1c8073f57989756a |
File details
Details for the file ass-0.4.5-py3-none-any.whl
.
File metadata
- Download URL: ass-0.4.5-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf26950b43a713df1cfe52386194c93dd3aef3aa2f7091c07e5d236d62017207 |
|
MD5 | 9c511a2edcc20c34bdff408facb20849 |
|
BLAKE2b-256 | be66aac65fb2959c6fbde24cc5982dc5091bbdb1937eef085abed29661b63e25 |