Created
August 29, 2022 14:06
-
-
Save nazir-kabani/a436f2e9ca3ed1b1276d0ced14ec88b1 to your computer and use it in GitHub Desktop.
this is main..py for cloud functions 2 from Automate your VOD transcoding at scale with GCP: Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import re | |
import argparse | |
import time | |
import json | |
from google.cloud import pubsub_v1 | |
from google.cloud.video.transcoder_v1.services.transcoder_service import ( | |
TranscoderServiceClient, | |
) | |
def get_job_state(project_id, location, job_id): | |
"""Gets a job's state. | |
Args: | |
project_id: The GCP project ID. | |
location: The location this job is in. | |
job_id: The job ID.""" | |
def hello_pubsub(event, context): | |
"""Triggered from a message on a Cloud Pub/Sub topic. | |
Args: | |
event (dict): Event payload. | |
context (google.cloud.functions.Context): Metadata for the event. | |
""" | |
pubsub_message = base64.b64decode(event['data']).decode('utf-8') | |
print(pubsub_message) | |
ljob_id=re.findall("/jobs/([a-z0-9].{35})", pubsub_message) | |
job_id=''.join(map(str, ljob_id)) | |
llocation=re.findall("/locations/([a-z0-9].+)/jobs", pubsub_message) | |
location=''.join(map(str, llocation)) | |
print(job_id) | |
print(location) | |
#transcoder api calls starting from here | |
client = TranscoderServiceClient() | |
name = f"projects/project-id/locations/{location}/jobs/{job_id}" # update your project-id in this line | |
response = client.get_job(name=name) | |
output_uri=str(response.config.output.uri) | |
print(output_uri) | |
path=re.findall("gs://{output-bucket-name}/(.+)",output_uri) # update your output bucket name in this line | |
spath=' '.join(map(str, path)) | |
hls_url="http://{cdn-host-name-or-ipv4-address}/"+spath+"manifest.m3u8" # update your media-cdn / cloud CDN hostname or IPv4 address in this line | |
dash_url="http://{cdn-host-name-or-ipv4-address}/"+spath+"manifest.mpd" # update your media-cdn / cloud CDN hostname or IPv4 address in this line | |
print(hls_url) | |
print(dash_url) | |
#pubsub publishing starting from here | |
try: | |
publisher = pubsub_v1.PublisherClient() | |
data=response | |
job_state=str(data.state) | |
print(job_state) | |
createTime=str(data.create_time) | |
startTime=str(data.start_time) | |
endTime=str(data.end_time) | |
duration=str(data.end_time-data.start_time) | |
print(duration) | |
topic_path = 'projects/project-id/topics/transcoding-complete-status' # update your project-id and transcoding-complete-status pub/sub topic (Pub/Sub 3 from blog) id in this line | |
message_json = json.dumps( | |
{ | |
'jobId': job_id, | |
'location': location, | |
'createTime': createTime, | |
'startTime': startTime, | |
'endTime': endTime, | |
'duration': duration, | |
'jobEndState': job_state, | |
'hlsUri': hls_url, | |
'dashUri': dash_url | |
} | |
) | |
message_bytes = message_json.encode('utf-8') | |
publish_future = publisher.publish(topic_path, data=message_bytes) | |
publish_future.result() # Verify the publish succeeded | |
print('Message published.') | |
except Exception as e: | |
print(e) | |
print (e, 500) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment