gpt4 book ai didi

node.js - 将 HLS 播放列表 list 文件和媒体片段文件直接上传到 GCP

转载 作者:行者123 更新时间:2023-12-04 23:06:51 25 4
gpt4 key购买 nike

我正在开发 VOD 功能,过去几天我无法将生成的 HLS 播放列表和文件输出到 GCP。我正在使用 fluent-ffmpeg用于将 hls 播放列表上传到 GCP。下面是使用 Node.js 上传 HLS 到 GCP 的测试代码环境

const path = require('path');
const { Storage } = require('@google-cloud/storage');
const { createHLSPlaylist } = require('./utils/ffmpeg');

//* Creates a client, keyFileName is link to service account authentication credentials
const storage = new Storage({
keyFilename: path.join(
__dirname,
'./config/FILE.json'
),
projectId: process.env.GCP_PROJECT_ID,
});

//* To be used in upload GCP videos controller (copy this file contents within controller)
const url = 'URL';
const bucket = storage.bucket('BUCKETNAME');
const playlistName = 'file.m3u8';

const newFile = bucket.file(playlistName);

createHLSPlaylist(url, newFile.createWriteStream());
这是将 url 中的视频转码为 hls 播放列表的 ffmpeg 代码
const ffmpeg = require('fluent-ffmpeg');
const ffmpegInstaller = require('@ffmpeg-installer/ffmpeg');

ffmpeg.setFfmpegPath(ffmpegInstaller.path);

const createHLSPlaylist = (url, stream) => {
const command = ffmpeg(url, { timeout: 432000 })
.addOption([
'-profile:v baseline',
'-level 3.0',
'-start_number 0',
'-hls_time 10',
'-hls_list_size 0',
'-f hls',
])
.on('end', (stdout, stderr) => {
console.log('Transcoding succeeded !');
process.exit(1);
})
.on('start', (commandLine) => {
console.log('start', commandLine);
})
.on('codecData', (data) => {
console.log(`Input is ${data.audio} audio with ${data.video} video`);
})
.on('progress', (progress) => {
console.log(`Processing. Timemark: -> ${progress.timemark}`);
})
.on('stderr', (stderrLine) => {
// do nothing
})
.on('error', (err, stdout, stderr) => {
console.log(`error1: ${err}`);
console.log(`error2: ${stdout}`);
console.log(`error3: ${stderr}`);
console.log(`Cannot process video: ${err.message}`);
})
.on('data', (chunk) => {
console.log(`ffmpeg just wrote ${chunk.length} bytes`);
})
.pipe(stream, { end: true }); //end = true, close output stream after writing
};

module.exports = {
createHLSPlaylist,
};
我通过 console.log 收到以下消息

Processing. Timemark: -> 00:00:28.62

Processing. Timemark: -> 00:00:28.62

Processing. Timemark: -> 00:00:28.62

Processing. Timemark: -> 00:00:28.89

Transcoding succeeded !


但是 GCP 存储桶中没有任何内容,任何指针都将不胜感激
编辑:我还将 URL 转换为 readStream 并在 github 上遵循以下答案但这一次没有任何进展。

最佳答案

目前,我已经设法通过使用提到的临时文件存储将 HLS 播放列表上传到 GCP here。 .我设法通过以下方式解决了这个问题

newVideo = somebucket.file(filename);

file
.pipe(newVideo.createWriteStream({ gzip: 'auto' }))
.on('error', (err) => {
console.log(err);
})
.on('finish', () => {
console.log(`File ${filename} upload finished`);
newVideo
.getSignedUrl({ action: 'read', expires: '03-17-2022' })
.then((url) => {
const command = ffmpeg(url[0], { timeout: 432000 })
.addOptions([
'-profile:v baseline',
'-f segment',
'-level 3.0',
'-start_number 0',
'-hls_base_url segments/',
`-hls_segment_filename ./assets/${playlistName}/file%03d.ts`,
// Apple recommended time = 6,
// In seconds
'-hls_time 6',
'-hls_list_size 0',
// format
'-f hls',
])
.output(`./assets/${playlistName}/${playlistName}.m3u8`)
.on('end', async () => {
console.log('Transcoding succeeded !');
try {
const files = await readdir(`./assets/${playlistName}`);
console.log('files', files);
await Promise.all(
files.map((folderFile) =>
securebucket.upload(
`./assets/${playlistName}/${folderFile}`,
{
destination: folderFile,
}
)
)
).then(() => {
fs.rm(
`./assets/${playlistName}`,
{ recursive: true },
(err) => {
if (err) {
throw err;
}
console.log(`./assets/${playlistName} is deleted!`);
res.status(201).json(url[0]);
res.end();
}
);
});
} catch (err) {
console.error(err);
}
})
.on('start', (commandLine) => {
console.log('start', commandLine);
})
.on('codecData', (data) => {
console.log(
`Input is ${data.audio} audio with ${data.video} video`
);
})
.on('progress', (progress) => {
console.log(`Processing. Timemark: -> ${progress.timemark}`);
})
.on('error', (err, stdout, stderr) => {
console.log(`Cannot process video: ${err.message}`);
});

fs.mkdir(`./assets/${playlistName}`, (err) => {
if (err && err.code !== 'EEXIST') {
throw err;
} else {
command.run();
}
});
});
我仍然不相信这种方法,因为我相信这可以通过流媒体以更简单的方式实现,但似乎我无法为我的解决方案找到明确的答案,因为 fluent-ffmpeg 没有提供 s3 或 GCP 的示例

关于node.js - 将 HLS 播放列表 list 文件和媒体片段文件直接上传到 GCP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70435518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com