This page explains how to insert overlays into transcoded videos. An overlay
consists of an image that is inserted on top of the output video, and can
optionally be faded in or out over a specified time period. To insert an
overlay, use the
overlays
array in
the JobConfig
template.
See the list of supported image file formats.
Upload an image to Cloud Storage
To get started, do the following to upload an overlay image to your Cloud Storage bucket:
- In the Google Cloud console, go to the Cloud Storage Browser page.
Go to the Cloud Storage Browser page - Click the name of your bucket to open it.
- Click Upload files.
- Select an image file to upload from your local machine.
Create an overlay
You can create two types of overlays: static or animated. Both types of overlays use a static image. You can show or hide static overlays. Animated overlays support fade in and fade out animations of the image.
You can insert multiple overlays into a single output video.
Create a static overlay
In the image
object, use the
uri
field to
specify the overlay image in Cloud Storage. In the resolution
object, set
the x and y values from 0 to 1.0. A value of 0 maintains the source image
resolution for that dimension; a value of 1.0 will stretch the image to match
the dimension of the output video. For example, use the values x: 1
and y:
0.5
to stretch the overlay image the full width and half of the height of the
output video.
In the animations
array, create an animationStatic
object with x and y
coordinates from 0 to 1.0. These coordinates are based on the output video
resolution. Use the values x: 0
and y: 0
to position the top-left corner of
the overlay in the top-left corner of the output video. Specify when the overlay
should appear in the output video timeline using the startTimeOffset
field.
To remove the static animation, create an animationEnd
object. Specify when
the animation should end (that is, the overlay should disappear) in the output
video timeline using the startTimeOffset
field.
You can add this configuration to a job template or include it in an ad-hoc job configuration:
REST
Before using any of the request data, make the following replacements:
PROJECT_ID
: Your Google Cloud project ID listed in the IAM Settings.LOCATION
: The location where your job will run. Use one of the supported regions.Show locationsus-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
northamerica-northeast1
asia-east1
asia-northeast1
asia-northeast3
asia-south1
asia-southeast1
australia-southeast1
europe-west1
europe-west2
europe-west4
STORAGE_BUCKET_NAME
: The name of the Cloud Storage bucket you created.STORAGE_INPUT_VIDEO
: The name of the video in your Cloud Storage bucket that you are transcoding, such asmy-vid.mp4
. This field should take into account any folders that you created in the bucket (for example,input/my-vid.mp4
).STORAGE_INPUT_OVERLAY
: The name of the image in your Cloud Storage bucket that you are using for the overlay, such asmy-overlay.png
. This field should take into account any folders that you created in the bucket (for example,input/my-overlay.png
).STORAGE_OUTPUT_FOLDER
: The Cloud Storage folder name where you want to save the encoded video outputs.
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID", "config": { ... }, "state": "PENDING", "createTime": CREATE_TIME, "ttlAfterCompletionDays": 30 }
gcloud
- Create a
request.json
file that defines the job fields. Make the following replacements for thegcloud
command:- STORAGE_BUCKET_NAME: The name of the Cloud Storage bucket you created.
- STORAGE_INPUT_VIDEO: The
name of the video in your Cloud Storage bucket that you are
transcoding, such as
my-vid.mp4
. This field should take into account any folders that you created in the bucket (for example,input/my-vid.mp4
). - STORAGE_INPUT_OVERLAY: The name of the
image file in your Cloud Storage bucket that you are using for the overlay, such as
my-overlay.png
. This field should take into account any folders that you created in the bucket (for example,input/my-overlay.png
). - LOCATION: The location where
your job will run. Use a location from the following list.
Show locations
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
northamerica-northeast1
asia-east1
asia-northeast1
asia-northeast3
asia-south1
asia-southeast1
australia-southeast1
europe-west1
europe-west2
europe-west4
- STORAGE_OUTPUT_FOLDER: The Cloud Storage folder name where you want to save the encoded video outputs.
{ "config": { "inputs": [ { "key": "input0", "uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO" } ], "elementaryStreams": [ { "key": "video-stream0", "videoStream": { "h264": { "heightPixels": 360, "widthPixels": 640, "bitrateBps": 550000, "frameRate": 60 } } }, { "key": "audio-stream0", "audioStream": { "codec": "aac", "bitrateBps": 64000 } } ], "muxStreams": [ { "key": "sd", "container": "mp4", "elementaryStreams": [ "video-stream0", "audio-stream0" ] } ], "output": { "uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/" }, "overlays": [ { "image": { "uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_OVERLAY", "resolution": { "x": 1, "y": 0.5 }, "alpha": 1 }, "animations": [ { "animationStatic": { "xy": { "x": 0, "y": 0 }, "startTimeOffset": "0s" } }, { "animationEnd": { "startTimeOffset": "10s" } } ] } ] } }
- Run the following command:
You should see a response similar to the following:gcloud transcoder jobs create --location=LOCATION --file="request.json"
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID", "config": { ... }, "state": "PENDING", "createTime": CREATE_TIME, "ttlAfterCompletionDays": 30 }
C#
Before trying this sample, follow the C# setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API C# API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
Before trying this sample, follow the Go setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Go API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Java API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Node.js API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
Before trying this sample, follow the PHP setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API PHP API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Python API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
Before trying this sample, follow the Ruby setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Ruby API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
In the output video, the static overlay has the following characteristics:
- It appears at the beginning of the timeline and is visible for 10 seconds.
- It stretches the full width and half of the height of the output video.
- It is positioned in the top-left corner of the output video.
See the sample output video for this configuration. This video uses a sample overlay image.
Create an animated overlay
In the image
object, use the
uri
field to
specify the overlay image in Cloud Storage. In the resolution
object, set
the x and y values from 0 to 1.0. A value of 0 maintains the source image
resolution for that dimension; a value of 1.0 will stretch the image to match
the dimension of the output video. For example, use the values x: 0
and y: 0
to maintain the original resolution of the overlay image.
In the animations
array, create an animationFade
object with a fadeType
of
FADE_IN
. Set the x and y coordinates from 0 to 1.0. These coordinates are
based on the output video resolution. Use the values x: 0.5
and y: 0.5
to
position the top-left corner of the overlay in the center of the output video.
Specify when the overlay should start to appear in the output video timeline
using the startTimeOffset
field. The overlay should be fully visible by the
time set in the endTimeOffset
field.
To fade out the overlay, create another animationFade
object. This time, set
the fadeType
to FADE_OUT
. Input the position coordinates and start and end
times as before.
You can add this configuration to a job template or include it in an ad-hoc job configuration:
REST
Before using any of the request data, make the following replacements:
PROJECT_ID
: Your Google Cloud project ID listed in the IAM Settings.LOCATION
: The location where your job will run. Use one of the supported regions.Show locationsus-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
northamerica-northeast1
asia-east1
asia-northeast1
asia-northeast3
asia-south1
asia-southeast1
australia-southeast1
europe-west1
europe-west2
europe-west4
STORAGE_BUCKET_NAME
: The name of the Cloud Storage bucket you created.STORAGE_INPUT_VIDEO
: The name of the video in your Cloud Storage bucket that you are transcoding, such asmy-vid.mp4
. This field should take into account any folders that you created in the bucket (for example,input/my-vid.mp4
).STORAGE_INPUT_OVERLAY
: The name of the image in your Cloud Storage bucket that you are using for the overlay, such asmy-overlay.png
. This field should take into account any folders that you created in the bucket (for example,input/my-overlay.png
).STORAGE_OUTPUT_FOLDER
: The Cloud Storage folder name where you want to save the encoded video outputs.
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID", "config": { ... }, "state": "PENDING", "createTime": CREATE_TIME, "ttlAfterCompletionDays": 30 }
gcloud
- Create a
request.json
file that defines the job fields. Make the following replacements for thegcloud
command:- STORAGE_BUCKET_NAME: The name of the Cloud Storage bucket you created.
- STORAGE_INPUT_VIDEO: The
name of the video in your Cloud Storage bucket that you are
transcoding, such as
my-vid.mp4
. This field should take into account any folders that you created in the bucket (for example,input/my-vid.mp4
). - STORAGE_INPUT_OVERLAY: The name of the
image file in your Cloud Storage bucket that you are using for the overlay, such as
my-overlay.png
. This field should take into account any folders that you created in the bucket (for example,input/my-overlay.png
). - LOCATION: The location where
your job will run. Use a location from the following list.
Show locations
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
northamerica-northeast1
asia-east1
asia-northeast1
asia-northeast3
asia-south1
asia-southeast1
australia-southeast1
europe-west1
europe-west2
europe-west4
- STORAGE_OUTPUT_FOLDER: The Cloud Storage folder name where you want to save the encoded video outputs.
{ "config": { "inputs": [ { "key": "input0", "uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO" } ], "elementaryStreams": [ { "key": "video-stream0", "videoStream": { "h264": { "heightPixels": 360, "widthPixels": 640, "bitrateBps": 550000, "frameRate": 60 } } }, { "key": "audio-stream0", "audioStream": { "codec": "aac", "bitrateBps": 64000 } } ], "muxStreams": [ { "key": "sd", "container": "mp4", "elementaryStreams": [ "video-stream0", "audio-stream0" ] } ], "output": { "uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/" }, "overlays": [ { "image": { "uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_OVERLAY", "resolution": { "x": 0, "y": 0 }, "alpha": 1 }, "animations": [ { "animationFade": { "fadeType": "FADE_IN", "xy": { "x": 0.5, "y": 0.5 }, "startTimeOffset": "5s", "endTimeOffset": "10s" } }, { "animationFade": { "fadeType": "FADE_OUT", "xy": { "x": 0.5, "y": 0.5 }, "startTimeOffset": "12s", "endTimeOffset": "15s" } } ] } ] } }
- Run the following command:
You should see a response similar to the following:gcloud transcoder jobs create --location=LOCATION --file="request.json"
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID", "config": { ... }, "state": "PENDING", "createTime": CREATE_TIME, "ttlAfterCompletionDays": 30 }
C#
Before trying this sample, follow the C# setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API C# API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
Before trying this sample, follow the Go setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Go API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Java API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Node.js API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
Before trying this sample, follow the PHP setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API PHP API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Python API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
Before trying this sample, follow the Ruby setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Ruby API reference documentation.
To authenticate to Transcoder API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
In the resulting video, the animated overlay has the following characteristics:
- It starts to fade in at the 5-second mark in the output video. The alpha value for the overlay starts at 0 and ends at 1.0. The top-left corner of the overlay appears at the center of the output video. The overlay appears at the original resolution of the overlay image.
- After it fades in, the overlay shows for 2 seconds.
- It starts to fade out at the 12-second mark in the output video. The alpha value for the overlay starts at 1.0 and ends at 0.
- The animation disappears by the 15-second mark.
See the sample output video for this configuration. This video uses a sample overlay image.