How to add PubSub Destination while creating a job using preset "preset/web-hd" using google cloud transcoder-api?

271 Views Asked by At

I am converting a video, uploaded to cloud storage using a signed URL, using Transcoder API. I have written a cloud function that is triggered with write operations on the bucket. Everything is working fine but I need to get a notification when the conversion is completed. I am creating the job to convert the vid using the following code. I am trying to follow the solution proposed in this answer Google Cloud Platform: Convert uploaded MP4 file to HLS file

    def create_job_from_preset(project_id, location, input_uri, output_uri, preset):
    """Creates a job based on a job preset.

    Args:
        project_id: The GCP project ID.
        location: The location to start the job in.
        input_uri: Uri of the video in the Cloud Storage bucket.
        output_uri: Uri of the video output folder in the Cloud Storage bucket.
        preset: The preset template (for example, 'preset/web-hd')."""

    client = TranscoderServiceClient()

    parent = f"projects/{project_id}/locations/{location}"
    job = transcoder_v1.types.Job()
    job.input_uri = input_uri
    job.output_uri = output_uri
    job.template_id = preset
    job.ttl_after_completion_days = 1
    job.config = transcoder_v1.types.JobConfig(
        PubsubDestination={
            topic_name=f"projects/{project_id}/topics/testing"
        }
    )
    response = client.create_job(parent=parent, job=job)
    print(f"Job: {response.name}")
    return response

The following snippet in the above code is not working

    job.config = transcoder_v1.types.JobConfig(
        PubsubDestination={
            topic_name=f"projects/{project_id}/topics/testing"
        }
    )

I have viewed the following but couldn't find any solution.

https://cloud.google.com/transcoder/docs/how-to/create-pub-sub

How to configure pubsub_destination in Transcoder API of GCP

1

There are 1 best solutions below

3
Scott B On BEST ANSWER

You cannot not define any configuration on your JobConfig on your code if you are creating a job from a preset or template since the preset and template will already populate the JobConfig for you.

As an alternative, you may create job using an ad-hoc configuration and then define PubsubDestination as shown on below code:

Note that I also corrected the syntax in using the PubsubDestination

from google.cloud.video import transcoder_v1
from google.cloud.video.transcoder_v1.services.transcoder_service import (
    TranscoderServiceClient,
)


def create_job_from_ad_hoc(project_id, location, input_uri, output_uri):
    """Creates a job based on an ad-hoc job configuration.

    Args:
        project_id: The GCP project ID.
        location: The location to start the job in.
        input_uri: Uri of the video in the Cloud Storage bucket.
        output_uri: Uri of the video output folder in the Cloud Storage bucket."""

    client = TranscoderServiceClient()

    parent = f"projects/{project_id}/locations/{location}"
    job = transcoder_v1.types.Job()
    job.input_uri = input_uri
    job.output_uri = output_uri
    job.config = transcoder_v1.types.JobConfig(
        elementary_streams=[
            transcoder_v1.types.ElementaryStream(
                key="video-stream0",
                video_stream=transcoder_v1.types.VideoStream(
                    h264=transcoder_v1.types.VideoStream.H264CodecSettings(
                        height_pixels=360,
                        width_pixels=640,
                        bitrate_bps=550000,
                        frame_rate=60,
                    ),
                ),
            ),
            transcoder_v1.types.ElementaryStream(
                key="video-stream1",
                video_stream=transcoder_v1.types.VideoStream(
                    h264=transcoder_v1.types.VideoStream.H264CodecSettings(
                        height_pixels=720,
                        width_pixels=1280,
                        bitrate_bps=2500000,
                        frame_rate=60,
                    ),
                ),
            ),
            transcoder_v1.types.ElementaryStream(
                key="audio-stream0",
                audio_stream=transcoder_v1.types.AudioStream(
                    codec="aac", bitrate_bps=64000
                ),
            ),
        ],
        mux_streams=[
            transcoder_v1.types.MuxStream(
                key="sd",
                container="mp4",
                elementary_streams=["video-stream0", "audio-stream0"],
            ),
            transcoder_v1.types.MuxStream(
                key="hd",
                container="mp4",
                elementary_streams=["video-stream1", "audio-stream0"],
            ),
        ],
        pubsub_destination=
            transcoder_v1.types.PubsubDestination(
                topic=f"projects/{project_id}/topics/your-topic"
            ),
    )
    response = client.create_job(parent=parent, job=job)
    print(f"Job: {response.name}")
    return response

Output of my testing: enter image description here

Other alternative is to create your own job template and then use it in your template_id so that you don't have to always define PubsubDestination in your code.