As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.
Transcrevendo com os SDKs AWS
Você pode usar SDKs para transcrições em lote e de streaming. Se você estiver transcrevendo um arquivo de mídia localizado em um Amazon S3 bucket, você está realizando uma transcrição em lote. Se estiver transcrevendo um streaming de dados de áudio em tempo real, isso significa que você está realizando uma transcrição de streaming.
Para obter uma lista das linguagens de programação com as quais você pode usar Amazon Transcribe, consulteLinguagens de programação compatíveis. Observe que as transcrições de streaming não são compatíveis com todos os AWS SDKs. Para ver os formatos de mídia aceitos e outros requisitos e restrições de mídia, consulte Entrada e saída de dados.
Para obter mais informações sobre todos os AWS SDKs e ferramentas de criação disponíveis, consulte Ferramentas para desenvolver. AWS
Você pode criar transcrições em lote usando o URI de um arquivo de mídia localizado em um bucket do Amazon S3
. Se você não tiver certeza de como criar um Amazon S3 bucket ou fazer upload do seu arquivo, consulte Criar seu primeiro bucket do S3 e fazer upload de um objeto no seu bucket.
- Java
-
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.transcribe.TranscribeClient;
import software.amazon.awssdk.services.transcribe.model.*;
import software.amazon.awssdk.services.transcribestreaming.model.LanguageCode;
public class TranscribeDemoApp {
private static final Region REGION = Region.US_WEST_2
;
private static TranscribeClient client;
public static void main(String args[]) {
client = TranscribeClient.builder()
.credentialsProvider(getCredentials())
.region(REGION)
.build();
String transcriptionJobName = "my-first-transcription-job
";
String mediaType = "flac
"; // can be other types
Media myMedia = Media.builder()
.mediaFileUri("s3://DOC-EXAMPLE-BUCKET
/my-input-files
/my-media-file
.flac
")
.build();
String outputS3BucketName = "s3://DOC-EXAMPLE-BUCKET
";
// Create the transcription job request
StartTranscriptionJobRequest request = StartTranscriptionJobRequest.builder()
.transcriptionJobName(transcriptionJobName)
.languageCode(LanguageCode.EN_US
.toString())
.mediaSampleRateHertz(16000
)
.mediaFormat(mediaType)
.media(myMedia)
.outputBucketName(outputS3BucketName)
.build();
// send the request to start the transcription job
StartTranscriptionJobResponse startJobResponse = client.startTranscriptionJob(request);
System.out.println("Created the transcription job");
System.out.println(startJobResponse.transcriptionJob());
// Create the get job request
GetTranscriptionJobRequest getJobRequest = GetTranscriptionJobRequest.builder()
.transcriptionJobName(transcriptionJobName)
.build();
// send the request to get the transcription job including the job status
GetTranscriptionJobResponse getJobResponse = client.getTranscriptionJob(getJobRequest);
System.out.println("Get the transcription job request");
System.out.println(getJobResponse.transcriptionJob());
}
private static AwsCredentialsProvider getCredentials() {
return DefaultCredentialsProvider.create();
}
}
- JavaScript
-
const { TranscribeClient, StartTranscriptionJobCommand } = require("@aws-sdk/client-transcribe"); // CommonJS import
const region = "us-west-2
";
const credentials = {
"accessKeyId": "",
"secretAccessKey": "",
};
const input = {
TranscriptionJobName: "my-first-transcription-job
",
LanguageCode: "en-US
",
Media: {
MediaFileUri: "s3://DOC-EXAMPLE-BUCKET
/my-input-files
/my-media-file
.flac
"
},
OutputBucketName: "DOC-EXAMPLE-BUCKET
",
};
async function startTranscriptionRequest() {
const transcribeConfig = {
region,
credentials
};
const transcribeClient = new TranscribeClient(transcribeConfig);
const transcribeCommand = new StartTranscriptionJobCommand(input);
try {
const transcribeResponse = await transcribeClient.send(transcribeCommand);
console.log("Transcription job created, the details:");
console.log(transcribeResponse.TranscriptionJob);
} catch(err) {
console.log(err);
}
}
startTranscriptionRequest();
- Python
-
import time
import boto3
def transcribe_file(job_name, file_uri, transcribe_client):
transcribe_client.start_transcription_job(
TranscriptionJobName = job_name,
Media = {
'MediaFileUri': file_uri
},
MediaFormat = 'flac
',
LanguageCode = 'en-US
'
)
max_tries = 60
while max_tries > 0:
max_tries -= 1
job = transcribe_client.get_transcription_job(TranscriptionJobName = job_name)
job_status = job['TranscriptionJob']['TranscriptionJobStatus']
if job_status in ['COMPLETED', 'FAILED']:
print(f"Job {job_name} is {job_status}.")
if job_status == 'COMPLETED':
print(
f"Download the transcript from\n"
f"\t{job['TranscriptionJob']['Transcript']['TranscriptFileUri']}.")
break
else:
print(f"Waiting for {job_name}. Current status is {job_status}.")
time.sleep(10)
def main():
transcribe_client = boto3.client('transcribe', region_name = 'us-west-2')
file_uri = 's3://DOC-EXAMPLE-BUCKET
/my-input-files
/my-media-file
.flac
'
transcribe_file('Example-job', file_uri, transcribe_client)
if __name__ == '__main__':
main()
É possível criar transcrições de streaming usando um arquivo de mídia transmitido ou um streaming de mídia ao vivo.
Observe que o padrão não AWS SDK for Python (Boto3) é compatível com Amazon Transcribe streaming. Para iniciar uma transcrição de streaming usando Python, use esse SDK assíncrono do Python para. Amazon Transcribe
- Java
-
O exemplo a seguir é um programa Java que transcreve áudio de streaming.
Para executar esse exemplo, observe o seguinte:
Você deve usar o AWS SDK para Java 2.x.
Os clientes devem usar o Java 1.8 para serem compatíveis com o AWS SDK para Java 2.x.
A taxa de amostragem especificada deve corresponder à taxa de amostragem real do fluxo de áudio.
Veja também: Tente novamente o cliente para Amazon Transcribe streaming (Java SDK). Esse código gerencia a conexão com o Amazon Transcribe e tenta enviar dados novamente quando há erros na conexão. Por exemplo, se houver um erro temporário na rede, esse cliente reenviará a solicitação que falhou.
public class TranscribeStreamingDemoApp {
private static final Region REGION = Region.US_WEST_2
;
private static TranscribeStreamingAsyncClient client;
public static void main(String args[]) throws URISyntaxException, ExecutionException, InterruptedException, LineUnavailableException {
client = TranscribeStreamingAsyncClient.builder()
.credentialsProvider(getCredentials())
.region(REGION)
.build();
CompletableFuture<Void> result = client.startStreamTranscription(getRequest(16_000),
new AudioStreamPublisher(getStreamFromMic()),
getResponseHandler());
result.get();
client.close();
}
private static InputStream getStreamFromMic() throws LineUnavailableException {
// Signed PCM AudioFormat with 16,000 Hz, 16 bit sample size, mono
int sampleRate = 16000
;
AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
InputStream audioStream = new AudioInputStream(line);
return audioStream;
}
private static AwsCredentialsProvider getCredentials() {
return DefaultCredentialsProvider.create();
}
private static StartStreamTranscriptionRequest getRequest(Integer mediaSampleRateHertz) {
return StartStreamTranscriptionRequest.builder()
.languageCode(LanguageCode.EN_US
.toString())
.mediaEncoding(MediaEncoding.PCM
)
.mediaSampleRateHertz(mediaSampleRateHertz)
.build();
}
private static StartStreamTranscriptionResponseHandler getResponseHandler() {
return StartStreamTranscriptionResponseHandler.builder()
.onResponse(r -> {
System.out.println("Received Initial response");
})
.onError(e -> {
System.out.println(e.getMessage());
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
System.out.println("Error Occurred: " + sw.toString());
})
.onComplete(() -> {
System.out.println("=== All records stream successfully ===");
})
.subscriber(event -> {
List<Result> results = ((TranscriptEvent) event).transcript().results();
if (results.size() > 0) {
if (!results.get(0).alternatives().get(0).transcript().isEmpty()) {
System.out.println(results.get(0).alternatives().get(0).transcript());
}
}
})
.build();
}
private InputStream getStreamFromFile(String myMediaFileName
) {
try {
File inputFile = new File(getClass().getClassLoader().getResource(myMediaFileName).getFile());
InputStream audioStream = new FileInputStream(inputFile);
return audioStream;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
private static class AudioStreamPublisher implements Publisher<AudioStream> {
private final InputStream inputStream;
private static Subscription currentSubscription;
private AudioStreamPublisher(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public void subscribe(Subscriber<? super AudioStream> s) {
if (this.currentSubscription == null) {
this.currentSubscription = new SubscriptionImpl(s, inputStream);
} else {
this.currentSubscription.cancel();
this.currentSubscription = new SubscriptionImpl(s, inputStream);
}
s.onSubscribe(currentSubscription);
}
}
public static class SubscriptionImpl implements Subscription {
private static final int CHUNK_SIZE_IN_BYTES = 1024 * 1;
private final Subscriber<? super AudioStream> subscriber;
private final InputStream inputStream;
private ExecutorService executor = Executors.newFixedThreadPool(1);
private AtomicLong demand = new AtomicLong(0);
SubscriptionImpl(Subscriber<? super AudioStream> s, InputStream inputStream) {
this.subscriber = s;
this.inputStream = inputStream;
}
@Override
public void request(long n) {
if (n <= 0) {
subscriber.onError(new IllegalArgumentException("Demand must be positive"));
}
demand.getAndAdd(n);
executor.submit(() -> {
try {
do {
ByteBuffer audioBuffer = getNextEvent();
if (audioBuffer.remaining() > 0) {
AudioEvent audioEvent = audioEventFromBuffer(audioBuffer);
subscriber.onNext(audioEvent);
} else {
subscriber.onComplete();
break;
}
} while (demand.decrementAndGet() > 0);
} catch (Exception e) {
subscriber.onError(e);
}
});
}
@Override
public void cancel() {
executor.shutdown();
}
private ByteBuffer getNextEvent() {
ByteBuffer audioBuffer = null;
byte[] audioBytes = new byte[CHUNK_SIZE_IN_BYTES];
int len = 0;
try {
len = inputStream.read(audioBytes);
if (len <= 0) {
audioBuffer = ByteBuffer.allocate(0);
} else {
audioBuffer = ByteBuffer.wrap(audioBytes, 0, len);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return audioBuffer;
}
private AudioEvent audioEventFromBuffer(ByteBuffer bb) {
return AudioEvent.builder()
.audioChunk(SdkBytes.fromByteBuffer(bb))
.build();
}
}
}
- JavaScript
-
const {
TranscribeStreamingClient,
StartStreamTranscriptionCommand,
} = require("@aws-sdk/client-transcribe-streaming");
const { createReadStream } = require("fs");
const { join } = require("path");
const audio = createReadStream(join(__dirname, "my-media-file
.flac
"), { highWaterMark: 1024 * 16});
const LanguageCode = "en-US
";
const MediaEncoding = "pcm
";
const MediaSampleRateHertz = "16000
";
const credentials = {
"accessKeyId": "",
"secretAccessKey": "",
};
async function startRequest() {
const client = new TranscribeStreamingClient({
region: "us-west-2
",
credentials
});
const params = {
LanguageCode,
MediaEncoding,
MediaSampleRateHertz,
AudioStream: (async function* () {
for await (const chunk of audio) {
yield {AudioEvent: {AudioChunk: chunk}};
}
})(),
};
const command = new StartStreamTranscriptionCommand(params);
// Send transcription request
const response = await client.send(command);
// Start to print response
try {
for await (const event of response.TranscriptResultStream) {
console.log(JSON.stringify(event));
}
} catch(err) {
console.log("error")
console.log(err)
}
}
startRequest();
- Python
-
O exemplo a seguir é um programa Python que transcreve áudio de streaming.
Para executar esse exemplo, observe o seguinte:
import asyncio
# This example uses aiofile for asynchronous file reads.
# It's not a dependency of the project but can be installed
# with `pip install aiofile`.
import aiofile
from amazon_transcribe.client import TranscribeStreamingClient
from amazon_transcribe.handlers import TranscriptResultStreamHandler
from amazon_transcribe.model import TranscriptEvent
"""
Here's an example of a custom event handler you can extend to
process the returned transcription results as needed. This
handler will simply print the text out to your interpreter.
"""
class MyEventHandler(TranscriptResultStreamHandler):
async def handle_transcript_event(self, transcript_event: TranscriptEvent):
# This handler can be implemented to handle transcriptions as needed.
# Here's an example to get started.
results = transcript_event.transcript.results
for result in results:
for alt in result.alternatives:
print(alt.transcript)
async def basic_transcribe():
# Set up our client with your chosen Region
client = TranscribeStreamingClient(region = "us-west-2
")
# Start transcription to generate async stream
stream = await client.start_stream_transcription(
language_code = "en-US
",
media_sample_rate_hz = 16000
,
media_encoding = "pcm
",
)
async def write_chunks():
# NOTE: For pre-recorded files longer than 5 minutes, the sent audio
# chunks should be rate limited to match the real-time bitrate of the
# audio stream to avoid signing issues.
async with aiofile.AIOFile('filepath
/my-media-file
.flac
', 'rb') as afp:
reader = aiofile.Reader(afp, chunk_size = 1024 * 16)
async for chunk in reader:
await stream.input_stream.send_audio_event(audio_chunk = chunk)
await stream.input_stream.end_stream()
# Instantiate our handler and start processing events
handler = MyEventHandler(stream.output_stream)
await asyncio.gather(write_chunks(), handler.handle_events())
loop = asyncio.get_event_loop()
loop.run_until_complete(basic_transcribe())
loop.close()
- C++
-
Consulte no capítulo “Exemplos de código” o exemplo do SDK de streaming para C++.
AWS kits de desenvolvimento de software (SDKs) estão disponíveis para muitas linguagens de programação populares. Cada SDK fornece uma API, exemplos de código e documentação que facilitam a criação de aplicações em seu idioma preferido pelos desenvolvedores.
Para obter exemplos específicos deste serviço, consulte Exemplos de código para o Amazon Transcribe usando AWS SDKs.
Exemplo de disponibilidade
Você não consegue encontrar o que precisa? Solicite um código de exemplo no link Fornecer feedback na parte inferior desta página.