Transcode a video with the Transcoder API
This page shows you how to create a basic video transcoding job using the
default settings of the Transcoder API and curl
, Windows PowerShell, or the
client libraries.
You can also perform this quickstart directly in the console using either of the following programming languages:
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the Transcoder API.
-
Create a service account:
-
In the console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
To provide access to your project, grant the following role(s) to your service account: Transcoder > Transcoder Admin, Cloud Storage > Storage Admin .
In the Select a role list, select a role.
For additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the Transcoder API.
-
Create a service account:
-
In the console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
To provide access to your project, grant the following role(s) to your service account: Transcoder > Transcoder Admin, Cloud Storage > Storage Admin .
In the Select a role list, select a role.
For additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again. - Install and initialize the Google Cloud CLI.
- Use the
gcloud
tool to test your authentication environment. Check that the following command outputs a text string without errors:gcloud auth application-default print-access-token
Create a Cloud Storage bucket
- In the console, go to the Cloud Storage Buckets page.
- Click Create bucket.
- On the Create a bucket page, enter your bucket information. To go to the next
step, click Continue.
- For Name your bucket, enter a unique bucket name. Don't include sensitive information in the bucket name, because the bucket namespace is global and publicly visible.
-
For Choose where to store your data, do the following:
- Select a Location type option.
- Select a Location option.
- For Choose a default storage class for your data, select the following: Standard.
- For Choose how to control access to objects, select an Access control option.
- For Advanced settings (optional), specify an encryption method, a retention policy, or bucket labels.
- Click Create.
- Click Create Folder and enter a name to create a folder in which to save the encoded video outputs.
Transcode a video using the Transcoder API
You need a video at least 5 seconds in duration from your local machine for the following steps (sample video). See the list of supported input and output formats.
Upload a video to your Cloud Storage bucket
- In the 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 a video file to upload from your local machine.
Your video is now saved to the Cloud Storage bucket.
Create a transcoding job
By default, the Transcoder API transcodes video using H.264 encoding and MP4, HLS, and MPEG-DASH packaging. For each input video, it delivers output renditions in high definition (1280x720 pixels) and standard definition (640x360 pixels).
To create a job, use the projects.locations.jobs.create
method. The following code samples create a basic transcoding job using a
preset.
REST & CMD LINE
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:
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
asia-east1
asia-south1
asia-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 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_OUTPUT_FOLDER: The Cloud Storage folder name where you want to save the encoded video outputs.
Request JSON body:
{ "inputUri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO", "outputUri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/", "templateId": "preset/web-hd" }
To send your request, expand one of these options:
This particular REST example uses the optional
fields
query parameter to only
show the created resource name in the response.
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID" }
gcloud
- Make the following replacements for the
gcloud
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
). - LOCATION: The location where
your job will run. Use one of the supported regions:
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
asia-east1
asia-south1
asia-southeast1
europe-west1
europe-west2
europe-west4
- STORAGE_OUTPUT_FOLDER: The Cloud Storage folder name where you want to save the encoded video outputs.
- Run the following command:
gcloud transcoder jobs create \ --input-uri="gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO" \ --location=LOCATION \ --output-uri="gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/"
You should see a response similar to the following:{ "config": { ... }, "createTime": CREATE_TIME, "name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID", "state": "PENDING", "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.
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.
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.
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.
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.
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.
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.
Copy the returned JOB_ID. You need it to get the status of the job.
Check the status of your transcoding job
To check the status of a job, use the projects.locations.jobs.get
method. The following code samples
get the job details and then
show the job state.
REST & CMD LINE
Before using any of the request data, make the following replacements:
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
asia-east1
asia-south1
asia-southeast1
europe-west1
europe-west2
europe-west4
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": { "inputs": [ { "key": "input0", "uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO" } ], "editList": [ { "key": "atom0", "inputs": [ "input0" ], "startTimeOffset": "0s" } ], "elementaryStreams": [ { "videoStream": { "h264": { "widthPixels": 640, "heightPixels": 360, "frameRate": 30, "bitrateBps": 550000, "pixelFormat": "yuv420p", "rateControlMode": "vbr", "crfLevel": 21, "gopDuration": "3s", "vbvSizeBits": 550000, "vbvFullnessBits": 495000, "entropyCoder": "cabac", "bFrameCount": 3, "aqStrength": 1, "profile": "high", "preset": "veryfast" } }, "key": "video-stream0" }, { "videoStream": { "h264": { "widthPixels": 1280, "heightPixels": 720, "frameRate": 30, "bitrateBps": 2500000, "pixelFormat": "yuv420p", "rateControlMode": "vbr", "crfLevel": 21, "gopDuration": "3s", "vbvSizeBits": 2500000, "vbvFullnessBits": 2250000, "entropyCoder": "cabac", "bFrameCount": 3, "aqStrength": 1, "profile": "high", "preset": "veryfast" } }, "key": "video-stream1" }, { "audioStream": { "codec": "aac", "bitrateBps": 64000, "channelCount": 2, "channelLayout": [ "fl", "fr" ], "sampleRateHertz": 48000 }, "key": "audio-stream0" } ], "muxStreams": [ { "key": "sd", "fileName": "sd.mp4", "container": "mp4", "elementaryStreams": [ "video-stream0", "audio-stream0" ] }, { "key": "hd", "fileName": "hd.mp4", "container": "mp4", "elementaryStreams": [ "video-stream1", "audio-stream0" ] }, { "key": "media-sd", "fileName": "media-sd.ts", "container": "ts", "elementaryStreams": [ "video-stream0", "audio-stream0" ] }, { "key": "media-hd", "fileName": "media-hd.ts", "container": "ts", "elementaryStreams": [ "video-stream1", "audio-stream0" ] }, { "key": "video-only-sd", "fileName": "video-only-sd.m4s", "container": "fmp4", "elementaryStreams": [ "video-stream0" ] }, { "key": "video-only-hd", "fileName": "video-only-hd.m4s", "container": "fmp4", "elementaryStreams": [ "video-stream1" ] }, { "key": "audio-only", "fileName": "audio-only.m4s", "container": "fmp4", "elementaryStreams": [ "audio-stream0" ] } ], "manifests": [ { "fileName": "manifest.m3u8", "type": "HLS", "muxStreams": [ "media-sd", "media-hd" ] }, { "fileName": "manifest.mpd", "type": "DASH", "muxStreams": [ "video-only-sd", "video-only-hd", "audio-only" ] } ], "output": { "uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/" } }, "state": "PENDING", "createTime": CREATE_TIME, "ttlAfterCompletionDays": 30 }
gcloud
- Make the following replacements for the
gcloud
command: - JOB_ID: The ID of the job you created.
- LOCATION: The location of your
job. Use one of the supported regions:
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
asia-east1
asia-south1
asia-southeast1
europe-west1
europe-west2
europe-west4
- Run the following command:
gcloud transcoder jobs describe JOB_ID --location=LOCATION
You should see a response similar to the following:{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/jobs/JOB_ID", "config": { "inputs": [ { "key": "input0", "uri": "gs://STORAGE_BUCKET_NAME/STORAGE_INPUT_VIDEO" } ], "editList": [ { "key": "atom0", "inputs": [ "input0" ], "startTimeOffset": "0s" } ], "elementaryStreams": [ { "videoStream": { "h264": { "widthPixels": 640, "heightPixels": 360, "frameRate": 30, "bitrateBps": 550000, "pixelFormat": "yuv420p", "rateControlMode": "vbr", "crfLevel": 21, "gopDuration": "3s", "vbvSizeBits": 550000, "vbvFullnessBits": 495000, "entropyCoder": "cabac", "bFrameCount": 3, "aqStrength": 1, "profile": "high", "preset": "veryfast" } }, "key": "video-stream0" }, { "videoStream": { "h264": { "widthPixels": 1280, "heightPixels": 720, "frameRate": 30, "bitrateBps": 2500000, "pixelFormat": "yuv420p", "rateControlMode": "vbr", "crfLevel": 21, "gopDuration": "3s", "vbvSizeBits": 2500000, "vbvFullnessBits": 2250000, "entropyCoder": "cabac", "bFrameCount": 3, "aqStrength": 1, "profile": "high", "preset": "veryfast" } }, "key": "video-stream1" }, { "audioStream": { "codec": "aac", "bitrateBps": 64000, "channelCount": 2, "channelLayout": [ "fl", "fr" ], "sampleRateHertz": 48000 }, "key": "audio-stream0" } ], "muxStreams": [ { "key": "sd", "fileName": "sd.mp4", "container": "mp4", "elementaryStreams": [ "video-stream0", "audio-stream0" ] }, { "key": "hd", "fileName": "hd.mp4", "container": "mp4", "elementaryStreams": [ "video-stream1", "audio-stream0" ] }, { "key": "media-sd", "fileName": "media-sd.ts", "container": "ts", "elementaryStreams": [ "video-stream0", "audio-stream0" ] }, { "key": "media-hd", "fileName": "media-hd.ts", "container": "ts", "elementaryStreams": [ "video-stream1", "audio-stream0" ] }, { "key": "video-only-sd", "fileName": "video-only-sd.m4s", "container": "fmp4", "elementaryStreams": [ "video-stream0" ] }, { "key": "video-only-hd", "fileName": "video-only-hd.m4s", "container": "fmp4", "elementaryStreams": [ "video-stream1" ] }, { "key": "audio-only", "fileName": "audio-only.m4s", "container": "fmp4", "elementaryStreams": [ "audio-stream0" ] } ], "manifests": [ { "fileName": "manifest.m3u8", "type": "HLS", "muxStreams": [ "media-sd", "media-hd" ] }, { "fileName": "manifest.mpd", "type": "DASH", "muxStreams": [ "video-only-sd", "video-only-hd", "audio-only" ] } ], "output": { "uri": "gs://STORAGE_BUCKET_NAME/STORAGE_OUTPUT_FOLDER/" } }, "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.
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.
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.
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.
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.
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.
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.
If the state
is SUCCEEDED
, the job is complete and your video outputs are
now available in your Cloud Storage bucket's list of encoded files.
Play your video
To play the generated media file in Shaka Player, complete the following steps:
- Make the Cloud Storage bucket you created publicly readable.
- To enable cross-origin resource
sharing (CORS) on a Cloud Storage bucket, do the following:
- Create a JSON file that contains the following:
[ { "origin": ["https://shaka-player-demo.appspot.com/"], "responseHeader": ["Content-Type", "Range"], "method": ["GET", "HEAD"], "maxAgeSeconds": 3600 } ]
-
Run the following command after replacing JSON_FILE_NAME with
the name of the JSON file you created in the previous step:
gsutil cors set JSON_FILE_NAME.json gs://STORAGE_BUCKET_NAME
- Create a JSON file that contains the following:
- Pick one of the MP4 or manifest files generated by the transcoding job in the Cloud Storage bucket. Click Copy URL in the file's Public access column.
- Navigate to Shaka Player, an online live stream player.
- Click Custom Content in the top navigation bar.
- Click the + button.
Paste the public URL of the file into the Manifest URL box.
Type a name in the Name box.
Click Save.
Click Play.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
In the Google Cloud console, go to the Cloud Storage Browser page.
Select the checkbox next to the bucket that you created.
Click Delete.
In the pop-up window that appears, click Delete to permanently delete the bucket and its contents.
You should also delete the job after it completes.
REST & CMD LINE
Before using any of the request data, make the following replacements:
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
asia-east1
asia-south1
asia-southeast1
europe-west1
europe-west2
europe-west4
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{}
gcloud
- Make the following replacements for the
gcloud
command: - JOB_ID: The ID of the job you created.
- LOCATION: The location of your
job. Use one of the supported regions:
us-central1
us-west1
us-west2
us-east1
us-east4
southamerica-east1
asia-east1
asia-south1
asia-southeast1
europe-west1
europe-west2
europe-west4
- Run the following command:
gcloud transcoder jobs delete JOB_ID --location=LOCATION
You should see a response similar to the following:Deleted job [projects/PROJECT_ID/locations/LOCATION/jobs/JOB_ID].
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.
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.
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.
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.
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.
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.
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.
What's next
- Read the Transcoder API overview.
- Review the available guides to learn how to use the Transcoder API.