-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from guardian/api-ffmpeg
Calculate duration of input files in order to decide which engine to use
- Loading branch information
Showing
15 changed files
with
257 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# ffmpeg lambda layer | ||
|
||
Ffmpeg (and ffprobe) are very useful tools for working with media files. In order to make use of them on AWS lambda, | ||
they need to be provided as a 'lambda layer' (essentially a zip file with the binaries in it). | ||
|
||
This folder contains a script to generate the zip file and push it to S3. If you are publishing a new version of the layer | ||
(e.g. to pull in some updates to ffmpeg) then you will need to run this script, and then update the FFMpegLayerZipKey | ||
parameter on the transcription-service stack. | ||
|
||
It probably would be possible to automate this process, but as we don't expect many updates to ffmpeg (I imagine it will | ||
only be necessary when we want to support new media codecs etc), I haven't bothered. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env bash | ||
|
||
set +x | ||
set -e | ||
|
||
echo "" | ||
echo "Downloading ffmpeg release and bundling for lambda layer..." | ||
echo "" | ||
|
||
WORKING_DIRECTORY="ffmpeg-layer-build" | ||
mkdir -p $WORKING_DIRECTORY | ||
|
||
pushd $WORKING_DIRECTORY | ||
curl -0L https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz > ffmpeg-release-amd64-static.tar.xz | ||
curl -0L https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz.md5 > ffmpeg-release-amd64-static.tar.xz.md5 | ||
md5sum -c ffmpeg-release-amd64-static.tar.xz.md5 | ||
|
||
tar -xf ffmpeg-release-amd64-static.tar.xz | ||
|
||
mkdir -p ffmpeg/bin | ||
mkdir -p ffprobe/bin | ||
|
||
mkdir -p bin | ||
|
||
cp ffmpeg-7.0.2-amd64-static/ffmpeg bin/ | ||
cp ffmpeg-7.0.2-amd64-static/ffprobe bin/ | ||
|
||
zip -r ffmpeg_x86_64.zip bin | ||
|
||
HASH=$(md5sum ffmpeg_x86_64.zip | cut -d ' ' -f 1) | ||
NAME_WITH_HASH="ffmpeg_x86_64-$HASH.zip" | ||
mv ffmpeg_x86_64.zip $NAME_WITH_HASH | ||
|
||
aws s3 cp ffmpeg_x86_64-$HASH.zip "s3://transcription-service-lambda-layers/${NAME_WITH_HASH}" | ||
popd | ||
rm -rf $WORKING_DIRECTORY | ||
|
||
echo "" | ||
echo "Layer zip key: ${NAME_WITH_HASH} - to use this layer you will need to update the FFMpegLayerZipKey parameter in the transcription-service stack" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { exec } from 'node:child_process'; | ||
import { logger } from '@guardian/transcription-service-backend-common'; | ||
import { promisify } from 'node:util'; | ||
|
||
const execPromise = promisify(exec); | ||
|
||
export const getFileDuration = async ( | ||
filePath: string, | ||
): Promise<number | undefined> => { | ||
const command = `ffprobe -i ${filePath} -show_entries format=duration -v quiet -of csv="p=0"`; | ||
try { | ||
const { stdout, stderr } = await execPromise(command); | ||
if (stderr) { | ||
logger.error(`ffprobe stderr: `, stderr); | ||
} | ||
return parseFloat(stdout); | ||
} catch (error) { | ||
logger.error(`Error during ffprobe file duration detection`, error); | ||
return undefined; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { GuStackProps } from '@guardian/cdk/lib/constructs/core'; | ||
import { GuStack } from '@guardian/cdk/lib/constructs/core'; | ||
import { GuS3Bucket } from '@guardian/cdk/lib/constructs/s3'; | ||
import type { App } from 'aws-cdk-lib'; | ||
import { CfnOutput } from 'aws-cdk-lib'; | ||
import { StringParameter } from 'aws-cdk-lib/aws-ssm'; | ||
|
||
export class TranscriptionServiceUniversalInfra extends GuStack { | ||
constructor(scope: App, id: string, props: GuStackProps) { | ||
super(scope, id, props); | ||
|
||
const layerBucket = new GuS3Bucket(this, 'LayerBucket', { | ||
bucketName: 'transcription-service-lambda-layers', | ||
app: 'transcription-service-universal-infra', | ||
}); | ||
|
||
new StringParameter(this, 'ExportFunctionName', { | ||
parameterName: `/investigations/transcription-service/lambdaLayerBucketArn`, | ||
stringValue: layerBucket.bucketArn, | ||
}); | ||
|
||
new CfnOutput(this, 'LayerBucket', { | ||
value: layerBucket.bucketArn, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.