Giter VIP home page Giter VIP logo

python-ass's Introduction

python-ass

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

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)

Test rendering

python-ass's People

Contributors

denizdogan avatar fichtefoll avatar funami580 avatar jpenney avatar myaamori avatar nattofriends avatar rsekman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

python-ass's Issues

Write more tests

We have some tests, but they barely scratch the surface and corner cases are badly documented, if covered at all. We should at least add tests for all publicly exposed API (#9), ideally more.

Add a better way to know if an event is a comment or not.

Currently as far as I know we have to do the following:

import ass

file = "example.ass"

with open(file, encoding='utf_8_sig') as f:
    doc = ass.parse(f)

for event in doc.events:
    if isinstance(event, ass.line.Comment):
        print("this is a comment:", event)

there should be a better way to identify if an event is a comment or not, like a property in the class

`Tag` class cannot be instantiated → remove it

Issue

问题出在self.params
类的初始化函数中不包含属性param,在后文中无法被引用
若在初始化函数中声明该属性,此方法即可正常使用

Origin

self.param = params

image

It should be...

self.params = params

image

Toggle between comment and dialogue

There is currently no straight-forward method to toggle between a comment and a dialogue event. The best available workaround is Comment(**event._field_mappings), which uses an internal property created by the Field-aware metaclass and this subject to changes (unless documented).

Raised by @po5.

Parsing string not file

Any functions to parse ass from string other than file? Sometimes it is hard or unnecessary to read from a file (e.g. Read ass file from a compressed file on the fly).

Tag parsing

There is a method to parse tags in a line, but it doesn't actually work because Tag.from_ass is unimplemented. (yeah)

Available options are:

  1. Remove it and let users choose whatever they want to parse text lines (e.g. ass-tag-parser)
  2. Use ass-tag-parser behind the scenes (not too satisfied with its current API)
  3. Come up with a simple implementation on my own (seems like wasted effort when 2) exists)

Can I merge the 'effect' column of more than 3 .ass files?

Hi, there :)

I am currently doing a transcription project, and there are three annotators to describe some certain features in the 'effect' column.
I'm wondering if it is possible that I can merge the text in the 'effect' column from the three annotators?

Thank you very much for your reply! :)

Somehow broken muxed ass works in player but throw error while parsing

error:
ValueError: invalid literal for int() with base 10: 'Comment'

Like you see the Marked=0 is in wrong order, while also missing Format
Somehow this work on most players I tried, ffmpeg extract it this way while its not ffmpeg bug.
I'm unable to parse it in python-ass so I could fix it via re-dumping.

body

[Script Info]
; Script generated by Aegisub v1.10 
; http://www.aegisub.net
Title: <untitled>
Original Script: <unknown>
ScriptType: v4.00+
Collisions: Normal
PlayResY: 1024
PlayDepth: 0
Timer: 100.0000
WrapStyle: 0
Last Style Storage: Default
Video Aspect Ratio: 0
Video Zoom: 6
Video Position: 0

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outli
ne, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Style1,Candara,70,&H00FFFFFF,&H0080FFFF,&H00306000,&H80306000,0,0,0,0,100,100,0,0,1,3,2,2,30,30,25,0
Style: Style2,Candara,60,&H00FFFFFF,&H0080FFFF,&H00603000,&H80603000,0,0,0,0,100,100,0,0,1,3,2,8,30,30,25,0

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:01:48.70,0:01:50.23,Marked=0,Style1,Comment,0000,0000,0000,,Pathetic.
Dialogue: 0,0:01:50.31,0:01:54.11,Marked=0,Style1,Comment,0000,0000,0000,,Can't ask much from a beginner\nwho just joined the game.
Dialogue: 0,0:02:11.36,0:02:14.45,Marked=0,Style1,Comment,0000,0000,0000,,That was a disaster.\NYou'd better be careful.
Dialogue: 0,0:02:15.03,0:02:18.02,Marked=0,Style1,Comment,0000,0000,0000,,Right now, there are\nmany PK's in "The World".
Dialogue: 0,0:02:19.67,0:02:20.43,Marked=0,Style1,Comment,0000,0000,0000,,PK?
Dialogue: 0,0:02:21.40,0:02:23.06,Marked=0,Style1,Comment,0000,0000,0000,,Player Killers.
Dialogue: 0,0:02:23.14,0:02:24.94,Marked=0,Style1,Comment,0000,0000,0000,,They hunt and kill other players.
Dialogue: 0,0:02:26.01,0:02:27.70,Marked=0,Style1,Comment,0000,0000,0000,,Like those guys before?

For now I'm removing Marked=0 which helps a lot, because Adding Marked in Format before Name does not help

Update packaging to use PEP-517

setup.py has been deprecated and should not be used anymore. pip already prints out warnings when installing sdists with a setup.py.

We should instead move to PEP-517 and use a dependency/package manager that provides proper tooling around pyproject.toml. poetry, flit and pdm come to mind.

custom attributes present in "Format" in sections

I am dealing .ass files which have Event section like this:

[Events]
Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:01:50.64,0:01:54.42,Main03,Kondo,0,0,0,,Prisoner 3-2034.

and i want to access the Actor attribute, how do i do that?

How can I add something to the ScriptInfoSection?

With doc.info[...] you can read the different infos.

But how can I add or change something to the current script if something is missing e.g. PlayResX: 640 to PlayResX: 720 or add ScaledBorderAndShadow if it is not present.

Write documentation

Documentation is almost non-existant. The readme shows some neat stuff you can do, but that's still way too shallow.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.