|
| 1 | +/** |
| 2 | + * Copyright 2021, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +function main( |
| 19 | + projectId, |
| 20 | + location, |
| 21 | + inputUri1, |
| 22 | + startTimeOffset1, |
| 23 | + endTimeOffset1, |
| 24 | + inputUri2, |
| 25 | + startTimeOffset2, |
| 26 | + endTimeOffset2, |
| 27 | + outputUri |
| 28 | +) { |
| 29 | + // [START transcoder_create_job_with_concatenated_inputs] |
| 30 | + /** |
| 31 | + * TODO(developer): Uncomment these variables before running the sample. |
| 32 | + */ |
| 33 | + // projectId = 'my-project-id'; |
| 34 | + // location = 'us-central1'; |
| 35 | + // inputUri1 = 'gs://my-bucket/my-video-file1'; |
| 36 | + // startTimeOffset1 = 0; |
| 37 | + // endTimeOffset1 = 8.1; |
| 38 | + // inputUri2 = 'gs://my-bucket/my-video-file2'; |
| 39 | + // startTimeOffset2 = 3.5; |
| 40 | + // endTimeOffset2 = 15; |
| 41 | + // outputUri = 'gs://my-bucket/my-output-folder/'; |
| 42 | + |
| 43 | + function calcOffsetNanoSec(offsetValueFractionalSecs) { |
| 44 | + if (offsetValueFractionalSecs.toString().indexOf('.') !== -1) { |
| 45 | + return ( |
| 46 | + 1000000000 * |
| 47 | + Number('.' + offsetValueFractionalSecs.toString().split('.')[1]) |
| 48 | + ); |
| 49 | + } |
| 50 | + return 0; |
| 51 | + } |
| 52 | + const startTimeOffset1Sec = Math.trunc(startTimeOffset1); |
| 53 | + const startTimeOffset1NanoSec = calcOffsetNanoSec(startTimeOffset1); |
| 54 | + const endTimeOffset1Sec = Math.trunc(endTimeOffset1); |
| 55 | + const endTimeOffset1NanoSec = calcOffsetNanoSec(endTimeOffset1); |
| 56 | + |
| 57 | + const startTimeOffset2Sec = Math.trunc(startTimeOffset2); |
| 58 | + const startTimeOffset2NanoSec = calcOffsetNanoSec(startTimeOffset2); |
| 59 | + const endTimeOffset2Sec = Math.trunc(endTimeOffset2); |
| 60 | + const endTimeOffset2NanoSec = calcOffsetNanoSec(endTimeOffset2); |
| 61 | + |
| 62 | + // Imports the Transcoder library |
| 63 | + const {TranscoderServiceClient} = |
| 64 | + require('@google-cloud/video-transcoder').v1; |
| 65 | + |
| 66 | + // Instantiates a client |
| 67 | + const transcoderServiceClient = new TranscoderServiceClient(); |
| 68 | + |
| 69 | + async function createJobWithConcatenatedInputs() { |
| 70 | + // Construct request |
| 71 | + const request = { |
| 72 | + parent: transcoderServiceClient.locationPath(projectId, location), |
| 73 | + job: { |
| 74 | + outputUri: outputUri, |
| 75 | + config: { |
| 76 | + inputs: [ |
| 77 | + { |
| 78 | + key: 'input1', |
| 79 | + uri: inputUri1, |
| 80 | + }, |
| 81 | + { |
| 82 | + key: 'input2', |
| 83 | + uri: inputUri2, |
| 84 | + }, |
| 85 | + ], |
| 86 | + editList: [ |
| 87 | + { |
| 88 | + key: 'atom1', |
| 89 | + inputs: ['input1'], |
| 90 | + startTimeOffset: { |
| 91 | + seconds: startTimeOffset1Sec, |
| 92 | + nanos: startTimeOffset1NanoSec, |
| 93 | + }, |
| 94 | + endTimeOffset: { |
| 95 | + seconds: endTimeOffset1Sec, |
| 96 | + nanos: endTimeOffset1NanoSec, |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + key: 'atom2', |
| 101 | + inputs: ['input2'], |
| 102 | + startTimeOffset: { |
| 103 | + seconds: startTimeOffset2Sec, |
| 104 | + nanos: startTimeOffset2NanoSec, |
| 105 | + }, |
| 106 | + endTimeOffset: { |
| 107 | + seconds: endTimeOffset2Sec, |
| 108 | + nanos: endTimeOffset2NanoSec, |
| 109 | + }, |
| 110 | + }, |
| 111 | + ], |
| 112 | + |
| 113 | + elementaryStreams: [ |
| 114 | + { |
| 115 | + key: 'video-stream0', |
| 116 | + videoStream: { |
| 117 | + h264: { |
| 118 | + heightPixels: 360, |
| 119 | + widthPixels: 640, |
| 120 | + bitrateBps: 550000, |
| 121 | + frameRate: 60, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + key: 'audio-stream0', |
| 127 | + audioStream: { |
| 128 | + codec: 'aac', |
| 129 | + bitrateBps: 64000, |
| 130 | + }, |
| 131 | + }, |
| 132 | + ], |
| 133 | + muxStreams: [ |
| 134 | + { |
| 135 | + key: 'sd', |
| 136 | + container: 'mp4', |
| 137 | + elementaryStreams: ['video-stream0', 'audio-stream0'], |
| 138 | + }, |
| 139 | + ], |
| 140 | + }, |
| 141 | + }, |
| 142 | + }; |
| 143 | + |
| 144 | + // Run request |
| 145 | + const [response] = await transcoderServiceClient.createJob(request); |
| 146 | + console.log(`Job: ${response.name}`); |
| 147 | + } |
| 148 | + |
| 149 | + createJobWithConcatenatedInputs(); |
| 150 | + // [END transcoder_create_job_with_concatenated_inputs] |
| 151 | +} |
| 152 | + |
| 153 | +// node createJobFromStaticOverlay.js <projectId> <location> <inputUri1> <startTimeOffset1> <endTimeOffset1> <inputUri2> <startTimeOffset2> <endTimeOffset2> <outputUri> |
| 154 | +process.on('unhandledRejection', err => { |
| 155 | + console.error(err.message); |
| 156 | + process.exitCode = 1; |
| 157 | +}); |
| 158 | +main(...process.argv.slice(2)); |
0 commit comments