1- import { TranscribeClient , StartTranscriptionJobCommand } from " @aws-sdk/client-transcribe" ;
2- import { randomUUID } from 'crypto ' ;
1+ import { S3Client , GetObjectCommand } from ' @aws-sdk/client-s3' ;
2+ import { TranscribeClient , StartTranscriptionJobCommand } from '@aws-sdk/client-transcribe ' ;
33
4- export const lambda_handler = async ( event , context ) => {
5- try {
6- const requestBody = JSON . parse ( event . body ) ;
7- const s3_url = requestBody . audio_url ;
8-
9- const transcribe = new TranscribeClient ( ) ;
10- const job_name = `transcribe-${ randomUUID ( ) } ` ;
11-
12- const command = new StartTranscriptionJobCommand ( {
13- TranscriptionJobName : job_name ,
14- Media : { MediaFileUri : s3_url } ,
15- MediaFormat : 'mp3' , // Adjust based on your needs
16- LanguageCode : 'en-US' // Adjust based on your needs
17- } ) ;
18-
19- await transcribe . send ( command ) ;
20-
21- return {
22- statusCode : 200 ,
23- body : JSON . stringify ( {
24- job_name : job_name ,
25- status : 'IN_PROGRESS'
26- } )
27- } ;
28-
29- } catch ( e ) {
30- return {
31- statusCode : 500 ,
32- body : JSON . stringify ( {
33- error : e . toString ( )
34- } )
35- } ;
4+ const s3Client = new S3Client ( { region : process . env . REGION } ) ;
5+ const transcribeClient = new TranscribeClient ( { region : process . env . REGION } ) ;
6+
7+ export const handler = async ( event ) => {
8+ try {
9+ // Get the S3 bucket and key from the event
10+ const bucket = event . Records [ 0 ] . s3 . bucket . name ;
11+ const key = decodeURIComponent ( event . Records [ 0 ] . s3 . object . key . replace ( / \+ / g, ' ' ) ) ;
12+
13+ console . log ( `Processing file: s3://${ bucket } /${ key } ` ) ;
14+
15+ // Extract file name and extension for the transcription job
16+ const fileName = key . split ( '/' ) . pop ( ) ;
17+ const fileNameWithoutExt = fileName . substring ( 0 , fileName . lastIndexOf ( '.' ) ) || fileName ;
18+ const fileExt = fileName . substring ( fileName . lastIndexOf ( '.' ) + 1 ) . toLowerCase ( ) ;
19+
20+ // Determine media format based on file extension
21+ let mediaFormat ;
22+ switch ( fileExt ) {
23+ case 'mp3' :
24+ mediaFormat = 'mp3' ;
25+ break ;
26+ case 'wav' :
27+ mediaFormat = 'wav' ;
28+ break ;
29+ case 'flac' :
30+ mediaFormat = 'flac' ;
31+ break ;
32+ default :
33+ throw new Error ( `Unsupported file format: ${ fileExt } ` ) ;
3634 }
37- } ;
35+
36+ const transcriptionJobName = `${ fileNameWithoutExt } -${ Date . now ( ) } ` ;
37+
38+ const mediaFileUri = `s3://${ bucket } /${ key } ` ;
39+
40+ const startTranscriptionParams = {
41+ TranscriptionJobName : transcriptionJobName ,
42+ LanguageCode : 'en-US' ,
43+ MediaFormat : mediaFormat ,
44+ Media : {
45+ MediaFileUri : mediaFileUri
46+ } ,
47+ OutputBucketName : bucket ,
48+ OutputKey : `transcriptions/${ fileNameWithoutExt } .json`
49+ } ;
50+
51+ const transcriptionCommand = new StartTranscriptionJobCommand ( startTranscriptionParams ) ;
52+ const transcriptionResponse = await transcribeClient . send ( transcriptionCommand ) ;
53+
54+ console . log ( `Started transcription job: ${ transcriptionJobName } ` ) ;
55+ console . log ( `Transcription job response: ${ JSON . stringify ( transcriptionResponse ) } ` ) ;
56+
57+ return {
58+ statusCode : 200 ,
59+ body : JSON . stringify ( {
60+ message : 'Transcription job started successfully' ,
61+ jobName : transcriptionJobName ,
62+ jobStatus : transcriptionResponse . TranscriptionJob . TranscriptionJobStatus
63+ } )
64+ } ;
65+ } catch ( error ) {
66+ console . error ( 'Error processing the file:' , error ) ;
67+
68+ return {
69+ statusCode : 500 ,
70+ body : JSON . stringify ( {
71+ message : 'Error starting transcription job' ,
72+ error : error . message
73+ } )
74+ } ;
75+ }
76+ } ;
0 commit comments