Skip to content

Output video is significantly sped up #21

Open
@harterc3

Description

@harterc3

For the project I'm working on, I'm mainly just taking mp4s recorded by users in a web browser and trying to transcode them to be manageable sizes as well as playable in all device/browser combos. I was using AWS MediaConvert for this but later found out that mobile Safari records video in a "fragmented" mp4 which is not supported by MediaConvert so I'm turning to replacing MC with an ffmpeg layer.

I've deployed the ffmpeg lambda layer from the SAR and copied most of the code from the example but for some reason all the output videos are significantly sped up. When I run the same commands with my local ffmpeg, everything works fine. Any help is appreciated!

export const onVideoUpload: Handler = Sentry.AWSLambda.wrapHandler(async (event: S3Event, context: Context) => {
  const EXTENSION = '.mp4',
	  MIME_TYPE =  'video/mp4';

  const eventRecord = event.Records && event.Records[0],
		inputBucket = eventRecord.s3.bucket.name,
		key = eventRecord.s3.object.key,
		id = context.awsRequestId,
		resultKey = key.replace(/\.[^.]+$/, EXTENSION),
		workdir = os.tmpdir(),
		inputFile = path.join(workdir,  id + path.extname(key)),
		outputFile = path.join(workdir, 'out_' + id + EXTENSION);

	console.log('converting', inputBucket, key, 'using', inputFile);
  // ffmpeg -i 2254.webm -c copy output.webm
  // ffmpeg -i input.mp4 -b:v 1M -b:a 192k output.avi
	return downloadFileFromS3(inputBucket, key, inputFile)
		.then(() => childProcess(
			'/opt/bin/ffmpeg',
			['-i', inputFile, '-b:v', '1M', '-b:a', '192k', '-nostdin', outputFile],
			{env: process.env, cwd: workdir}
		))
		.then(() => uploadFileToS3(process.env['DestinationBucket']!, resultKey, outputFile, MIME_TYPE));
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions