View Full Version : ConvertoToXXX - "like the second file"
I am missing the clip.ConvertTo(clip From) function...
How do I convert a clip so that it has the same parameters as another clip (mainly the YUV space and the matrix).
I tried to come up with something like:
#eval("""ConvertTo""" + Source.PixelType + """(matrix=Source.propGetString("_Matrix"))""")
But "matrix" unfortunately does not accept int values.
Sharc
9th March 2025, 11:14
Maybe easiest is to convert the number into a string variable - e.g. "matrix" - which is then used like ConvertToxxx(matrix=matrix,.....)
gispos
9th March 2025, 18:25
For the matrix, propGet and propSet should work, but I have never used them.
For color space 'BuildPixelType' returns a string that can be used with LWLibavVideoSource, for example.
c1=LWLibavVideoSource("E:\test.mkv", cache=False)
pt=BuildPixelType(sample_clip=c1)
# c2 will have the same color space as c1
c2=LWLibavVideoSource("E:\test2.mkv", cache=False, format=pt)
hello_hello
9th March 2025, 22:19
Keep in mind that matrix only has an effect when converting between RGB and YUV.
For the function below, when converting an RGB clip to YUV, the default is to use the matrix of the YUV clip for the conversion.
If it needs to be converted using a different matrix, or if there's no matrix in frame properties, you can specify one.
When converting a YUV clip to RGB, the matrix of the YUV clip is used as it normally would be.
Once again you can specify something else if need be, or if there's no matrix in frame properties.
If you're converting between YUV formats and want to change the matrix in frame properties without actually converting the colors, use propSet("_Matrix", INTEGER)
function ConvertAtoB(clip A, clip B, string "Matrix", int "Bits") {
PropMatrixA = propGetAny(A, "_Matrix")
PropMatrixB = propGetAny(B, "_Matrix")
PropChroma = propGetAny(B, "_ChromaLocation")
PropMatrixStrA = !defined(PropMatrixA) ? undefined() : \
(PropMatrixA == 0) ? "rgb" : (PropMatrixA == 1) ? "709" : (PropMatrixA == 4) ? "fcc" : \
(PropMatrixA == 5) ? "601" : (PropMatrixA == 6) ? "170m" : (PropMatrixA == 7) ? "240m" : \
(PropMatrixA == 9) ? "2020" : (PropMatrixA == 10) ? "2020cl" : undefined()
PropMatrixStrB = !defined(PropMatrixB) ? undefined() : \
(PropMatrixB == 0) ? "rgb" : (PropMatrixB == 1) ? "709" : (PropMatrixB == 4) ? "fcc" : \
(PropMatrixB == 5) ? "601" : (PropMatrixB == 6) ? "170m" : (PropMatrixB == 7) ? "240m" : \
(PropMatrixB == 9) ? "2020" : (PropMatrixB == 10) ? "2020cl" : undefined()
PropChromaStr = !defined(PropChroma) ? undefined() : \
(PropChroma == 0) ? "left" : (PropChroma == 1) ? "center" : (PropChroma == 2) ? "top_left" : \
(PropChroma == 3) ? "top" : (PropChroma == 4) ? "Bottom_left" : (PropChroma == 5) ? "Bottom" : \
(PropChroma == 6) ? "dv" : undefined()
MatrixStr = default(Matrix, IsRGB(A) ? PropMatrixStrB : PropMatrixStrA)
Bits = default(Bits, BitsPerComponent(B))
ColorFamilyStr = IsRGB(B) ? "RGB" : IsY(B) ? "Y" : "YUV"
SubSampleStr = IsRGB(B) || IsY(B) ? "" : Is420(B) ? "420" : Is422(B) ? "422" : "444"
BitsStr = (Bits == BitsPerComponent(A)) ? "" : ".ConvertBits(Bits)"
ChromaStr = Is420(B) && defined(PropChromaStr) ? ", ChromaOutPlacement=PropChromaStr" : ""
return Eval("A.ConvertTo" + ColorFamilyStr + SubSampleStr + "(matrix=MatrixStr" + ChromaStr + ")" + BitsStr) }
A = BlankClip()
B = A.ConvertToYV12().ConvertBits(16)
# ConvertAtoB(A, B)
# ConvertAtoB(B, A)
rgr
13th March 2025, 11:34
Maybe easiest is to convert the number into a string variable - e.g. "matrix" - which is then used like ConvertToxxx(matrix=matrix,.....)
I know, but I thought it would be possible to do it without unnecessary writing.
Keep in mind that matrix only has an effect when converting between RGB and YUV.
For the function below, when converting an RGB clip to YUV, the default is to use the matrix of the YUV clip for the conversion.
If it needs to be converted using a different matrix, or if there's no matrix in frame properties, you can specify one.
Well, I thought there was a simple trick to convert RGB->YUV based on the properties of another clip, but I see there isn't.
Thanks.
Edit: The function is not _ColorRange aware as I see.
StvG
13th March 2025, 18:37
Well, I thought there was a simple trick to convert RGB->YUV based on the properties of another clip, but I see there isn't.
Thanks.
# YUV clip
clip_with_props_to_be_used = z_ConvertFormat(use_props=3)
# RGB clip to YUV with props from clip_with_props_to_be_used
rgb_clip = ...
propCopy(rgb_clip, clip_with_props_to_be_used, merge=true, props="z_*") # Needs avs+ >= r4610.
z_ConvertFormat(pixel_type=PixelType(clip_with_props_to_be_used), use_props=4)
rgr
17th March 2025, 12:08
Thanks. I think that's what I meant.
clip_with_props_to_be_used = z_ConvertFormat(use_props=3)
It almost worked right away.
Unfortunately, the function requires the Transfer and Primiaries properties to be set. Strange, because they are not needed at all, at least for BT601/709.
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.