Skip to content

Commit d47ce53

Browse files
Upgrade ffmpeg-convert-audio sample to ES2017 syntax.
1 parent 077952f commit d47ce53

File tree

4 files changed

+33
-68
lines changed

4 files changed

+33
-68
lines changed

ffmpeg-convert-audio/README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,22 @@ The audio conversion is performed using ffmpeg. The audio is first downloaded lo
1010

1111
The dependencies are listed in [functions/package.json](functions/package.json).
1212

13-
1413
## Trigger rules
1514

1615
The function triggers on upload of any file to your Firebase project's default Cloud Storage bucket.
1716

18-
1917
## Deploy and test
2018

2119
To deploy and test the sample:
2220

23-
- Create a Firebase project on the [Firebase Console](https://console.firebase.google.com) and visit the **Storage** tab.
24-
- Get the code, for instance using `git clone https://github.com/firebase/functions-samples`
25-
- Enter the correct directory `cd functions-samples/ffmpeg-convert-audio`
26-
- Setup the CLI to use your Firebase project using `firebase use --add` and select your Firebase project
27-
- Deploy your project's code using `firebase deploy`
28-
- Go to the Firebase Console **Storage** tab and upload an audio. After a short time a converted audio with the same name but a `_output.flac` suffix will be created in the same folder (make sure you refresh the UI to see the new file).
21+
- Create a Firebase project on the [Firebase Console](https://console.firebase.google.com) and visit the **Storage** tab.
22+
- Get the code, for instance using `git clone https://github.com/firebase/functions-samples`
23+
- Enter the correct directory `cd functions-samples/ffmpeg-convert-audio`
24+
- Setup the CLI to use your Firebase project using `firebase use --add` and select your Firebase project
25+
- Deploy your project's code using `firebase deploy`
26+
- Go to the Firebase Console **Storage** tab and upload an audio. After a short time a converted audio with the same name but a `_output.flac` suffix will be created in the same folder (make sure you refresh the UI to see the new file).
2927

3028
## Notes
3129

32-
- Take into account that the audio files produced should not exceed the size of the memory of your function.
33-
- The audio conversion could take a certain amount of time, increase the timeout of your function using the cloud functions webgui so the function can run for a longer time.
34-
30+
- Take into account that the audio files produced should not exceed the size of the memory of your function.
31+
- The audio conversion could take a certain amount of time, increase the timeout of your function using the cloud functions webgui so the function can run for a longer time.

ffmpeg-convert-audio/functions/.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"parserOptions": {
33
// Required for certain syntax usages
4-
"ecmaVersion": 6
4+
"ecmaVersion": 2017
55
},
66
"plugins": [
77
"promise"

ffmpeg-convert-audio/functions/index.js

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,49 +23,21 @@ const fs = require('fs');
2323
const ffmpeg = require('fluent-ffmpeg');
2424
const ffmpeg_static = require('ffmpeg-static');
2525

26+
// Makes an ffmpeg command return a promise.
2627
function promisifyCommand(command) {
2728
return new Promise((resolve, reject) => {
28-
command
29-
.on('end', () => {
30-
resolve();
31-
})
32-
.on('error', (error) => {
33-
reject(error);
34-
})
35-
.run();
36-
});
37-
}
38-
39-
/**
40-
* Utility method to convert audio to mono channel using FFMPEG.
41-
*/
42-
function reencodeAsync(tempFilePath, targetTempFilePath) {
43-
return new Promise((resolve, reject) => {
44-
const command = ffmpeg(tempFilePath)
45-
.setFfmpegPath(ffmpeg_static.path)
46-
.audioChannels(1)
47-
.audioFrequency(16000)
48-
.format('flac')
49-
.on('error', (err) => {
50-
console.log('An error occurred: ' + err.message);
51-
reject(err);
52-
})
53-
.on('end', () => {
54-
console.log('Output audio created at', targetTempFilePath);
55-
})
56-
.save(targetTempFilePath);
29+
command.on('end', resolve).on('error', reject).run();
5730
});
5831
}
5932

6033
/**
6134
* When an audio is uploaded in the Storage bucket We generate a mono channel audio automatically using
6235
* node-fluent-ffmpeg.
6336
*/
64-
exports.generateMonoAudio = functions.storage.object().onFinalize((object) => {
37+
exports.generateMonoAudio = functions.storage.object().onFinalize(async (object) => {
6538
const fileBucket = object.bucket; // The Storage bucket that contains the file.
6639
const filePath = object.name; // File path in the bucket.
6740
const contentType = object.contentType; // File content type.
68-
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
6941

7042
// Exit if this is triggered on a file that is not an audio.
7143
if (!contentType.startsWith('audio/')) {
@@ -89,33 +61,26 @@ exports.generateMonoAudio = functions.storage.object().onFinalize((object) => {
8961
const targetTempFilePath = path.join(os.tmpdir(), targetTempFileName);
9062
const targetStorageFilePath = path.join(path.dirname(filePath), targetTempFileName);
9163

92-
return bucket.file(filePath).download({
93-
destination: tempFilePath,
94-
}).then(() => {
95-
console.log('Audio downloaded locally to', tempFilePath);
96-
// Convert the audio to mono channel using FFMPEG.
64+
await bucket.file(filePath).download({destination: tempFilePath});
65+
console.log('Audio downloaded locally to', tempFilePath);
66+
// Convert the audio to mono channel using FFMPEG.
9767

98-
let command = ffmpeg(tempFilePath)
99-
.setFfmpegPath(ffmpeg_static.path)
100-
.audioChannels(1)
101-
.audioFrequency(16000)
102-
.format('flac')
103-
.output(targetTempFilePath);
68+
let command = ffmpeg(tempFilePath)
69+
.setFfmpegPath(ffmpeg_static.path)
70+
.audioChannels(1)
71+
.audioFrequency(16000)
72+
.format('flac')
73+
.output(targetTempFilePath);
10474

105-
command = promisifyCommand(command);
75+
await promisifyCommand(command);
76+
console.log('Output audio created at', targetTempFilePath);
77+
// Uploading the audio.
78+
await bucket.upload(targetTempFilePath, {destination: targetStorageFilePath});
79+
console.log('Output audio uploaded to', targetStorageFilePath);
10680

107-
return command;
108-
}).then(() => {
109-
console.log('Output audio created at', targetTempFilePath);
110-
// Uploading the audio.
111-
return bucket.upload(targetTempFilePath, {destination: targetStorageFilePath});
112-
}).then(() => {
113-
console.log('Output audio uploaded to', targetStorageFilePath);
81+
// Once the audio has been uploaded delete the local file to free up disk space.
82+
fs.unlinkSync(tempFilePath);
83+
fs.unlinkSync(targetTempFilePath);
11484

115-
// Once the audio has been uploaded delete the local file to free up disk space.
116-
fs.unlinkSync(tempFilePath);
117-
fs.unlinkSync(targetTempFilePath);
118-
119-
return console.log('Temporary files removed.', targetTempFilePath);
120-
});
85+
console.log('Temporary files removed.', targetTempFilePath);
12186
});

ffmpeg-convert-audio/functions/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
"deploy": "firebase deploy --only functions",
2121
"logs": "firebase functions:log"
2222
},
23+
"engines": {
24+
"node": "8"
25+
},
2326
"private": true
2427
}

0 commit comments

Comments
 (0)