Skip to content

Commit 2ebebc5

Browse files
author
thegamerhat
committed
implement serviceaccount
1 parent 3b902e9 commit 2ebebc5

21 files changed

+150
-86
lines changed

.dockerignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
node_modules
2+
dist
3+
.DS_Store
4+
5+
# Ignore the output video from Git but not videos you import into src/.
6+
out
7+
.env
8+
9+
# GCP credentials
10+
serviceaccount.json

.env.example

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
SERVER_PORT=5050
22

3-
GOOGLE_CLOUD_PRIVATE_KEY=
4-
GOOGLE_CLOUD_CLIENT_EMAIL=
5-
GOOGLE_CLOUD_CLIENT_ID=
6-
73
FIREBASE_API_KEY=
84
FIREBASE_AUTH_DOMAIN=
95
FIREBASE_PROJECT_ID=
106
FIREBASE_STORAGE_BUCKET=
117
FIREBASE_MESSAGING_SENDER_ID=
128
FIREBASE_APP_ID=
9+
10+
GOOGLE_APPLICATION_CREDENTIALS="serviceaccount.json"
11+
12+
# NOT REQUIRED IF YOU HAVE `GOOGLE_APPLICATION_CREDENTIALS`
13+
#
14+
# GOOGLE_CLOUD_PRIVATE_KEY=
15+
# GOOGLE_CLOUD_CLIENT_EMAIL=
16+
# GOOGLE_CLOUD_CLIENT_ID=

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ dist
55
# Ignore the output video from Git but not videos you import into src/.
66
out
77
.env
8+
9+
# GCP credentials
10+
serviceaccount.json

.vscode/extensions.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
3+
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
4+
5+
// List of extensions which should be recommended for users of this workspace.
6+
"recommendations": [
7+
8+
],
9+
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
10+
"unwantedRecommendations": [
11+
12+
]
13+
}

README.md

+79-35
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,90 @@
99
</a>
1010
</p>
1111

12+
## PROBLEMS TO FIX:
13+
14+
1. Must kill the express server after render finishes.
15+
1216
Welcome to your TTS Remotion project!
1317

1418
## Get Started
1519

