AWS Lambda函数用于剪辑视频,可以执行以下步骤:
aws iam create-role --role-name lambda-cli-role --assume-role-policy-document file://trust-policy.json
aws iam attach-role-policy --role-name lambda-cli-role --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
aws lambda create-function --function-name trim-video --runtime nodejs12.x --role arn:aws:iam::123456789012:role/lambda-cli-role --handler index.handler --zip-file fileb://./trim-video.zip --timeout 60
exports.handler = async(event) => {
const sourceBucket = event.Records[0].s3.bucket.name;
const sourceKey = event.Records[0].s3.object.key;
const targetBucket = 'target-bucket';
const targetKey = sourceKey.replace(/\.mp4$/, '-trimmed.mp4');
const params = {
FunctionName: 'ffmpeg-layer',
Payload: JSON.stringify({
sourceBucket: sourceBucket,
sourceKey: sourceKey,
targetBucket: targetBucket,
targetKey: targetKey
})
};
await lambda.invoke(params).promise();
const s3 = new aws.S3();
const response = await s3.getObject({
Bucket: targetBucket,
Key: targetKey
}).promise();
const data = await parseMeta(response.Body);
console.log(data);
const sns = new aws.SNS();
const message = `Video trimmed: ${sourceKey} -> ${targetKey}\n${JSON.stringify(data, null, 2)}`;
await sns.publish({
TopicArn: 'arn:aws:sns:us-east-1:123456789012:my-topic',
Message: message
}).promise();
};
const FfmpegCommand = require('fluent-ffmpeg');
exports.handler = async(event) => {
const { sourceBucket, sourceKey, targetBucket, targetKey } = event;
const s3source = `s3://${sourceBucket}/${sourceKey}`;
const s3target = `s3://${targetBucket}/${targetKey}`;
const downloadCommand = new FfmpegCommand()
.input(s3source)
.audioCodec('copy')
.videoCodec('copy')
.output('pipe:');
const