GPU-accelerated video transcoding with FFmpeg on Cloud Run jobs

This tutorial describes how to transcode low-priority offline videos using Cloud Run jobs.

Objectives

In this tutorial, you will do the following:

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.

New Google Cloud users might be eligible for afree trial.

Before you begin

  1. 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.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. Toinitialize the gcloud CLI, run the following command:

    gcloudinit
  5. Create or select a Google Cloud 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.create permission.Learn how to grant roles.
    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.
    • Create a Google Cloud project:

      gcloud projects createPROJECT_ID

      ReplacePROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set projectPROJECT_ID

      ReplacePROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Install the Google Cloud CLI.

  8. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  9. Toinitialize the gcloud CLI, run the following command:

    gcloudinit
  10. Create or select a Google Cloud 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.create permission.Learn how to grant roles.
    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.
    • Create a Google Cloud project:

      gcloud projects createPROJECT_ID

      ReplacePROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set projectPROJECT_ID

      ReplacePROJECT_ID with your Google Cloud project name.

  11. Verify that billing is enabled for your Google Cloud project.

  12. 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.enable permission.Learn how to grant roles.

    gcloudservicesenablerun.googleapis.com cloudbuild.googleapis.com artifactregistry.googleapis.com
  13. Set your region as an environment variable:
    exportREGION=REGION
  14. Create a service account:
    gcloudiamservice-accountscreatevideo-encoding
  15. RequestTotal 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:

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

  1. In the Google Cloud console, go to theIAM page.

    Go to IAM
  2. Select the project.
  3. ClickGrant access.
  4. In theNew principals field, enter your user identifier. This is typically the email address that is used to deploy the Cloud Run service.

  5. In theSelect a role list, select a role.
  6. To grant additional roles, clickAdd another role and add each additional role.
  7. 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:

  1. Clone the sample repository to your local machine:

    gitclonehttps://github.com/GoogleCloudPlatform/cloud-run-samples
  2. Change 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:

  1. Create a bucket to store videos before processing:

    gcloudstoragebucketscreategs://preprocessing-PROJECT_ID\--locationLOCATION

    Replace the following:

    • PROJECT_ID: your project ID.
    • LOCATION: the Cloud Storage location.
  2. 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.

  3. Create a bucket to store transcoded videos after processing:

    gcloudstoragebucketscreategs://transcoded-PROJECT_ID\--locationLOCATION

    Replace the following:

    • PROJECT_ID: your project ID.
    • LOCATION: the Cloud Storage location.
  4. 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:

  1. Navigate to the sample directory:

    cdcloud-run-samples/jobs-video-encoding

  1. Create an Artifact Registry if the default Cloud Run registry doesn't already exist:

    gcloudartifactsrepositoriescreatecloud-run-source-deploy\--repository-format=docker\--locationLOCATION

    ReplaceLOCATION with the name of the location of the registry.

  2. Build the container image:

    gcloudbuildssubmit\--tagLOCATION-docker.pkg.dev/PROJECT_ID/cloud-run-source-deploy/IMAGE_NAME\--machine-typeE2-HIGHCPU-32

    Replace 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.

  3. 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=/outputs

    Replace 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:

  1. Upload an example video to encode:

    gcloudstoragecpgs://cloud-samples-data/video/cat.mp4gs://preprocessing-PROJECT_ID
  2. Run the job:

    gcloudrunjobsexecutevideo-encoding-job\--regionREGION\--wait\--args="cat.mp4,encoded_cat.mp4,-vcodec,h264_nvenc,-cq,21,-movflags,+faststart"

    Theentrypoint.sh file requires an input file, output file, and any arguments to send to FFmpeg.

  3. Review the Cloud Run logs to make sure the video trancoded:

    gcloudrunjobslogsreadvideo-encoding-job--regionREGION
  4. Download 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:

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Locate the job you want to delete in the jobs list, and clickits checkbox to select it.

  3. ClickDelete. This terminates all the job executions in progress andall running container instances.

gcloud

To delete a job, run the following command:

gcloudrunjobsdeleteJOB_NAME

ReplaceJOB_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:

    Caution: Deleting a project has the following effects:
    • Everything in the project is deleted. If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
    • Custom project IDs are lost. When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as anappspot.com URL, delete selected resources inside the project instead of deleting the whole project.

    Delete a Google Cloud project:

    gcloud projects deletePROJECT_ID

What's next

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.