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_v1beta1.types.Job() job.input_uri = input_uri job.output_uri = output_uri job.config = transcoder_v1beta1.types.JobConfig( elementary_streams=[ transcoder_v1beta1.types.ElementaryStream( key="video-stream0", video_stream=transcoder_v1beta1.types.VideoStream( codec="h264", height_pixels=360, width_pixels=640, bitrate_bps=550000, frame_rate=60, ), ), transcoder_v1beta1.types.ElementaryStream( key="video-stream1", video_stream=transcoder_v1beta1.types.VideoStream( codec="h264", height_pixels=720, width_pixels=1280, bitrate_bps=2500000, frame_rate=60, ), ), transcoder_v1beta1.types.ElementaryStream( key="audio-stream0", audio_stream=transcoder_v1beta1.types.AudioStream( codec="aac", bitrate_bps=64000 ), ), ], mux_streams=[ transcoder_v1beta1.types.MuxStream( key="sd", container="mp4", elementary_streams=["video-stream0", "audio-stream0"], ), transcoder_v1beta1.types.MuxStream( key="hd", container="mp4", elementary_streams=["video-stream1", "audio-stream0"], ), ], ) response = client.create_job(parent=parent, job=job) print(f"Job: {response.name}") return response
def get_job_template(project_id, location, template_id): """Gets a job template. Args: project_id: The GCP project ID. location: The location of the template. template_id: The user-defined template ID.""" client = TranscoderServiceClient() name = f"projects/{project_id}/locations/{location}/jobTemplates/{template_id}" response = client.get_job_template(name=name) print(f"Job template: {response.name}") return response
def delete_job(project_id, location, job_id): """Gets a job. Args: project_id: The GCP project ID. location: The location this job is in. job_id: The job ID.""" client = TranscoderServiceClient() name = f"projects/{project_id}/locations/{location}/jobs/{job_id}" response = client.delete_job(name=name) print("Deleted job") return response
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.""" client = TranscoderServiceClient() name = f"projects/{project_id}/locations/{location}/jobs/{job_id}" response = client.get_job(name=name) print(f"Job state: {str(response.state)}") return response
def list_jobs(project_id, location): """Lists all jobs in a location. Args: project_id: The GCP project ID. location: The location of the jobs.""" client = TranscoderServiceClient() parent = f"projects/{project_id}/locations/{location}" response = client.list_jobs(parent=parent) print("Jobs:") for job in response.jobs: print({job.name}) return response
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.""" client = TranscoderServiceClient() parent = f"projects/{project_id}/locations/{location}" job = transcoder_v1beta1.types.Job() job.input_uri = input_uri job.output_uri = output_uri job.template_id = preset response = client.create_job(parent=parent, job=job) print(f"Job: {response.name}") return response
def create_job_with_static_overlay( project_id, location, input_uri, overlay_image_uri, output_uri ): """Creates a job based on an ad-hoc job configuration that includes a static image overlay. 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. overlay_image_uri: Uri of the JPEG image for the overlay in the Cloud Storage bucket. Must be a JPEG. output_uri: Uri of the video output folder in the Cloud Storage bucket.""" client = TranscoderServiceClient() parent = f"projects/{project_id}/locations/{location}" job = transcoder_v1beta1.types.Job() job.input_uri = input_uri job.output_uri = output_uri job.config = transcoder_v1beta1.types.JobConfig( elementary_streams=[ transcoder_v1beta1.types.ElementaryStream( key="video-stream0", video_stream=transcoder_v1beta1.types.VideoStream( codec="h264", height_pixels=360, width_pixels=640, bitrate_bps=550000, frame_rate=60, ), ), transcoder_v1beta1.types.ElementaryStream( key="audio-stream0", audio_stream=transcoder_v1beta1.types.AudioStream( codec="aac", bitrate_bps=64000 ), ), ], mux_streams=[ transcoder_v1beta1.types.MuxStream( key="sd", container="mp4", elementary_streams=["video-stream0", "audio-stream0"], ), ], overlays=[ transcoder_v1beta1.types.Overlay( image=transcoder_v1beta1.types.Overlay.Image( uri=overlay_image_uri, resolution=transcoder_v1beta1.types.Overlay.NormalizedCoordinate( x=1, y=0.5, ), alpha=1, ), animations=[ transcoder_v1beta1.types.Overlay.Animation( animation_static=transcoder_v1beta1.types.Overlay.AnimationStatic( xy=transcoder_v1beta1.types.Overlay.NormalizedCoordinate( x=0, y=0, ), start_time_offset=duration.Duration( seconds=0, ), ), ), transcoder_v1beta1.types.Overlay.Animation( animation_end=transcoder_v1beta1.types.Overlay.AnimationEnd( start_time_offset=duration.Duration( seconds=10, ), ), ), ], ), ], ) response = client.create_job(parent=parent, job=job) print(f"Job: {response.name}") return response
def create_job_template(project_id, location, template_id): """Creates a job template. Args: project_id: The GCP project ID. location: The location to store this template in. template_id: The user-defined template ID.""" client = TranscoderServiceClient() parent = f"projects/{project_id}/locations/{location}" job_template = transcoder_v1beta1.types.JobTemplate() job_template.name = ( f"projects/{project_id}/locations/{location}/jobTemplates/{template_id}" ) job_template.config = transcoder_v1beta1.types.JobConfig( elementary_streams=[ transcoder_v1beta1.types.ElementaryStream( key="video-stream0", video_stream=transcoder_v1beta1.types.VideoStream( codec="h264", height_pixels=360, width_pixels=640, bitrate_bps=550000, frame_rate=60, ), ), transcoder_v1beta1.types.ElementaryStream( key="video-stream1", video_stream=transcoder_v1beta1.types.VideoStream( codec="h264", height_pixels=720, width_pixels=1280, bitrate_bps=2500000, frame_rate=60, ), ), transcoder_v1beta1.types.ElementaryStream( key="audio-stream0", audio_stream=transcoder_v1beta1.types.AudioStream( codec="aac", bitrate_bps=64000), ), ], mux_streams=[ transcoder_v1beta1.types.MuxStream( key="sd", container="mp4", elementary_streams=["video-stream0", "audio-stream0"], ), transcoder_v1beta1.types.MuxStream( key="hd", container="mp4", elementary_streams=["video-stream1", "audio-stream0"], ), ], ) response = client.create_job_template(parent=parent, job_template=job_template, job_template_id=template_id) print(f"Job template: {response.name}") return response
def create_job_with_periodic_images_spritesheet(project_id, location, input_uri, output_uri): """Creates a job based on an ad-hoc job configuration that generates two spritesheets. 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_v1beta1.types.Job() job.input_uri = input_uri job.output_uri = output_uri job.config = transcoder_v1beta1.types.JobConfig( # Create an ad-hoc job. For more information, see https://cloud.google.com/transcoder/docs/how-to/jobs#create_jobs_ad_hoc. # See all options for the job config at https://cloud.google.com/transcoder/docs/reference/rest/v1beta1/JobConfig. elementary_streams=[ # This section defines the output video stream. transcoder_v1beta1.types.ElementaryStream( key="video-stream0", video_stream=transcoder_v1beta1.types.VideoStream( codec="h264", height_pixels=360, width_pixels=640, bitrate_bps=550000, frame_rate=60, ), ), # This section defines the output audio stream. transcoder_v1beta1.types.ElementaryStream( key="audio-stream0", audio_stream=transcoder_v1beta1.types.AudioStream( codec="aac", bitrate_bps=64000), ), ], # This section multiplexes the output audio and video together into a container. mux_streams=[ transcoder_v1beta1.types.MuxStream( key="sd", container="mp4", elementary_streams=["video-stream0", "audio-stream0"], ), ], # Generate two sprite sheets from the input video into the GCS bucket. For more information, see # https://cloud.google.com/transcoder/docs/how-to/generate-spritesheet#generate_image_periodically. sprite_sheets=[ # Generate a sprite sheet with 64x32px images. An image is taken every 7 seconds from the video. transcoder_v1beta1.types.SpriteSheet( file_prefix="small-sprite-sheet", sprite_width_pixels=64, sprite_height_pixels=32, interval=duration.Duration(seconds=7, ), ), # Generate a sprite sheet with 128x72px images. An image is taken every 7 seconds from the video. transcoder_v1beta1.types.SpriteSheet( file_prefix="large-sprite-sheet", sprite_width_pixels=128, sprite_height_pixels=72, interval=duration.Duration(seconds=7, ), ), ], ) response = client.create_job(parent=parent, job=job) print(f"Job: {response.name}") return response
def create_job_with_periodic_images_spritesheet(project_id, location, input_uri, output_uri): """Creates a job based on an ad-hoc job configuration that generates two spritesheets. 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_v1beta1.types.Job() job.input_uri = input_uri job.output_uri = output_uri job.config = transcoder_v1beta1.types.JobConfig( # Create an ad-hoc job. For more information, see https://cloud.google.com/transcoder/docs/how-to/jobs#create_jobs_ad_hoc. # See all options for the job config at https://cloud.google.com/transcoder/docs/reference/rest/v1beta1/JobConfig. inputs=[ transcoder_v1beta1.types.Input( key="caption_input0", uri="gs://alme-test-eyu/transcode_input/subs_2.srt", ), transcoder_v1beta1.types.Input(key="input0", ) ], edit_list=[ transcoder_v1beta1.types.EditAtom( key="atom0", inputs=[ "caption_input0", "input0", ], ), ], elementary_streams=[ # This section defines the output video stream. transcoder_v1beta1.types.ElementaryStream( key="video-stream0", video_stream=transcoder_v1beta1.types.VideoStream( codec="h264", height_pixels=360, width_pixels=640, bitrate_bps=550000, frame_rate=60, ), ), # This section defines the output audio stream. transcoder_v1beta1.types.ElementaryStream( key="audio-stream0", audio_stream=transcoder_v1beta1.types.AudioStream( codec="aac", bitrate_bps=64000), ), # This section defines the output text stream. transcoder_v1beta1.types.ElementaryStream( key="text-stream0", text_stream=transcoder_v1beta1.types.TextStream( codec="webvtt", #codec="cea608", language_code="en-US", #mapping=[ # transcoder_v1beta1.types.TextStream.TextAtom( # key="atom0", # inputs=[ # transcoder_v1beta1.types.TextStream.TextAtom.TextInput( # key="caption_input0", # ), # ], # ), #] ), ), ], # This section multiplexes the output audio and video together into a container. #mux_streams=[ # transcoder_v1beta1.types.MuxStream( # key="sd", # container="mp4", # elementary_streams=["video-stream0", "audio-stream0", "text-stream0"], # ), #], mux_streams=[ transcoder_v1beta1.types.MuxStream( key="sd", container="ts", elementary_streams=["video-stream0", "audio-stream0"], #segment_settings=transcoder_v1beta1.types.SegmentSettings( # segment_duration=duration.Duration(seconds=6), # individual_segments=True #), ), #transcoder_v1beta1.types.MuxStream( # key="audio", # container="fmp4", # elementary_streams=["audio-stream0"], # segment_settings=transcoder_v1beta1.types.SegmentSettings( # segment_duration=duration.Duration(seconds=5), # individual_segments=True # ), #), transcoder_v1beta1.types.MuxStream( key="subtitle", container="vtt", elementary_streams=["text-stream0"], segment_settings=transcoder_v1beta1.types.SegmentSettings( segment_duration=duration.Duration(seconds=6), individual_segments=True), ), ], manifests=[ transcoder_v1beta1.types.Manifest( file_name="master.m3u8", type_="HLS", mux_streams=["sd", "subtitle"], ) ], # Generate two sprite sheets from the input video into the GCS bucket. For more information, see # https://cloud.google.com/transcoder/docs/how-to/generate-spritesheet#generate_image_periodically. sprite_sheets=[ # Generate a sprite sheet with 64x32px images. An image is taken every 7 seconds from the video. transcoder_v1beta1.types.SpriteSheet( file_prefix="4-3-", sprite_width_pixels=720, sprite_height_pixels=540, column_count=1, row_count=1, total_count=5, #interval=duration.Duration( # seconds=7, #), ), # Generate a sprite sheet with 128x72px images. An image is taken every 7 seconds from the video. transcoder_v1beta1.types.SpriteSheet( file_prefix="16-9-", sprite_width_pixels=960, sprite_height_pixels=540, column_count=1, row_count=1, total_count=5, #interval=duration.Duration( # seconds=7, #), ), ], ) response = client.create_job(parent=parent, job=job) print(f"Job: {response.name}") return response
def create_job_template(project_id, location, template_id, pubsub_topic): """Creates a job template. Args: project_id: The GCP project ID. location: The location to store this template in. Ex: us-central1 template_id: The user-defined template ID. Ex: template_transcode_to_mp4_with_notification pubsub_topic: The name of the Pub/Sub topic to publish job completion notification to. Example: my_pubsub_topic """ client = TranscoderServiceClient() parent = f"projects/{project_id}/locations/{location}" pubsub_topic_fullname = f"projects/{project_id}/topics/{pubsub_topic}" job_template = transcoder_v1beta1.types.JobTemplate() job_template.name = ( f"projects/{project_id}/locations/{location}/jobTemplates/{template_id}" ) job_template.config = transcoder_v1beta1.types.JobConfig( pubsub_destination={"topic": pubsub_topic_fullname}, elementary_streams=[ transcoder_v1beta1.types.ElementaryStream( key="video-stream0", video_stream=transcoder_v1beta1.types.VideoStream( codec="h264", height_pixels=360, width_pixels=640, bitrate_bps=550000, frame_rate=60, ), ), transcoder_v1beta1.types.ElementaryStream( key="video-stream1", video_stream=transcoder_v1beta1.types.VideoStream( codec="h264", height_pixels=720, width_pixels=1280, bitrate_bps=2500000, frame_rate=60, ), ), transcoder_v1beta1.types.ElementaryStream( key="audio-stream0", audio_stream=transcoder_v1beta1.types.AudioStream( codec="aac", bitrate_bps=64000), ), ], mux_streams=[ transcoder_v1beta1.types.MuxStream( key="sd", container="mp4", elementary_streams=["video-stream0", "audio-stream0"], ), transcoder_v1beta1.types.MuxStream( key="hd", container="mp4", elementary_streams=["video-stream1", "audio-stream0"], ), ], ) response = client.create_job_template(parent=parent, job_template=job_template, job_template_id=template_id) print(f"Job template: {response.name}") return response