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

Output video is significantly sped up #21

Open
harterc3 opened this issue Aug 10, 2021 · 2 comments
Open

Output video is significantly sped up #21

harterc3 opened this issue Aug 10, 2021 · 2 comments

Comments

@harterc3
Copy link

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));
});
@gojko
Copy link
Contributor

gojko commented Aug 11, 2021

maybe try without the explicit bitrate (-b:v)?

@harterc3
Copy link
Author

Updated the function to

return downloadFileFromS3(inputBucket, key, inputFile)
  .then(() => childProcess(
    '/opt/bin/ffmpeg',
    ['-i', inputFile, '-nostdin', outputFile],
    {env: process.env, cwd: workdir}
  ))
  .then(() => uploadFileToS3(process.env['DestinationBucket']!, resultKey, outputFile, MIME_TYPE));

and I still have the same result:
https://user-images.githubusercontent.com/3442109/129050132-b7da97c1-217a-4631-9919-68606fd9c6d5.mp4

Original video:
https://user-images.githubusercontent.com/3442109/129050443-78463553-4fda-43a7-81ce-6bf04aa36e1d.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants