Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to run Aegisub's resolution resampler in a python script? #29

Open
knaik95 opened this issue Jan 1, 2020 · 7 comments
Open

Comments

@knaik95
Copy link

knaik95 commented Jan 1, 2020

Filtering and merging two .ass files of differing PlayRes's makes the subs from the lower resolution file minuscule and off center.

@tkarabela
Copy link
Owner

Would something like this help? :)

def resize_subs(subs, res_x_dest=1920):
	res_x_src = int(subs.info["PlayResX"])
	res_y_src = int(subs.info["PlayResY"])
	scale = res_x_dest / float(res_x_src)
	res_y_dest = int(scale * res_y_src)

	# metadata
	subs.info["PlayResX"] = str(res_x_dest)
	subs.info["PlayResY"] = str(res_y_dest)

	# styles
	for style in subs.styles.values():
		style.fontsize *= scale
		style.marginl *= scale
		style.marginr *= scale
		style.marginv *= scale
		style.outline *= scale
		style.shadow *= scale
		style.spacing *= scale

	# events
	# XXX

If you have override tags in individual subtitles, fixing those would be more tricky, though perhaps fixing \pos and \fs via regular expressions would be good enough? pysubs2 does not really have a good way of working with override tags, apart from pysubs2.substation.parse_tags().

Aegisub's resolution resampler is implemented in C++ ( https://github.com/Aegisub/Aegisub/blob/ce658d070925effea8c626b2ada2f819d01ab4fb/src/resolution_resampler.cpp ) and is not exposed in its Lua API, as far as i can tell ( http://docs.aegisub.org/3.2/Automation/ ).

@knaik95
Copy link
Author

knaik95 commented Jan 1, 2020

Yeah that's more or less what I have. It's the individual subtitle tags that I'm having an issue with. I can set up the regex but I have to find all the tags I have to scale. So far I just have \pos being scaled up. The sample .ass file I'm using has sparkles that are supposed to be behind the song lyrics (karaoke) but after scaling \pos, the sparkles stay where they were.

The tags look like this: \fad(1000,0)\an5\pos(381,35\fscx100\fscy100).

I'm not really sure what the \fad, \an, \fscx, and \fscy even do so I don't know if they need scaling and/or what to scale them by.

There are also \move tags in other subtitles. I scaled those up and the text got stretched. \move has 4 parameters. Do I scale every other one?

I downloaded the aegisub source code from the github and want to send the needed parameters to resolution_resampler.cpp to scale everything before running the main script but can't get C++ (clang) to work with VSCode.

@tkarabela
Copy link
Owner

Aegisub docs have a list of ASS override tags and their parameters: http://docs.aegisub.org/manual/ASS_Tags

You should scale everything which is given in pixels: font size and spacing, outline, shadow, margins, \pos, \org, first four parameters of \move, and coordinates for vector drawing stuff (\p) if you have that, too. At that point, it may be more simple to do the resizing the other way around, keeping the effects as they are.

If I remember correctly, the resolution given in ASS info section is virtual, anyway; during playback, the renderer rasterizes it depending on the actual video resolution (with some supersampling, 2x or 4x I think). So even if you have 640x480 ASS subtitles, they should not look blocky with full HD video. At least that was my experience with MPC-HC some years back.

The C++ code I linked more for reference, it would most likely be faster to reimplement it from scratch in Python than frankenstein the backend of a monolithic C++ app to do something useful :)

@knaik95
Copy link
Author

knaik95 commented Jan 1, 2020

The issue I'm facing is that one .ass file is 720p and another is 1080p, so when I bring the styles and subs from 720 into 1080, all the 720 ones are drawn in a 1080 window, meaning they're essentially in their own little 720p layer on top of the already present 1080p subs.

Re-implementing the C++ in Python requires knowing the C++ syntax :(

Most of the syntax is easy enough but other parts are a bit confusing. For example:

enum class YCbCrMatrix : int {
	rgb,
	tv_601,
	pc_601,
	tv_709,
	pc_709,
	tv_fcc,
	pc_fcc,
	tv_240m,
	pc_240m
};

What does the : int in the first line mean?

@knaik95
Copy link
Author

knaik95 commented Jan 10, 2020

....and I've pretty much reimplemented the backend in Python. Just a couple classes left.

@fagnerpatricio
Copy link

I Would like this function too, i have many problem wiith this too

@tkarabela
Copy link
Owner

If anyone wants to implement this, please feel free to open a pull request. Sketch of the implementation is in #29 (comment) but it would also need to support \pos tags, drawing tags, etc. It should be a new method of SSAFile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@fagnerpatricio @knaik95 @tkarabela and others