Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: Migrates code to typescript #65

Draft
wants to merge 3 commits into
base: v2/main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions @types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface DownloadedFileInfo {
source: {
name: string;
ext: string;
};
destination: { temp: { video: string; audio?: string } };
}
File renamed without changes.
File renamed without changes.
19 changes: 0 additions & 19 deletions functions/extractAudio.js

This file was deleted.

18 changes: 18 additions & 0 deletions functions/extractAudio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { videoBucket, downloadFile, uploadFlac } from "../utils/storage-utils";
import { extractAudio } from "../utils/ffmpeg-utils";

export default async (event) => {
const videoFile = videoBucket.file(event.data.name);
console.log("downloading video file 2...");
const fileInfo = await downloadFile(videoFile, event.data.name);

//extract audio and transcode to FLAC
const {
destination: {
temp: { audio: tempAudioPath },
},
} = await extractAudio(fileInfo);

console.log(`Uploading ${tempAudioPath} to flac bucket`);
return uploadFlac(tempAudioPath);
};
File renamed without changes.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"main": "app.js",
"scripts": {
"deploy": "node deploy.js",
"deploy": "ts-node deploy.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Abhishek Jain <[email protected]>",
Expand All @@ -30,5 +30,10 @@
"@google-cloud/speech": "^4.1.3",
"@google-cloud/storage": "^5.3.0",
"fluent-ffmpeg": "^2.1.2"
},
"devDependencies": {
"@types/fluent-ffmpeg": "^2.1.16",
"ts-node": "^9.0.0",
"typescript": "^4.0.3"
}
}
File renamed without changes.
29 changes: 0 additions & 29 deletions utils/ffmpeg-utils.js

This file was deleted.

35 changes: 35 additions & 0 deletions utils/ffmpeg-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { EventEmitter } from "events";

import { path as ffmpegPath } from "@ffmpeg-installer/ffmpeg";
import ffmpeg from "fluent-ffmpeg";
import { DownloadedFileInfo } from "../@types/types";

ffmpeg.setFfmpegPath(ffmpegPath);

export const extractAudio = async (fileInfo: DownloadedFileInfo) => {
let tempAudioPath = "/tmp/" + fileInfo.source.name + ".flac";
fileInfo.destination.temp.audio = tempAudioPath;
const {
destination: {
temp: { video: tempVideoPath },
},
} = fileInfo;
const stream = ffmpeg(tempVideoPath)
.videoBitrate(19200)
.inputOptions("-vn")
.format("flac")
.audioChannels(1)
.output(tempAudioPath);
stream.run();
await streamToPromise(stream);
return fileInfo;
};

const streamToPromise = async (stream: EventEmitter) => {
return new Promise((resolve, reject) => {
stream.on("end", () => resolve());
stream.on("error", (err) => reject(err));
});
};

export default extractAudio;
File renamed without changes.
69 changes: 0 additions & 69 deletions utils/storage-utils.js

This file was deleted.

45 changes: 45 additions & 0 deletions utils/storage-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import path from "path";
import { Storage, Bucket, File } from "@google-cloud/storage";
import { DownloadedFileInfo } from "../@types/types";

const appConfig = require("../app-config");

const storageClient = new Storage();

export const videoBucket = storageClient.bucket(appConfig.buckets.video);
export const flacBucket = storageClient.bucket(appConfig.buckets.audio);
export const outputBucket = storageClient.bucket(
appConfig.buckets.speechResponse
);

export const getFilePathFromFile = (storageFile: File) => {
return `gs://${storageFile.bucket.name}/${storageFile.name}`;
};

export const downloadFile = async (file: File, fileName: string):Promise<DownloadedFileInfo> => {
console.log("Download started for " + fileName);
let sourcePath = path.parse(fileName);
let tempDestination = "/tmp/" + fileName;
const response = await file.download({
destination: tempDestination,
});
return {
source: {
name: sourcePath.name,
ext: sourcePath.ext,
},
destination: { temp: { video: tempDestination } },
};
};

export const uploadToBucket = async (bucket: Bucket, filepath: string) => {
return bucket.upload(filepath);
};

export const uploadFlac = (filepath: string) => {
return flacBucket.upload(filepath);
};

export const uploadJsonOutput = (filepath: string) => {
return outputBucket.upload(filepath);
};
56 changes: 56 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@
dependencies:
"@types/node" "*"

"@types/fluent-ffmpeg@^2.1.16":
version "2.1.16"
resolved "https://registry.yarnpkg.com/@types/fluent-ffmpeg/-/fluent-ffmpeg-2.1.16.tgz#63949b0cb6bc88c9157a579cdd80858a269f3a3a"
integrity sha512-1FTstm6xY/2WsJVt6ARV7CiJjNCQDlR/nfw6xuYk5ITbVjk7sw89Biyqm2DGW4c3aZ3vBx+5irZvsql4eybpoQ==
dependencies:
"@types/node" "*"

"@types/long@^4.0.0", "@types/long@^4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.1.tgz#459c65fa1867dafe6a8f322c4c51695663cc55e9"
Expand Down Expand Up @@ -269,6 +276,11 @@ ansi-styles@^4.0.0:
dependencies:
color-convert "^2.0.1"

arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==

arrify@^2.0.0, arrify@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/arrify/-/arrify-2.0.1.tgz#c9655e9331e0abcd588d2a7cad7e9956f66701fa"
Expand Down Expand Up @@ -381,6 +393,11 @@ decamelize@^1.2.0:
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
integrity sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=

diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

dot-prop@^5.2.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-5.3.0.tgz#90ccce708cd9cd82cc4dc8c3ddd9abdd55b20e88"
Expand Down Expand Up @@ -675,6 +692,11 @@ make-dir@^3.0.0:
dependencies:
semver "^6.0.0"

make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

[email protected]:
version "1.44.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.44.0.tgz#fa11c5eb0aca1334b4233cb4d52f10c5a6272f92"
Expand Down Expand Up @@ -872,6 +894,19 @@ snakeize@^0.1.0:
resolved "https://registry.yarnpkg.com/snakeize/-/snakeize-0.1.0.tgz#10c088d8b58eb076b3229bb5a04e232ce126422d"
integrity sha1-EMCI2LWOsHazIpu1oE4jLOEmQi0=

source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"

source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

stream-events@^1.0.1, stream-events@^1.0.4, stream-events@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/stream-events/-/stream-events-1.0.5.tgz#bbc898ec4df33a4902d892333d47da9bf1c406d5"
Expand Down Expand Up @@ -930,6 +965,17 @@ teeny-request@^7.0.0:
stream-events "^1.0.5"
uuid "^8.0.0"

ts-node@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"

typedarray-to-buffer@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080"
Expand All @@ -942,6 +988,11 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.3.tgz#153bbd468ef07725c1df9c77e8b453f8d36abba5"
integrity sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==

unique-string@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-2.0.0.tgz#39c6451f81afb2749de2b233e3f7c5e8843bd89d"
Expand Down Expand Up @@ -1034,3 +1085,8 @@ yargs@^15.3.1:
which-module "^2.0.0"
y18n "^4.0.0"
yargs-parser "^18.1.2"

[email protected]:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==