GPU-accelerated video transcoding with FFmpeg on Cloud Run jobs Stay organized with collections Save and categorize content based on your preferences.
This tutorial describes how to transcode low-priority offline videos using Cloud Run jobs.
Objectives
In this tutorial, you will do the following:
- Create Cloud Storage buckets to store the videos for processing and to store the encoding results.
- Deploy a Cloud Run job using GPUs to accelerate video transcoding.
- Run the job and make sure the video transcoded successfully.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage, use thepricing calculator.
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.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
Create or select a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
Create a Google Cloud project:
gcloud projects createPROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating.Select the Google Cloud project that you created:
gcloud config set projectPROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
Verify that billing is enabled for your Google Cloud project.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
Create or select a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
Create a Google Cloud project:
gcloud projects createPROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating.Select the Google Cloud project that you created:
gcloud config set projectPROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Run, Artifact Registry, and Cloud Build APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission.Learn how to grant roles.gcloudservicesenablerun.googleapis.com
cloudbuild.googleapis.com artifactregistry.googleapis.com - Set your region as an environment variable:
exportREGION=REGION
- Create a service account:
gcloudiamservice-accountscreatevideo-encoding
- Request
Total Nvidia L4 GPU allocation without zonal redundancy, per project per region, under Cloud Run Admin API in theQuotas and system limits page to complete this tutorial. Alternatively, you can deploy a Cloud Run service to automatically receive a grant of 3 nvidia-l4 GPU quota (zonal redundancy off) for a region.
Required roles
To get the permissions that you need to complete the tutorial, ask your administrator to grant you the following IAM roles on your project:
- Artifact Registry Repository Administrator (
roles/artifactregistry.repoAdmin) - Cloud Build Editor (
roles/cloudbuild.builds.editor) - Cloud Run Admin (
roles/run.admin) - Create Service Accounts (
roles/iam.serviceAccountCreator) - Service Account User (
roles/iam.serviceAccountUser)
For more information about granting roles, seeManage access to projects, folders, and organizations.
You might also be able to get the required permissions throughcustom roles or otherpredefined roles.
Note:IAM basic roles might also contain permissions to complete the tutorial. You shouldn't grant basic roles in a production environment, but you can grant them in a development or test environment.Grant the roles
Console
In the Google Cloud console, go to theIAM page.
Go to IAM- Select the project.
- ClickGrant access.
In theNew principals field, enter your user identifier. This is typically the email address that is used to deploy the Cloud Run service.
- In theSelect a role list, select a role.
- To grant additional roles, clickAdd another role and add each additional role.
- ClickSave.
gcloud
To grant the required IAM roles to your account on your project:
gcloudprojectsadd-iam-policy-bindingPROJECT_ID\--member=PRINCIPAL\--role=ROLE
Replace:
- PROJECT_NUMBER with your Google Cloud project number.
- PROJECT_ID with your Google Cloud project ID.
- PRINCIPAL with the account you are adding the binding for. This is typically the email address that is used to deploy the Cloud Run service.
- ROLE with the role you are adding to the deployer account.
Prepare your application
To retrieve the code sample for use:
Clone the sample repository to your local machine:
gitclonehttps://github.com/GoogleCloudPlatform/cloud-run-samplesChange to the directory that contains the Cloud Run samplecode:
cdcloud-run-samples/jobs-video-encoding
Create Cloud Storage buckets
To store the videos for processing, and to save the results of encoding, create the following two Cloud Storage buckets:
Create a bucket to store videos before processing:
gcloudstoragebucketscreategs://preprocessing-PROJECT_ID\--locationLOCATIONReplace the following:
- PROJECT_ID: your project ID.
- LOCATION: the Cloud Storage location.
Grant the service account access to read from this bucket:
gcloudstoragebucketsadd-iam-policy-bindinggs://preprocessing-PROJECT_ID\--member="serviceAccount:video-encoding@PROJECT_ID.iam.gserviceaccount.com"\--role="roles/storage.objectViewer"ReplacePROJECT_ID with your project ID.
Create a bucket to store transcoded videos after processing:
gcloudstoragebucketscreategs://transcoded-PROJECT_ID\--locationLOCATIONReplace the following:
- PROJECT_ID: your project ID.
- LOCATION: the Cloud Storage location.
Grant the service account access to read from and write to this bucket:
gcloudstoragebucketsadd-iam-policy-bindinggs://transcoded-PROJECT_ID\--member="serviceAccount:video-encoding@PROJECT_ID.iam.gserviceaccount.com"\--role="roles/storage.objectAdmin"ReplacePROJECT_ID with your project ID.
Deploy a Cloud Run job
Create a Cloud Run job by using the Dockerfile in the sample repository andmounting the buckets that you created:
Navigate to the sample directory:
cdcloud-run-samples/jobs-video-encoding
Create an Artifact Registry if the default Cloud Run registry doesn't already exist:
gcloudartifactsrepositoriescreatecloud-run-source-deploy\--repository-format=docker\--locationLOCATIONReplaceLOCATION with the name of the location of the registry.
Build the container image:
gcloudbuildssubmit\--tagLOCATION-docker.pkg.dev/PROJECT_ID/cloud-run-source-deploy/IMAGE_NAME\--machine-typeE2-HIGHCPU-32Replace the following:
- PROJECT_ID: your project ID.
- LOCATION:name of the location of the registry.
- IMAGE_NAME: name for the container image, for example:
ffmpeg-image.
Cloud Run uses a larger machine type to reduce build time.
Deploy the job:
gcloudrunjobscreatevideo-encoding-job\--imageLOCATION-docker.pkg.dev/PROJECT_ID/cloud-run-source-deploy/IMAGE_NAME\--regionREGION\--memory32Gi\--cpu8\--gpu1\--gpu-typenvidia-l4\--no-gpu-zonal-redundancy\--max-retries1\--service-accountvideo-encoding@PROJECT_ID.iam.gserviceaccount.com\--add-volume=name=input-volume,type=cloud-storage,bucket=preprocessing-PROJECT_ID,readonly=true\--add-volume-mount=volume=input-volume,mount-path=/inputs\--add-volume=name=output-volume,type=cloud-storage,bucket=transcoded-PROJECT_ID\--add-volume-mount=volume=output-volume,mount-path=/outputsReplace the following:
- PROJECT_ID: your project ID.
- REGION: the name of the region.Note: This must be the same region that you have GPU quota for.
- IMAGE_NAME: name for the container image, for example,
ffmpeg-image.
If this is the first time you deployed from source in this project, Cloud Run prompts you to create a default Artifact Registry repository.
Run the job
To run the job, follow these steps:
Upload an example video to encode:
gcloudstoragecpgs://cloud-samples-data/video/cat.mp4gs://preprocessing-PROJECT_IDRun the job:
gcloudrunjobsexecutevideo-encoding-job\--regionREGION\--wait\--args="cat.mp4,encoded_cat.mp4,-vcodec,h264_nvenc,-cq,21,-movflags,+faststart"The
entrypoint.shfile requires an input file, output file, and any arguments to send to FFmpeg.Review the Cloud Run logs to make sure the video trancoded:
gcloudrunjobslogsreadvideo-encoding-job--regionREGIONDownload the transcoded video:
gcloudstoragecpgs://transcoded-PROJECT_ID/encoded_cat.mp4.
Clean up
To avoid additional charges to your Google Cloud account, delete all the resourcesyou deployed with this quickstart.
Delete your repository
Cloud Run only charges for the time your job executes.However, you might still becharged for storing the container image inArtifact Registry. To delete Artifact Registry repositories,follow the steps inDeleterepositories in the Artifact Registrydocumentation.
Delete your job
Cloud Run jobs only incur cost when a job task is executing.To delete your Cloud Run job, follow one of these steps:
Console
To delete a job:
In the Google Cloud console, go to Cloud Run:
Locate the job you want to delete in the jobs list, and clickits checkbox to select it.
ClickDelete. This terminates all the job executions in progress andall running container instances.
gcloud
To delete a job, run the following command:
gcloudrunjobsdeleteJOB_NAMEReplaceJOB_NAME with the name of the job.
Delete your test project
Deleting your Google Cloud project stops billing for all resources in thatproject. To release all Google Cloud resources in your project, follow these steps:
What's next
- Explore otherCloud Run demos, tutorials, and samples.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.