16-
- Create Azure Account
17-
- Create TTS service on Azure
18-
- Create AWS Account
19-
- Setup S3 Bucket with public access
20-
- Configure bucket policy
21-
```json
22-
{
23-
"Version": "2008-10-17",
24-
"Statement": [
25-
{
26-
"Sid": "AllowPublicRead",
27-
"Effect": "Allow",
28-
"Principal": {
29-
"AWS": "*"
30-
},
31-
"Action": "s3:GetObject",
32-
"Resource": "arn:aws:s3:::<YOUR-BUCKET-NAME>/*"
33-
}
34-
]
20+
1. Create Firebase Project
21+
![Create project](./assets/firebase-create.png)
22+
23+
2. Go to Project Settings. In the "General" tab, go to "Your apps" section and register a "Web App".
24+
25+
<!-- VIDEO -->
26+
27+
/assets/firebase-register.mp4
28+
29+
3. Copy the config credentials and paste into `.env`
30+
31+
4. Enable storage, create storage bucket, and edit rules to allow read, write for `remotion-gtts` directory (or any other directory that you have specified for `audioDirectoryInBucket` in the `constants.ts` file).
32+
33+
<!-- VIDEO -->
34+
35+
/assets/firebase-storage-enable.mp4
36+
37+
- Configure bucket rules
38+
39+
```js
40+
rules_version = '2';
41+
service firebase.storage {
42+
match /b/{bucket}/o {
43+
match /remotion-gtts/{allPaths=**} {
44+
allow read, write: if true;
45+
}
3546
}
36-
```
37-
- Configure bucket CORS
38-
- Use it only as a template, we recommend you to edit "AllowedOrigins" entering your origin
39-
```json
40-
[
41-
{
42-
"AllowedHeaders": ["*"],
43-
"AllowedMethods": ["HEAD", "GET", "PUT", "POST", "DELETE"],
44-
"AllowedOrigins": ["*"],
45-
"ExposeHeaders": ["ETag", "x-amz-meta-custom-header"]
46-
}
47-
]
48-
```
49-
- Copy `.env.example` to `.env` entering your secrets
50-
- ⚠️ Ensure your AWS credentials only allow reading and uploading to a specific S3 bucket `s3:GetObject` and `s3:PutObject` to not compromise your credentials if you deploy your Remotion project
51-
- Use method `textToSpeech` from `src/TextToSpeech/tts.ts` to convert Text to Audio, this method will return file url, you can use it as source of `<Audio />` component
47+
}
48+
```
49+
50+
> Maybe you should have better security validation instead of just allowing `write` to everyone.
51+
52+
![Create rules](./assets/firebase-storage-rules.png)
53+
54+
5. Enable TTS API on GCP
55+
56+
- You will probably already have a project set up in GCP with the same name as the Firebase project you created earlier. Just use that, to keep things simple.
57+
58+
- Open hambuger menu, go to APIs and Services -> Library
59+
- Search for "text to speech", and enable **Cloud Text-to-Speech API**. You may be required to enable billing, by creating a billing account. (Be sure to also review the pricing tab)
60+
61+
![Create credentials](/assets/gcp-enable-api.png)
62+
63+
<!-- VIDEO -->
64+
65+
/assets/gcp-enable-api.mp4
66+
67+
6. Create Credentials
68+
69+
- After API is enabled, click on **Manage** -> **Credentials** (on sidebar)
70+
71+
![Create credentials](/assets/gcp-manage-api.png)
72+
73+
- Click on **CREATE CREDENTIALS** and select Service Account
74+
75+
![Create credentials](/assets/gcp-create-credentials.png)
76+
77+
- Fill relevant fields, select the _Basic_ role of **_Owner_**, and skip the other optional fields if not required.
78+
79+
<!-- VIDEO -->
80+
81+
/assets/gcp-create-serviceaccount.mp4
82+
83+
- Now create a JSON key to download credentials as a `.json` file.
84+
85+
<!-- VIDEO -->
86+
87+
/assets/gcp-create-key.mp4
88+
89+
- Name the JSON file as `serviceaccount.json` and place it in the root of your project - `/serviceaccount.json`
90+
91+
> **IMPORTANT:** This file must never be committed, and must be added to .gitignore, .dockerignore, etc. if you change its name to something different.
92+
93+
> If you change the location of this file, make sure to also update `GOOGLE_APPLICATION_CREDENTIALS` in `.env`
94+
95+
7. Copy `.env.example` to `.env` and enter your secrets.
5296

5397
## Example
5498

assets/firebase-create.png

166 KB
Loading

assets/firebase-register.mp4

1.46 MB
Binary file not shown.

assets/firebase-storage-enable.mp4

1.59 MB
Binary file not shown.

assets/firebase-storage-rules.png

63.6 KB
Loading

assets/gcp-create-credentials.png

35.1 KB
Loading

assets/gcp-create-key.mp4

2.63 MB
Binary file not shown.

assets/gcp-create-serviceaccount.mp4

2.32 MB
Binary file not shown.

assets/gcp-enable-api.mp4

503 KB
Binary file not shown.

assets/gcp-enable-api.png

28.4 KB
Loading

assets/gcp-manage-api.png

25.5 KB
Loading

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "My Remotion video",
55
"scripts": {
66
"start": "npm run audioserver & remotion preview",
7-
"build": "remotion render HelloWorld out/video.mp4",
7+
"build": "npm run audioserver & sleep 5; remotion render HelloWorld out/video.mp4; pkill -f \"npm run audioserver\"",
88
"audioserver": "ts-node src/server/index.ts",
99
"upgrade": "remotion upgrade",
1010
"test": "eslint src --ext ts,tsx,js,jsx && tsc"

src/Root.tsx

+33-31
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,38 @@ import {Composition} from 'remotion';
22
import {HelloWorld} from './HelloWorld';
33

44
export const RemotionRoot: React.FC = () => {
5-
// If (!process.env.AZURE_TTS_KEY) {
6-
// throw new Error(
7-
// 'AZURE_TTS_KEY environment variable is missing. Read the instructions in README.md file and complete the setup.'
8-
// );
9-
// }
10-
// if (!process.env.AZURE_TTS_REGION) {
11-
// throw new Error(
12-
// 'AZURE_TTS_REGION environment variable is missing. Read the instructions in README.md file and complete the setup.'
13-
// );
14-
// }
15-
// if (!process.env.AWS_S3_BUCKET_NAME) {
16-
// throw new Error(
17-
// 'AWS_S3_BUCKET_NAME environment variable is missing. Read the instructions in README.md file and complete the setup.'
18-
// );
19-
// }
20-
// if (!process.env.AWS_S3_REGION) {
21-
// throw new Error(
22-
// 'AWS_S3_REGION environment variable is missing. Read the instructions in README.md file and complete the setup.'
23-
// );
24-
// }
25-
// if (!process.env.AWS_ACCESS_KEY_ID) {
26-
// throw new Error(
27-
// 'AWS_ACCESS_KEY_ID environment variable is missing. Read the instructions in README.md file and complete the setup.'
28-
// );
29-
// }
30-
// if (!process.env.AWS_SECRET_ACCESS_KEY) {
31-
// throw new Error(
32-
// 'AWS_SECRET_ACCESS_KEY environment variable is missing. Read the instructions in README.md file and complete the setup.'
33-
// );
34-
// }
5+
if (!process.env.GOOGLE_APPLICATION_CREDENTIALS)
6+
throw new Error(
7+
'GOOGLE_APPLICATION_CREDENTIALS environment variable is missing. Read the instructions in README.md file and complete the setup.'
8+
);
9+
10+
if (!process.env.FIREBASE_API_KEY)
11+
throw new Error(
12+
'FIREBASE_API_KEY environment variable is missing. Read the instructions in README.md file and complete the setup.'
13+
);
14+
if (!process.env.FIREBASE_AUTH_DOMAIN)
15+
throw new Error(
16+
'FIREBASE_AUTH_DOMAIN environment variable is missing. Read the instructions in README.md file and complete the setup.'
17+
);
18+
19+
if (!process.env.FIREBASE_PROJECT_ID)
20+
throw new Error(
21+
'FIREBASE_PROJECT_ID environment variable is missing. Read the instructions in README.md file and complete the setup.'
22+
);
23+
if (!process.env.FIREBASE_STORAGE_BUCKET)
24+
throw new Error(
25+
'FIREBASE_STORAGE_BUCKET environment variable is missing. Read the instructions in README.md file and complete the setup.'
26+
);
27+
28+
if (!process.env.FIREBASE_MESSAGING_SENDER_ID)
29+
throw new Error(
30+
'FIREBASE_MESSAGING_SENDER_ID environment variable is missing. Read the instructions in README.md file and complete the setup.'
31+
);
32+
33+
if (!process.env.FIREBASE_APP_ID)
34+
throw new Error(
35+
'FIREBASE_APP_ID environment variable is missing. Read the instructions in README.md file and complete the setup.'
36+
);
3537

3638
return (
3739
<>
@@ -44,7 +46,7 @@ export const RemotionRoot: React.FC = () => {
4446
height={1080}
4547
defaultProps={{
4648
titleText:
47-
'Text to speech on Remotion v4, working with Firebase + Google Cloud, with realtime text changes...',
49+
'Text to speech on Remotion v4, working with Firebase + Google Cloud!',
4850
titleColor: 'black',
4951
}}
5052
/>

src/lib/client-utils.ts

-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ export const getTTSFromServer = async (
1818
})
1919
).json();
2020

21-
console.log(`DEBUG: You got result!!!!`);
22-
console.log(result);
23-
2421
return result.url;
2522
} catch (err) {
2623
console.error(err);

src/lib/firebase/utils.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const uploadFileToFirebase = async (
1212

1313
// Upload file
1414
const uploadedFile = await uploadBytes(storageRef, audioData);
15-
console.log('🫰🏼 File uploaded!');
1615
return uploadedFile;
1716
}; // Will return big object
1817

@@ -34,7 +33,7 @@ export const checkIfAudioHasAlreadyBeenSynthesized = async (
3433

3534
// Return URL for download
3635
const url = await getDownloadURL(ref(storage, filePath));
37-
console.log(`DEBUG: File exists! 🥳`);
36+
// Console.log(`Requested file already exists!`);
3837
return url;
3938
} catch {
4039
return false;

src/server/TextToSpeech/index.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ import {
99
import {audioDirectoryInBucket, voices} from './constants';
1010
import textToSpeech from '@google-cloud/text-to-speech';
1111

12-
const client = new textToSpeech.TextToSpeechClient({
13-
credentials: {
14-
private_key: process.env.GOOGLE_CLOUD_PRIVATE_KEY,
15-
client_email: process.env.GOOGLE_CLOUD_CLIENT_EMAIL,
16-
client_id: process.env.GOOGLE_CLOUD_CLIENT_ID,
17-
},
18-
});
12+
const client = new textToSpeech.TextToSpeechClient();
1913

2014
export const createTextToSpeechAudio = async (
2115
text: string,

src/server/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ dotenv.config();
1515
const app = express();
1616
const port = process.env.SERVER_PORT || 5050;
1717

18-
// Const tmpDir = fs.promises.mkdtemp(path.join(os.tmpdir(), 'remotion-'));
19-
2018
// This setting will reveal the real IP address of the user, so we can apply rate limiting.
2119
app.set('trust proxy', 1);
2220

@@ -40,5 +38,5 @@ app.post(`/getdata`, async (req, res) => {
4038
});
4139

4240
app.listen(port, () => {
43-
console.log(`DEBUG: Umm audio server actually listening on ${port}`);
41+
console.log(`TTS server listening on ${port}`);
4442
});

0 commit comments

Comments
 (0)