如何去除Transcoder API生成的spriteSheets文件名数字后缀?
Google Cloud Transcoder API 雪碧图文件名固定问题
Google Cloud Transcoder API目前不支持直接去掉雪碧图文件名的数字后缀,生成的文件名格式是固定的[filePrefix][10位数字].jpeg。
这个数字后缀是API的设计特性,主要用于应对视频时长较长的场景:当单张雪碧图无法容纳所有按间隔截取的缩略帧时,API会自动生成多张雪碧图,通过不同的数字后缀区分这些文件。
如果需要固定文件名,可以通过以下两种方式实现:
- 自动重命名(推荐):利用Pub/Sub的任务完成通知触发Cloud Functions,在任务结束后自动将带后缀的雪碧图文件重命名为固定名称。示例Node.js代码如下:
const { Storage } = require('@google-cloud/storage'); const storage = new Storage(); async function handleTranscodeComplete(message) { // 解析Pub/Sub消息中的输出路径信息 const jobData = JSON.parse(Buffer.from(message.data, 'base64').toString()); const outputPrefix = jobData.outputUri.replace('gs://your-cdn-bucket/', ''); // 查找对应前缀的雪碧图文件 const [files] = await storage.bucket('your-cdn-bucket').getFiles({ prefix: `${outputPrefix}spritesheet` }); if (files.length > 0) { const sourceFile = files[0]; const destinationPath = `${outputPrefix}spritesheet.jpeg`; await sourceFile.move(destinationPath); console.log(`雪碧图已重命名为: ${destinationPath}`); } }
- 手动/批量重命名:如果是少量任务,可以在任务完成后,通过Cloud Storage控制台或
gsutil命令行工具找到对应前缀的文件,手动重命名。例如使用gsutil mv命令:
gsutil mv gs://your-cdn-bucket/output-folder/spritesheet0000000000.jpeg gs://your-cdn-bucket/output-folder/spritesheet.jpeg
内容的提问来源于stack exchange,提问作者Rodrigo
相关产品推荐
相关产品推荐

