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

Add media download service (part 1) #104

Merged
merged 14 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 48 additions & 0 deletions packages/backend-common/src/process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { spawn } from 'child_process';
import { logger } from './logging';
export interface ProcessResult {
code?: number;
stdout: string;
stderr: string;
}

export const runSpawnCommand = (
processName: string,
cmd: string,
args: ReadonlyArray<string>,
): Promise<ProcessResult> => {
return new Promise((resolve, reject) => {
const cp = spawn(cmd, args);
const stdout: string[] = [];
const stderr: string[] = [];
cp.stdout.on('data', (data) => {
stdout.push(data.toString());
});

cp.stderr.on('data', (data) => {
stderr.push(data.toString());
});

cp.on('error', (e) => {
stderr.push(e.toString());
});

cp.on('close', (code) => {
const result = {
stdout: stdout.join(''),
stderr: stderr.join(''),
code: code || undefined,
};
logger.info('Ignoring stdout to avoid logging sensitive data');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it important not to log stdout when running yt-dlp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I've updated this ecff3ce

logger.info(`process ${processName} stderr: ${result.stderr}`);
if (code === 0) {
resolve(result);
} else {
logger.error(
`process ${processName} failed with code ${result.code} due to: ${result.stderr}`,
);
reject(result);
}
});
});
};
1 change: 0 additions & 1 deletion packages/media-download/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"dependencies": {
"@guardian/transcription-service-common": "1.0.0",
"@guardian/transcription-service-backend-common": "1.0.0",
"zx": "8.1.8",
"@aws-sdk/lib-storage": "3.658.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/media-download/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const requestTranscription = async (
};

const main = async () => {
console.log('Starting media download service');
const config = await getConfig();

const s3Client = new S3Client({ region: config.aws.region });
Expand Down
12 changes: 9 additions & 3 deletions packages/media-download/src/yt-dlp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs';
import { $ } from 'zx';
import { runSpawnCommand } from '@guardian/transcription-service-backend-common/src/process';

export type MediaMetadata = {
title: string;
Expand All @@ -24,8 +24,14 @@ export const downloadMedia = async (
destinationDirectoryPath: string,
id: string,
) => {
const output =
await $`yt-dlp --write-info-json --no-clean-info-json --newline -o "${destinationDirectoryPath}/${id}" ${url}`;
const output = await runSpawnCommand('downloadMedia', 'yt-dlp', [
'--write-info-json',
'--no-clean-info-json',
'--newline',
'-o',
`${destinationDirectoryPath}/${id}`,
url,
]);
console.log(output);
const metadata = extractInfoJson(
`${destinationDirectoryPath}/${id}.info.json`,
Expand Down
49 changes: 1 addition & 48 deletions packages/worker/src/transcribe.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { spawn } from 'child_process';
import path from 'path';
import { readFile } from '@guardian/transcription-service-backend-common';
import { logger } from '@guardian/transcription-service-backend-common';
import {
LanguageCode,
languageCodes,
} from '@guardian/transcription-service-common';

interface ProcessResult {
code?: number;
stdout: string;
stderr: string;
}
import { runSpawnCommand } from '@guardian/transcription-service-backend-common/src/process';

interface FfmpegResult {
wavPath: string;
Expand Down Expand Up @@ -48,47 +42,6 @@ export type WhisperBaseParams = {

const CONTAINER_FOLDER = '/input';

const runSpawnCommand = (
processName: string,
cmd: string,
args: ReadonlyArray<string>,
): Promise<ProcessResult> => {
return new Promise((resolve, reject) => {
const cp = spawn(cmd, args);
const stdout: string[] = [];
const stderr: string[] = [];
cp.stdout.on('data', (data) => {
stdout.push(data.toString());
});

cp.stderr.on('data', (data) => {
stderr.push(data.toString());
});

cp.on('error', (e) => {
stderr.push(e.toString());
});

cp.on('close', (code) => {
const result = {
stdout: stdout.join(''),
stderr: stderr.join(''),
code: code || undefined,
};
logger.info('Ignoring stdout to avoid logging sensitive data');
logger.info(`process ${processName} stderr: ${result.stderr}`);
if (code === 0) {
resolve(result);
} else {
logger.error(
`process ${processName} failed with code ${result.code} due to: ${result.stderr}`,
);
reject(result);
}
});
});
};

export const getOrCreateContainer = async (
tempDir: string,
): Promise<string> => {
Expand Down
Loading