How do I put in the JSON of the Lambda event several objects with different extensions, such as mp4 or mp3?

0

Hi, I'm doing a project where the lambda takes files from the s3 bucket and sends it to Transcribe, but the Lambda is only taking mp3 type files, in the code when I put it in MediaFormat="mp3" or "mp4" in Lambda returns with the error "The media format you specified does not match the detected media format. Please check the media format and try your request again." My code is:

import json import urllib.parse from urllib.parse import unquote_plus import boto3 def lambda_handler(event, context):

record = event['Records'][0]

domenicoteste1 = record['s3']['bucket']['name']
s3object = record['s3']['object']['key']
file_obj = event["Records"][0]
object = unquote_plus(str(file_obj["s3"]["object"]["key"]))

s3Path = "s3://" + domenicoteste1 + "/" + object
jobName = domenicoteste1 + '-' + str(uuid.uuid4())

client = boto3.client('transcribe')

response = client.start_transcription_job(
    TranscriptionJobName=jobName,
    LanguageCode='pt-BR',
    MediaFormat="mp3" or "mp4",
    Media={

'MediaFileUri': s3Path }, OutputBucketName= "outputeteste1" )

return {
    'TranscriptionJobName': response['TranscriptionJob']['TranscriptionJobName']
}
profile picture
asked 2 years ago513 views
2 Answers
0

Hi!

According to the documentation https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/transcribe.html#TranscribeService.Client.start_transcription_job the mediaFormat has to be a string. In Python "OR" is a boolean operator. The result of "mp3" or "mp4" is the value "mp3". This is probably not your intended input for the function.

As the API documentation does not mention a way to specify several format, I suggest creating two different lambda function. One for mp3, the other for mp4.

Best, Matthäus

AWS
answered 2 years ago
0

Hi - Thanks for reaching out. The flag media-format is a string and can take possible values like mp3, mp4, wav, flac. Don't think so it will take something like MediaFormat="mp3" or "mp4".

One possible option is to

  1. Get the extension from eventRecord
  2. Check for valid extension type like if (!['mp3', 'mp4', 'wav', 'flac'].includes(extension))
  3. If valid then create a dynamic Media format like MediaFormat: extension

In this way, you can have a single lambda function that can be re-purposed for the supported extensions

Hope this helps.

profile pictureAWS
EXPERT
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions