我在node.js服务器上使用bitmovin-javascript。我正在尝试为Firebase存储上的视频创建编码,并使用bitmovin-javascript API将输出保存到我的bitmovin帐户中。
我已经从旧的bitmovin api(v1)升级为javascript(现已弃用),并且正在实现当前的api(v2)。但是每次我运行该代码时,它都显示HTTP请求失败:在我的终端控制台中,HTTP响应代码被禁止为403。
我已经进行了交叉检查,并确保我正在从Firebase存储中读取的API密钥(accesss-key和secret-key)是有效密钥,并且bitmovin API密钥也是有效密钥。 bitmovin的输出ID也是有效的ID。
该api生成输入和视频编解码器配置,音频编解码器配置和清单,但不生成输出或编码。
该错误似乎是在api试图创建编码资源的时候发生的。
这是我的代码的样子:
const Bitmovin = require('bitmovin-javascript').default;
const start = async () => {
const bitmovin = Bitmovin({ 'apiKey': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
// create the input
const input = await bitmovin.encoding.inputs.gcs.create({
name: 'Input',
accessKey: 'GOOGXXXXXXXXXXXXXXXX',
secretKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
bucketName: 'project-name.appspot.com'
});
// create the output
const outputId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
// create the video and audio codec configurations
const videoCodecConfig1 = await bitmovin.encoding.codecConfigurations.h264.create({
name: 'H264 Codec Config',
bitrate: 240000,
width: 384,
profile: 'HIGH'
});
const audioCodecConfig = await bitmovin.encoding.codecConfigurations.aac.create({
name: 'my-aac-128kbit-cc',
bitrate: 128000, // 128 KBit/s
rate: 48000
});
// create the encoding resource
const encoding = await bitmovin.encoding.encodings.create({
name: 'Encoding',
cloudRegion: 'AUTO',
encoderVersion: '2.12.1'
});
// add the video and audio streams to the encoding
const inputPath = 'https://firebasestorage.googleapis.com/v0/b/project-name.appspot.com/o/new%2Fvideos%2Fsample.mp4?alt=media&token=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
const videoStreamConfig1 = {
codecConfigId: videoCodecConfig1.id,
inputStreams: [{
inputId: input.id,
inputPath: inputPath,
selectionMode: 'AUTO'
}]
};
const streamVideo1 = await bitmovin.encoding.encodings(encoding.id).streams.add(videoStreamConfig1);
const audioStreamConfig = {
codecConfigId: audioCodecConfig.id,
inputStreams: [{
inputId: input.id,
inputPath: inputPath,
selectionMode: 'AUTO'
}]
};
const audioStream = await bitmovin.encoding.encodings(encoding.id).streams.add(audioStreamConfig);
// add the video muxings to the encoding
const segmentLength = 4;
const outputPath = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/' + Date.now();
const segmentNaming = 'seg_%number%.m4s';
const initSegmentName = 'init.mp4';
const fmp4VideoMuxingConfig1 = {
segmentLength,
segmentNaming,
initSegmentName,
streams: [{
streamId: streamVideo1.id
}],
outputs: [{
outputId: outputId,
outputPath: outputPath + '/video/384_240000/fmp4/',
acl: [{
permission: 'PUBLIC_READ'
}]
}]
};
const videoMuxing1 = await bitmovin.encoding.encodings(encoding.id).muxings.fmp4.add(fmp4VideoMuxingConfig1);
// add the audio muxing to the encoding
const fmp4AudioMuxingConfig = {
segmentLength,
segmentNaming,
initSegmentName,
streams: [{
streamId: audioStream.id
}],
outputs: [{
outputId: outputId,
outputPath: outputPath + '/audio/128000/fmp4/',
acl: [{
permission: 'PUBLIC_READ'
}]
}]
};
const fmp4AudioMuxing = await bitmovin.encoding.encodings(encoding.id).muxings.fmp4.add(fmp4AudioMuxingConfig);
//create the manifest
const manifestConfig = {
name: 'Manifest',
manifestName: 'manifest.mpd',
outputs: [{
outputId: outputId,
outputPath: outputPath,
acl: [{
permission: 'PUBLIC_READ'
}]
}]
};
const manifest = await bitmovin.encoding.manifests.dash.create(manifestConfig);
const period = await bitmovin.encoding.manifests.dash(manifest.id).periods.add({});
let videoAdaptationSet = {
};
let audioAdaptationSet = {
lang: 'en'
};
videoAdaptationSet = await bitmovin.encoding.manifests
.dash(manifest.id)
.periods(period.id)
.adaptationSets.video.create(videoAdaptationSet);
audioAdaptationSet = await bitmovin.encoding.manifests
.dash(manifest.id)
.periods(period.id)
.adaptationSets.audio.create(audioAdaptationSet);
// Adding Audio Representation
const fmp4AudioRepresentation = {
type: 'TEMPLATE',
encodingId: encoding.id,
muxingId: fmp4AudioMuxing.id,
segmentPath: 'audio/128000/fmp4'
};
await bitmovin.encoding.manifests
.dash(manifest.id)
.periods(period.id)
.adaptationSets(audioAdaptationSet.id)
.representations.fmp4
.add(fmp4AudioRepresentation);
// Adding Video Representation
const fmp4VideoRepresentation1 = {
type: 'TEMPLATE',
encodingId: encoding.id,
muxingId: videoMuxing1.id,
segmentPath: 'video/384_240000/fmp4'
};
await bitmovin.encoding.manifests
.dash(manifest.id)
.periods(period.id)
.adaptationSets(videoAdaptationSet.id)
.representations.fmp4
.add(fmp4VideoRepresentation1);
// start the encoding
await bitmovin.encoding.encodings(encoding.id).start();
// wait for the encoding to be finished
await finish(encoding);
//start and wait for the manifest to be finished
await bitmovin.encoding.manifests.dash(manifest.id).start();
console.log('Generating DASH manifest...');
const waitForManifestToBeFinished = async () => {
let manifestResponse;
do {
await bitmovin.encoding.manifests
.dash(manifest.id)
.status()
.then(response => {
console.log('DASH Manifest status is ' + response.status);
manifestResponse = response;
});
await new Promise(resolve => setTimeout(resolve, 2000));
}
while (manifestResponse.status === 'RUNNING' || manifestResponse.status === 'CREATED');
};
await waitForManifestToBeFinished();
console.log('Everything finished...');
function getStatus(encoding) {
return bitmovin.encoding.encodings(encoding.id).status();
}
function finish(encoding, interval = 5 * 1000, timeout = 5 * 60 * 1000) {
let t, s = new Date();
const check = (resolve, reject) => {
clearTimeout(t);
t = setTimeout(() => {
getStatus(encoding)
.then(current => {
console.log('Encoding progress: ' + current.progress);
if (current.status === 'FINISHED') {
return resolve(current);
}
if (current.status === 'ERROR') {
return reject('Encoding error');
}
if (new Date() - s >= timeout) {
return reject('Timeout reached');
}
check(resolve, reject);
});
}, interval);
}
return new Promise((resolve, reject) => {
check(resolve, reject);
});
}
}
start().catch(e => console.log(e.message));
我希望该API能够读取我的Firebase存储,对来自该存储的视频进行编码,将其存储在我的bitmovin输出下,为我提供一个到输出的bitmovin链接,以便可以将其保存到我自己的数据库中。
帮助将不胜感激。
最佳答案
到目前为止,我还没有注意到您提供的代码段有任何特定的问题。但是,此问题可能是由另一个因素引起的,例如输入/输出设置或输入文件本身。
请随时与[email protected]取得联系并提供编码ID和输入文件,以便我们对此进行更详细的研究。