A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloudinary.com/documentation/video_user_defined_variables below:

Transform Videos with Variables & Arithmetic Operators

You can add arithmetic and/or user-defined variables to your transformations. These expressions are evaluated on the fly, enabling you to add an additional layer of sophistication and flexibility to your transformations.

User-defined variables are especially valuable when used within named transformations. This enables complete separation of the transformation from the varying values used for delivery. This also makes it significantly easier to reuse common transformations for many assets, even when some specific adjustments must be made to the transformation depending on the specific asset or other data passed from another source.

You can achieve many complex transformation goals by using user-defined variables in conjunction with arithmetic expressions and conditional transformations.

See also: Variable and arithmetic image transformations.

Arithmetic expressions

You can create arithmetic expressions by using arithmetic operators with numeric transformation parameters or user-defined variables.

The following example demonstrates using an arithmetic expression to define the final delivery width of the video to be 20% of the video's original width.

When you use multiple arithmetic operators in an expression, standard order of operations apply (multiplication and division before addition and subtraction).

For example, iw_mul_2_add_ih_mul_2 would give you the perimeter measurement of the initial asset by first multiplying both the initial width by 2 and the initial height by 2, and then adding these two products.

Supported arithmetic operators operation URL syntax SDK syntax add add + subtract sub - multiply mul * divide div / modulo (remainder) mod % pow
(to the power of) pow ^ User-defined variables - overview

To use user-defined variables in your transformations, you first declare and assign values to the variables you want to use. In later components of your chained transformation, you can use the variables as the values of your transformation parameters.

Variable naming

User-defined variables start with the $ sign, for example $newwidth. The name can include only alphanumeric characters and must begin with a letter.

Variable value types

Variables can be assigned a number value, string value, or they can take on the value of a numeric asset characteristic, such as iw (initial width) or du (duration).

When working with string values:

Assigning values to variables

Use the underscore to assign a value to a variable. For example:

To convert a string value to a numeric value, append _to_i for an integer, or _to_f for a float. For example, to assign the value of the contextual metadata key, breadth, to the variable $newwidth:

If assigning structured metadata to a variable, and the metadata type is numeric, you do not need to specify _to_i. For example, if you have a number field with external ID, position, you can use the syntax: $newwidth_md:!position!. However, if it is a text field you still need to append _to_i or _to_f to convert it to an integer or float.

Using arithmetic expressions with variables

You can use arithmetic operators with numeric variables just as you do with numeric transformation parameters, or even in combination. Consider this example:

$small_150/$big_2_mul_$small/c_fill,h_$small_add_20,w_$big

Testing whether a variable has been defined

You can test whether or not a variable has been defined using the parameters if_isdef_$<variable name> and if_isndef_$<variable name>.

For example, check if the $big variable has been defined, and if so, set the width:

if_isdef_$big/c_scale,w_$big/if_end

Similarly, if the $small variable is not defined, set it to a default value:

if_isndef_$small/$small_300/if_end

Learn more about conditional transformations.

Variable types and supported parameters

You can use user-defined variables with videos to compare how the current value of a parameter relates to the assigned value of a user-defined variable, or you can transform your video by setting the value of a parameter with a variable.

There are currently limitations on which parameters you can set a value for using a variable. The following sections describe all those that Cloudinary supports.

Using variables to resize a video

You can use variables for both the width and height parameters.

The following example defines two variables: $mainvideowidth and $overlaywidth. The $overlaywidth is $mainvideowidth divided by 3.

You can't use variables declared on a video for use within an

image overlay

on a video.

Using variables in text overlays

You can use a variable for all or part of the text value in a text overlay.

To mix static text with a string variable when specifying a text overlay, use the syntax: static text $(variable) more static text. For example:

You can also use a variable for the text color. For example:

In this example, set the variable $award to the string value !First!. Then set the text overlay to use the text $(award) Place. The color of the text is also set using a variable ($color). The text overlay is placed near the bottom of the image on a semi-transparent, rounded border.

You can also populate the text or color from contextual metadata or structured metadata. For example:

For structured metadata, use the syntax:

md:!<external_id>!

. Be sure to use the external ID, not the label.

Using variables in video conditions

You can check the value of a video characteristic against a user-defined variable value within the IF part of conditional transformation.

For this purpose, you can use any parameter that is supported within conditional transformations.

In the example below, the conditional transformation checks whether the video has a certain tag. If it does, the video is delivered at a high quality level of 90. Otherwise, it's delivered at a low quality level of 30. The value of the tag that controls this behavior is declared using the $qualtag variable. In this case, only videos that are tagged as 'Pro' will be delivered with high quality.

The above transformation is running on 'meadow_walk', which has a tag value of 'Pro', and thus it's delivered with a quality value of 90. If you use the same transformation as above with the 'meadow_walk2' video, which is identical, except without the 'Pro' tag, it's delivered with a low quality value of 30.

Expression evaluation order

Within a transformation component, condition (if) statements are evaluated first, then variable assignments, then transformations. Therefore:

However, the best practice is to separate conditions from results in separate URL components in the order you want them evaluated. For example: if_w_gt_5/$x_5/c_scale,w_$x/if_end, as explained in Conditional transformations.

Using variables with named transformations

By using variables to set the determining values of a condition, you could then define the conditional transformation as a named transformation that could be used with many video assets, and assign the variable value externally.

For example, you could set the named transformation highqual-if-tagged to a transformation similar to the one we used above for the kitten video: if_$qualtag_in_tags,q_80/if_else,q_50/w_$delwidth, and then use it to deliver any video, controlling both the quality and final delivery width by setting the relevant variable values. In this case, the quality condition is applied if the video has a tag set to important. Regardless of the tag, the delivery width will be 450px.

When you use variable or conditional expressions that include the

tags

,

ctx

or

md

parameters, their values are exposed publicly in the URL. If you want to prevent such values from being exposed, you can disable the

Usage of tags/context/metadata in transformation URLs

option in the

Security

Settings (enabled by default). When this setting is disabled, any URL that exposes tags, contextual metadata or structured metadata values will return an error.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4