-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtran.js
More file actions
110 lines (96 loc) · 3.31 KB
/
tran.js
File metadata and controls
110 lines (96 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const Airtable = require("airtable");
const AWS = require("aws-sdk");
const axios = require("axios");
const s3 = new AWS.S3();
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Initialize Airtable
Airtable.configure({
endpointUrl: "https://api.airtable.com",
apiKey: "key0aVfVmWLnBm0dC",
});
const base = Airtable.base("appWveOREcCzpb8yt");
// Initialize AWS
AWS.config.update({ region: "eu-west-2" });
const transcribeservice = new AWS.TranscribeService();
base("Videos")
.select({
view: "Grid view",
})
.eachPage(
async (records, fetchNextPage) => {
for (const record of records) {
// Only process records where tranJobId is empty
const tranJobId = record.get("tranJobId");
if (tranJobId) continue;
const videoField = record.get("video");
if (videoField) {
try {
// download video and check its content type
const videoUrl = videoField[0].url;
const videoName = videoField[0].filename;
// Get video from url
const response = await axios.get(videoUrl, {
responseType: "arraybuffer",
});
const videoBuffer = response.data;
const contentType = response.headers["content-type"];
if (!contentType.startsWith("video/")) {
// This is not a video file, skip it
console.log(`File ${videoName} is not a video, skipping...`);
continue;
}
// Upload video to S3
const s3Params = {
Bucket: "drpcrd-dashboard",
Key: `tran/${videoName}`,
Body: videoBuffer,
ContentType: contentType,
};
const uploadResult = await s3.upload(s3Params).promise();
// start transcription job
const transcribeParams = {
LanguageCode: "en-US", // set language code
Media: {
MediaFileUri: `s3://drpcrd-dashboard/tran/${videoName}`,
},
MediaFormat: "mp4", // or other format as per your video file
TranscriptionJobName: `${record.getId()}-transcription`,
};
const transcribeResponse = await transcribeservice
.startTranscriptionJob(transcribeParams)
.promise();
// Update the Airtable record with the TranscriptionJobName (Job ID)
base("Videos").update(
[
{
id: record.id,
fields: {
tranJobId:
transcribeResponse.TranscriptionJob.TranscriptionJobName,
},
},
],
(err, records) => {
if (err) {
throw err; // throw error to be caught in outer catch block
}
// console.log(records);
}
);
} catch (err) {
// log error and continue with the next record
console.error(`Error processing record ${record.id}: ${err}`);
continue;
}
await delay(1000); // delay in milliseconds
}
}
// fetch next page of records
fetchNextPage();
},
(err) => {
if (err) console.error(err);
}
);