Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit e30431f

Browse files
authored
feat: add samples and tests for adding captions to a job (#131)
* feat: add samples and tests for adding captions to a job * remove space * remove extraneous explanation
1 parent 0ae071f commit e30431f

File tree

4 files changed

+495
-7
lines changed

4 files changed

+495
-7
lines changed

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ Next Steps
8181
APIs that we cover.
8282

8383
.. _Samples: https://github.com/googleapis/python-video-transcoder/blob/main/samples/snippets/README.md
84-
.. _Transcoder API Product documentation: https://cloud.google.com/transcoder/docs
84+
.. _Transcoder API Product documentation: https://cloud.google.com/transcoder/docs
8585
.. _README: https://github.com/googleapis/google-cloud-python/blob/main/README.rst
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Cloud Transcoder sample for creating a job that embeds captions in the output video.
18+
19+
Example usage:
20+
python create_job_with_embedded_captions.py --project_id <project-id> --location <location> \
21+
--input_video_uri <uri> --input_captions_uri <uri> --output_uri <uri>
22+
"""
23+
24+
# [START transcoder_create_job_with_embedded_captions]
25+
26+
import argparse
27+
28+
from google.cloud.video import transcoder_v1
29+
from google.cloud.video.transcoder_v1.services.transcoder_service import (
30+
TranscoderServiceClient,
31+
)
32+
33+
34+
def create_job_with_embedded_captions(
35+
project_id,
36+
location,
37+
input_video_uri,
38+
input_captions_uri,
39+
output_uri,
40+
):
41+
"""Creates a job based on an ad-hoc job configuration that embeds captions in the output video.
42+
43+
Args:
44+
project_id (str): The GCP project ID.
45+
location (str): The location to start the job in.
46+
input_video_uri (str): Uri of the input video in the Cloud Storage
47+
bucket.
48+
input_captions_uri (str): Uri of the input captions file in the Cloud
49+
Storage bucket.
50+
output_uri (str): Uri of the video output folder in the Cloud Storage
51+
bucket."""
52+
53+
client = TranscoderServiceClient()
54+
55+
parent = f"projects/{project_id}/locations/{location}"
56+
job = transcoder_v1.types.Job()
57+
job.output_uri = output_uri
58+
job.config = transcoder_v1.types.JobConfig(
59+
inputs=[
60+
transcoder_v1.types.Input(
61+
key="input0",
62+
uri=input_video_uri,
63+
),
64+
transcoder_v1.types.Input(
65+
key="caption-input0",
66+
uri=input_captions_uri,
67+
),
68+
],
69+
edit_list=[
70+
transcoder_v1.types.EditAtom(
71+
key="atom0",
72+
inputs=["input0", "caption-input0"],
73+
),
74+
],
75+
elementary_streams=[
76+
transcoder_v1.types.ElementaryStream(
77+
key="video-stream0",
78+
video_stream=transcoder_v1.types.VideoStream(
79+
h264=transcoder_v1.types.VideoStream.H264CodecSettings(
80+
height_pixels=360,
81+
width_pixels=640,
82+
bitrate_bps=550000,
83+
frame_rate=60,
84+
),
85+
),
86+
),
87+
transcoder_v1.types.ElementaryStream(
88+
key="audio-stream0",
89+
audio_stream=transcoder_v1.types.AudioStream(
90+
codec="aac", bitrate_bps=64000
91+
),
92+
),
93+
transcoder_v1.types.ElementaryStream(
94+
key="cea-stream0",
95+
# The following doesn't work because "mapping" is a reserved
96+
# argument name in GCP python client libraries (see
97+
# https://github.com/googleapis/proto-plus-python/blob/main/proto/message.py#L447):
98+
#
99+
# text_stream=transcoder_v1.types.TextStream(
100+
# codec="cea608",
101+
# mapping=[
102+
# transcoder_v1.types.TextStream.TextMapping(
103+
# atom_key="atom0",
104+
# input_key="caption-input0",
105+
# input_track=0,
106+
# ),
107+
# ],
108+
# ),
109+
# Use a python dictionary as a workaround:
110+
text_stream={
111+
"codec": "cea608",
112+
"mapping": [
113+
{
114+
"atom_key": "atom0",
115+
"input_key": "caption-input0",
116+
"input_track": 0,
117+
}
118+
],
119+
},
120+
),
121+
],
122+
mux_streams=[
123+
transcoder_v1.types.MuxStream(
124+
key="sd",
125+
container="mp4",
126+
elementary_streams=["video-stream0", "audio-stream0"],
127+
),
128+
transcoder_v1.types.MuxStream(
129+
key="sd-hls",
130+
container="ts",
131+
elementary_streams=["video-stream0", "audio-stream0"],
132+
),
133+
transcoder_v1.types.MuxStream(
134+
key="sd-dash",
135+
container="fmp4",
136+
elementary_streams=["video-stream0"],
137+
),
138+
transcoder_v1.types.MuxStream(
139+
key="audio-dash",
140+
container="fmp4",
141+
elementary_streams=["audio-stream0"],
142+
),
143+
],
144+
manifests=[
145+
transcoder_v1.types.Manifest(
146+
file_name="manifest.m3u8",
147+
type_="HLS",
148+
mux_streams=["sd-hls"],
149+
),
150+
transcoder_v1.types.Manifest(
151+
file_name="manifest.mpd",
152+
type_="DASH",
153+
mux_streams=["sd-dash", "audio-dash"],
154+
),
155+
],
156+
)
157+
response = client.create_job(parent=parent, job=job)
158+
print(f"Job: {response.name}")
159+
return response
160+
161+
162+
# [END transcoder_create_job_with_embedded_captions]
163+
164+
if __name__ == "__main__":
165+
parser = argparse.ArgumentParser()
166+
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
167+
parser.add_argument(
168+
"--location",
169+
help="The location to start this job in.",
170+
default="us-central1",
171+
)
172+
parser.add_argument(
173+
"--input_video_uri",
174+
help="Uri of the input video in the Cloud Storage bucket.",
175+
required=True,
176+
)
177+
parser.add_argument(
178+
"--input_captions_uri",
179+
help="Uri of the input captions file in the Cloud Storage bucket.",
180+
required=True,
181+
)
182+
parser.add_argument(
183+
"--output_uri",
184+
help="Uri of the video output folder in the Cloud Storage bucket. "
185+
+ "Must end in '/'.",
186+
required=True,
187+
)
188+
args = parser.parse_args()
189+
create_job_with_embedded_captions(
190+
args.project_id,
191+
args.location,
192+
args.input_video_uri,
193+
args.input_captions_uri,
194+
args.output_uri,
195+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Cloud Transcoder sample for creating a job that can use captions from a standalone file.
18+
19+
Example usage:
20+
python create_job_with_standalone_captions.py --project_id <project-id> --location <location> \
21+
--input_video_uri <uri> --input_captions_uri <uri> --output_uri <uri>
22+
"""
23+
24+
# [START transcoder_create_job_with_standalone_captions]
25+
26+
import argparse
27+
28+
from google.cloud.video import transcoder_v1
29+
from google.cloud.video.transcoder_v1.services.transcoder_service import (
30+
TranscoderServiceClient,
31+
)
32+
from google.protobuf import duration_pb2 as duration
33+
34+
35+
def create_job_with_standalone_captions(
36+
project_id,
37+
location,
38+
input_video_uri,
39+
input_captions_uri,
40+
output_uri,
41+
):
42+
"""Creates a job based on an ad-hoc job configuration that can use captions from a standalone file.
43+
44+
Args:
45+
project_id (str): The GCP project ID.
46+
location (str): The location to start the job in.
47+
input_video_uri (str): Uri of the input video in the Cloud Storage
48+
bucket.
49+
input_captions_uri (str): Uri of the input captions file in the Cloud
50+
Storage bucket.
51+
output_uri (str): Uri of the video output folder in the Cloud Storage
52+
bucket."""
53+
54+
client = TranscoderServiceClient()
55+
56+
parent = f"projects/{project_id}/locations/{location}"
57+
job = transcoder_v1.types.Job()
58+
job.output_uri = output_uri
59+
job.config = transcoder_v1.types.JobConfig(
60+
inputs=[
61+
transcoder_v1.types.Input(
62+
key="input0",
63+
uri=input_video_uri,
64+
),
65+
transcoder_v1.types.Input(
66+
key="caption-input0",
67+
uri=input_captions_uri,
68+
),
69+
],
70+
edit_list=[
71+
transcoder_v1.types.EditAtom(
72+
key="atom0",
73+
inputs=["input0", "caption-input0"],
74+
),
75+
],
76+
elementary_streams=[
77+
transcoder_v1.types.ElementaryStream(
78+
key="video-stream0",
79+
video_stream=transcoder_v1.types.VideoStream(
80+
h264=transcoder_v1.types.VideoStream.H264CodecSettings(
81+
height_pixels=360,
82+
width_pixels=640,
83+
bitrate_bps=550000,
84+
frame_rate=60,
85+
),
86+
),
87+
),
88+
transcoder_v1.types.ElementaryStream(
89+
key="audio-stream0",
90+
audio_stream=transcoder_v1.types.AudioStream(
91+
codec="aac", bitrate_bps=64000
92+
),
93+
),
94+
transcoder_v1.types.ElementaryStream(
95+
key="vtt-stream0",
96+
# The following doesn't work because "mapping" is a reserved
97+
# argument name in GCP python client libraries (see
98+
# https://github.com/googleapis/proto-plus-python/blob/main/proto/message.py#L447):
99+
#
100+
# text_stream=transcoder_v1.types.TextStream(
101+
# codec="webvtt",
102+
# mapping=[
103+
# transcoder_v1.types.TextStream.TextMapping(
104+
# atom_key="atom0",
105+
# input_key="caption-input0",
106+
# input_track=0,
107+
# ),
108+
# ],
109+
# ),
110+
# Use a python dictionary as a workaround:
111+
text_stream={
112+
"codec": "webvtt",
113+
"mapping": [
114+
{
115+
"atom_key": "atom0",
116+
"input_key": "caption-input0",
117+
"input_track": 0,
118+
}
119+
],
120+
},
121+
),
122+
],
123+
mux_streams=[
124+
transcoder_v1.types.MuxStream(
125+
key="sd-hls-fmp4",
126+
container="fmp4",
127+
elementary_streams=["video-stream0"],
128+
),
129+
transcoder_v1.types.MuxStream(
130+
key="audio-hls-fmp4",
131+
container="fmp4",
132+
elementary_streams=["audio-stream0"],
133+
),
134+
transcoder_v1.types.MuxStream(
135+
key="text-vtt",
136+
container="vtt",
137+
elementary_streams=["vtt-stream0"],
138+
segment_settings=transcoder_v1.types.SegmentSettings(
139+
segment_duration=duration.Duration(
140+
seconds=6,
141+
),
142+
individual_segments=True,
143+
),
144+
),
145+
],
146+
manifests=[
147+
transcoder_v1.types.Manifest(
148+
file_name="manifest.m3u8",
149+
type_="HLS",
150+
mux_streams=["sd-hls-fmp4", "audio-hls-fmp4", "text-vtt"],
151+
),
152+
],
153+
)
154+
response = client.create_job(parent=parent, job=job)
155+
print(f"Job: {response.name}")
156+
return response
157+
158+
159+
# [END transcoder_create_job_with_standalone_captions]
160+
161+
if __name__ == "__main__":
162+
parser = argparse.ArgumentParser()
163+
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
164+
parser.add_argument(
165+
"--location",
166+
help="The location to start this job in.",
167+
default="us-central1",
168+
)
169+
parser.add_argument(
170+
"--input_video_uri",
171+
help="Uri of the input video in the Cloud Storage bucket.",
172+
required=True,
173+
)
174+
parser.add_argument(
175+
"--input_captions_uri",
176+
help="Uri of the input captions file in the Cloud Storage bucket.",
177+
required=True,
178+
)
179+
parser.add_argument(
180+
"--output_uri",
181+
help="Uri of the video output folder in the Cloud Storage bucket. "
182+
+ "Must end in '/'.",
183+
required=True,
184+
)
185+
args = parser.parse_args()
186+
create_job_with_standalone_captions(
187+
args.project_id,
188+
args.location,
189+
args.input_video_uri,
190+
args.input_captions_uri,
191+
args.output_uri,
192+
)

0 commit comments

Comments
 (0)