2
2
import random
3
3
from typing import Any
4
4
5
- from vstools import (CustomIndexError , CustomValueError , FuncExceptT , KwargsT ,
6
- check_variable_resolution , core , fallback , get_h , get_w ,
7
- vs )
5
+ from vstools import (CustomIndexError , CustomValueError , Dar , FuncExceptT ,
6
+ KwargsT , check_variable_resolution , core , fallback , get_h ,
7
+ get_w , vs )
8
8
9
9
__all__ = [
10
10
'colored_clips' ,
@@ -64,9 +64,10 @@ def colored_clips(
64
64
65
65
66
66
def get_match_centers_scaling (
67
- clip : vs .VideoNode ,
67
+ base_dimensions : vs .VideoNode | tuple [ int , int ] = ( 1920 , 1080 ) ,
68
68
target_width : int | None = None ,
69
69
target_height : int | None = 720 ,
70
+ dar : Dar | None = None ,
70
71
func_except : FuncExceptT | None = None
71
72
) -> KwargsT :
72
73
"""
@@ -102,8 +103,8 @@ def get_match_centers_scaling(
102
103
103
104
The formula for calculating values we can use during desampling is simple:
104
105
105
- * width: clip.width * (target_width - 1) / (clip.width - 1)
106
- * height: clip.height * (target_height - 1) / (clip.height - 1)
106
+ * width: base_width * (target_width - 1) / (base_width - 1)
107
+ * height: base_height * (target_height - 1) / (base_height - 1)
107
108
108
109
Example usage:
109
110
@@ -117,18 +118,23 @@ def get_match_centers_scaling(
117
118
The output is meant to be passed to `vodesfunc.DescaleTarget` as keyword arguments,
118
119
but it may also apply to other functions that require similar parameters.
119
120
120
- :param clip: The clip to base the calculations on.
121
- :param target_width: Target width for the descale. This should probably be equal to the base width.
122
- If not provided, this value is calculated using the `target_height`.
123
- Default: None.
124
- :param target_height: Target height for the descale. This should probably be equal to the base height.
125
- If not provided, this value is calculated using the `target_width`.
126
- Default: 720.
127
- :param func_except: Function returned for custom error handling.
128
- This should only be set by VS package developers.
129
-
130
- :return: A dictionary with the keys, {width, height, base_width, base_height},
131
- which can be passed directly to `vodesfunc.DescaleTarget` or similar functions.
121
+ :param base_dimensions: The base dimensions to base the calculations on. This may be derived from
122
+ a given clip or a tuple of (Width, Height).
123
+ Default: (1920, 1080)
124
+ :param target_width: Target width for the descale. This should probably be equal to the base width.
125
+ If not provided, this value is calculated using the `target_height`.
126
+ Default: None.
127
+ :param target_height: Target height for the descale. This should probably be equal to the base height.
128
+ If not provided, this value is calculated using the `target_width`.
129
+ Default: 720.
130
+ :param dar: Display aspect ratio. Used for calculating the width/height if either is None.
131
+ This is used for anamorphic sources. If None, derive from `base_dimensions`.
132
+ Default: None.
133
+ :param func_except: Function returned for custom error handling.
134
+ This should only be set by VS package developers.
135
+
136
+ :return: A dictionary with the keys, {width, height, base_width, base_height},
137
+ which can be passed directly to `vodesfunc.DescaleTarget` or similar functions.
132
138
"""
133
139
134
140
func = fallback (func_except , get_match_centers_scaling )
@@ -140,14 +146,20 @@ def get_match_centers_scaling(
140
146
if target is not None and (not isinstance (target , int ) or target <= 0 ):
141
147
raise CustomValueError (f"`target_{ name } ` must be a positive integer or None." , func )
142
148
143
- check_variable_resolution (clip , func )
149
+ if isinstance (base_dimensions , vs .VideoNode ):
150
+ check_variable_resolution (base_dimensions , func )
151
+
152
+ base_dimensions = (base_dimensions .width , base_dimensions .height )
153
+
154
+ base_width , base_height = base_dimensions
155
+ dar = dar or Dar .from_size (* base_dimensions )
144
156
145
157
if target_height is None :
146
- target_height = get_h (target_width , clip , 1 )
158
+ target_height = get_h (target_width , dar , 1 )
147
159
elif target_width is None :
148
- target_width = get_w (target_height , clip , 1 )
160
+ target_width = get_w (target_height , dar , 1 )
149
161
150
- width = clip . width * (target_width - 1 ) / (clip . width - 1 )
151
- height = clip . height * (target_height - 1 ) / (clip . height - 1 )
162
+ width = base_width * (target_width - 1 ) / (base_width - 1 )
163
+ height = base_height * (target_height - 1 ) / (base_height - 1 )
152
164
153
165
return KwargsT (width = width , height = height , base_width = target_width , base_height = target_height )
0 commit comments