Generate the output filename to match the original video

Using a simple function script, you can save the output file with a filename that matches the original video. An example flow is: you want AI-generated SRT captions for a video you uploaded to S3. You want the SRT saved back into AWS S3 and with the same filename as that of the original video.

Clone an example Flow and get the Function script too here.

To manually add the function script to your Flow:

First, go to Functions and "+ Add a Function":

Add this script into main window:

const S3URLParser = require('amazon-s3-uri');

async function main (service, context) {

const { region, bucket, key } = S3URLParser(service.videoURL)
const objectName = getObjectName(key);
console.log(objectName);
const fileName = getFileName(objectName);
console.log(fileName);
const extension = getExtension(objectName);
console.log(extension);
return {
name: fileName,
ext: extension,
fileName: `/${service.folder}/${fileName}${service.ext}`
}
}

function getExtension(fileName) {
return (/[.]/.exec(fileName)) ? /[^.]+$/.exec(fileName) : '';
}

function getFileName(path) {
return path.split("/").pop().split('.')[0];
}

function getObjectName(url) {
return url.split("/").pop();
}

module.exports = main;

In Packages, add the following:

[
'amazon-s3-uri'
]

So it looks like this:

Add these Input parameters:

[
{
dataTypeId: 4,
supportedDataTypeIds: [
'4',
'16',
'14',
'12',
'15',
'1',
'2',
'3',
'6',
'7'
],
require: true,
key: 'videoURL',
dataType: 'string'
},
{
dataTypeId: 4,
supportedDataTypeIds: [
'4',
'16',
'14',
'12',
'15',
'1',
'2',
'3',
'6',
'7'
],
key: 'folder',
dataType: 'string'
},
{
dataTypeId: 4,
supportedDataTypeIds: [
'4',
'16',
'14',
'12',
'15',
'1',
'2',
'3',
'6',
'7'
],
require: true,
key: 'ext',
dataType: 'string'
}
]

Add these Output parameters:

[
{
dataTypeId: 4,
require: true,
key: 'name',
dataType: 'string'
},
{
dataTypeId: 4,
require: true,
key: 'ext',
dataType: 'string'
},
{
dataTypeId: 4,
require: true,
key: 'fileName',
dataType: 'string'
}
]

It should all look like this:

Now setup your Flow. Add the Function before the Transfer to Storage step.

In the Function setup, notice the red box where you need to choose the details correctly. "Folder" refers to the folder in AWS S3 you want to save the output file to.

In the Transfer to Storage step, choose the "fileName" output from the Function step as the input for the field "File Name," as highlighted in red below:

That's all! Turn on your Flow and output files will save to Storage with a filename that matches the original ingested media.

Did this answer your question?