Skip to content

Commit ae495b5

Browse files
committed
Use ffmpeg build for Amazon Linux 2023
Use @aws-sdk/lib-storage to upload transcoded audio to S3 Make sure ffmpeg exits cleanly
1 parent c9a9b60 commit ae495b5

10 files changed

+58
-47
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,4 @@ env.*.json
231231
*.parameters
232232
/schemas
233233
.sam-pids
234+
av-download/layers/ffmpeg/bin

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,6 @@ sync-code: sync
126126
secrets:
127127
ln -s ../tfvars/dc-api/*.yaml .
128128
clean:
129-
rm -rf .aws-sam api/.aws-sam chat/.aws-sam av-download/.aws-sam api/node_modules api/src/node_modules chat/**/__pycache__ chat/.coverage chat/.ruff_cache
129+
rm -rf .aws-sam api/.aws-sam chat/.aws-sam av-download/.aws-sam api/node_modules api/src/node_modules av-download/layers chat/**/__pycache__ chat/.coverage chat/.ruff_cache
130130
reset:
131131
for f in $$(find . -maxdepth 2 -name '*.orig'); do mv $$f $${f%%.orig}; done

api/template.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Description: dc-api-v2 API
1313
Globals:
1414
Function:
1515
CodeUri: ./src
16-
Runtime: nodejs20.x
16+
Runtime: nodejs22.x
1717
Architectures:
1818
- x86_64
1919
MemorySize: 128
@@ -130,10 +130,10 @@ Resources:
130130
#* Description: Dependencies for API handlers
131131
#* ContentUri: ./dependencies
132132
#* CompatibleRuntimes:
133-
#* - nodejs20.x
133+
#* - nodejs22.x
134134
#* LicenseInfo: Apache-2.0
135135
#* Metadata:
136-
#* BuildMethod: nodejs20.x
136+
#* BuildMethod: nodejs22.x
137137
# Configuration
138138
apiConfiguration:
139139
Type: AWS::SecretsManager::Secret

av-download/lambdas/package-lock.json

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

av-download/lambdas/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
"aws-sdk": "^2.1640.0"
1616
},
1717
"dependencies": {
18-
"fluent-ffmpeg": "2.1.2"
18+
"fluent-ffmpeg": "2.1.3"
1919
}
2020
}
+39-24
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
1-
const AWS = require('aws-sdk');
1+
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
2+
const { Upload } = require('@aws-sdk/lib-storage');
23
const ffmpeg = require('fluent-ffmpeg');
34
const stream = require('stream');
45

6+
57
module.exports.handler = async (event) => {
6-
7-
const s3 = new AWS.S3();
8+
const s3Client = new S3Client();
89
const url = event.streamingUrl;
910
const referer = event.referer || null;
1011
const inputOptions = referer ? ["-referer", referer] : [];
1112
const bucket = event.destinationBucket;
1213
const key = event.destinationKey;
1314
const pass = new stream.PassThrough();
15+
const upload = new Upload({
16+
client: s3Client,
17+
params: {
18+
Bucket: bucket,
19+
Key: key,
20+
Body: pass,
21+
ContentType: 'audio/mp3'
22+
}
23+
});
24+
25+
upload.on("end", ({ loaded, total, part, Key, Bucket }) => {
26+
console.log(`Uploaded ${total} bytes in ${part} parts to s3://${Bucket}/${Key}`);
27+
});
1428

15-
return new Promise((resolve, reject) => {
16-
ffmpeg()
29+
try {
30+
ffmpeg({ logger: console })
1731
.input(url)
1832
.inputOptions(inputOptions)
19-
.format('mp3')
20-
.output(pass, { end: true })
21-
.on('start', () => {
22-
s3.upload({
23-
Bucket: bucket,
24-
Key: key,
25-
Body: pass,
26-
}, (error, _data) => {
27-
if (error) {
28-
console.error('upload failed', error);
29-
reject(error);
30-
} else {
31-
resolve({ success: true });
32-
}
33-
});
33+
.format("mp3")
34+
.output(pass, { end: false })
35+
.on('start', function(commandLine) {
36+
console.log('Spawned Ffmpeg with command: ' + commandLine);
3437
})
35-
.on('error', (error) => {
36-
console.error('ffmpeg error', error);
37-
reject(error);
38+
.on("error", (error) => {
39+
console.error("ffmpeg error", error);
40+
})
41+
.on("end", () => {
42+
console.log("ffmpeg finished");
43+
pass.end();
44+
})
45+
.on("*", (event, ...args) => {
46+
console.log('ffmpeg fired event:', event, 'with', args);
3847
})
3948
.run();
40-
});
49+
} catch (error) {
50+
console.error("ffmpeg error", error);
51+
reject(error);
52+
}
53+
54+
await upload.done();
55+
return { status: 200, message: 'done' };
4156
}

av-download/template.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Parameters:
1414
FfmpegLayer:
1515
Type: String
1616
Description: "FFMPEG Lambda Layer ARN"
17-
Default: "arn:aws:lambda:us-east-1:625046682746:layer:ffmpeg:11"
17+
Default: arn:aws:lambda:us-east-1:625046682746:layer:ffmpeg:15
1818
MediaConvertDestinationBucket:
1919
Type: String
2020
Description: S3 bucket destination for transcoded AV resource

chat/template.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ Resources:
263263
- bedrock:InvokeModelWithResponseStream
264264
Resource: "*"
265265
#* Metadata:
266-
#* BuildMethod: nodejs20.x
266+
#* BuildMethod: nodejs22.x
267267
ChatSyncFunction:
268268
Type: AWS::Serverless::Function
269269
Properties:
@@ -322,7 +322,7 @@ Resources:
322322
- bedrock:InvokeModelWithResponseStream
323323
Resource: "*"
324324
#* Metadata:
325-
#* BuildMethod: nodejs20.x
325+
#* BuildMethod: nodejs22.x
326326
ChatMetricsLog:
327327
Type: AWS::Logs::LogGroup
328328
Properties:

docs/template.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Resources:
1818
rootRedirect:
1919
Type: AWS::Serverless::Function
2020
Properties:
21-
Runtime: nodejs20.x
21+
Runtime: nodejs22.x
2222
CodeUri: ./redirect
2323
Handler: index.handler
2424
Timeout: 1

template.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ Parameters:
5555
DevTeamNetIds:
5656
Type: String
5757
Description: Northwestern NetIDs of the development team
58-
FfmpegLayer:
59-
Type: String
60-
Description: "FFMPEG Lambda Layer ARN"
61-
Default: "arn:aws:lambda:us-east-1:625046682746:layer:ffmpeg:11"
6258
EnvironmentPrefix:
6359
Type: String
6460
Description: Index Prefix
@@ -177,7 +173,6 @@ Resources:
177173
Properties:
178174
Location: ./av-download/template.yaml
179175
Parameters:
180-
FfmpegLayer: !Ref FfmpegLayer
181176
MediaConvertDestinationBucket: !Ref MediaConvertDestinationBucket
182177
MediaConvertEndpoint: !Ref MediaConvertEndpoint
183178
MediaConvertJobQueueArn: !Ref MediaConvertJobQueueArn

0 commit comments

Comments
 (0)