Skip to content
Open
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
27 changes: 27 additions & 0 deletions speech-to-text/functions/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {getTranscodedStoragePath} from '../src/util';

describe('getTranscodedStoragePath', () => {
it('uses the file basename when no output path is configured', () => {
expect(getTranscodedStoragePath('myDirectory/audio.wav')).toBe(
'audio.wav.wav'
);
});

it('writes nested input files under the configured output path once', () => {
expect(
getTranscodedStoragePath('myDirectory/audio.wav', 'transcriptions')
).toBe('transcriptions/audio.wav.wav');
});

it('normalizes leading and trailing slashes in the configured output path', () => {
expect(
getTranscodedStoragePath('nested/audio.wav', '/transcriptions/')
).toBe('transcriptions/audio.wav.wav');
});

it('does not duplicate the source directory when output path matches it', () => {
expect(
getTranscodedStoragePath('myDirectory/audio.wav', 'myDirectory')
).toBe('myDirectory/audio.wav.wav');
});
});
16 changes: 12 additions & 4 deletions speech-to-text/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import * as mkdirp from 'mkdirp';
import {FieldValue} from 'firebase-admin/firestore';

import * as logs from './logs';
import {publishFailureEvent, errorFromAny, publishCompleteEvent} from './util';
import {
publishFailureEvent,
errorFromAny,
publishCompleteEvent,
getTranscodedStoragePath,
} from './util';
import {
transcodeToLinear16,
transcribeAndUpload,
Expand Down Expand Up @@ -126,11 +131,14 @@ export const transcribeAudio = functions.storage
message: 'Transcoding audio file.',
});

const transcodedStoragePath = getTranscodedStoragePath(
filePath,
config.outputStoragePath
);

const transcodedUploadResult = await uploadTranscodedFile({
localPath: transcodeResult.outputPath,
storagePath: config.outputStoragePath
? `${config.outputStoragePath}${transcodeResult.outputPath}`
: transcodeResult.outputPath.slice(1),
storagePath: transcodedStoragePath,
bucket: bucket,
});

Expand Down
13 changes: 13 additions & 0 deletions speech-to-text/functions/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
import * as util from 'util';
import * as ffmpeg from 'fluent-ffmpeg';
import * as path from 'path';
import {Failure, TranscribeAudioSuccess} from './types';
import {Channel} from 'firebase-admin/eventarc';
import {google} from '@google-cloud/speech/build/protos/protos';
Expand Down Expand Up @@ -89,6 +90,18 @@ export const probePromise = util.promisify<string, ffmpeg.FfprobeData>(
ffmpeg.ffprobe
);

export function getTranscodedStoragePath(
objectName: string,
outputStoragePath?: string
): string {
const fileName = `${path.posix.basename(objectName)}.wav`;
const normalizedOutputPath = outputStoragePath?.replace(/^\/+|\/+$/g, '');

return normalizedOutputPath
? `${normalizedOutputPath}/${fileName}`
: fileName;
}

export async function publishFailureEvent(
eventChannel: Channel,
{...contents}: Failure,
Expand Down
Loading