Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Trascrizione con gli SDK AWS
È possibile utilizzare gli SDK sia per trascrizioni in batch e che in streaming. Se stai trascrivendo un file multimediale che si trova in un Amazon S3 bucket, stai eseguendo una trascrizione in batch. Se stai trascrivendo un flusso audio in tempo reale, stai eseguendo una trascrizione in streaming.
Per un elenco dei linguaggi di programmazione utilizzabili, consulta. Amazon TranscribeLinguaggi di programmazione compatibili Tieni presente che le trascrizioni in streaming non sono supportate con tutti gli AWS SDK. Per visualizzare i formati multimediali supportati e altri requisiti e vincoli multimediali, consulta Input e output di dati.
Per ulteriori informazioni su tutti gli AWS SDK e gli strumenti di creazione disponibili, consulta Tools to Build on. AWS
È possibile creare trascrizioni in batch utilizzando l'URI di un file multimediale contenuto in un bucket Amazon S3
. Se non sei sicuro di come creare un Amazon S3 bucket o caricare il tuo file, consulta Creare il tuo primo bucket S3 e Caricare un oggetto nel tuo 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()
Puoi creare trascrizioni in streaming utilizzando un file multimediale in streaming o un flusso multimediale in diretta.
Tieni presente che lo standard non AWS SDK for Python (Boto3) è supportato per lo streaming. Amazon Transcribe Per avviare una trascrizione in streaming usando Python, usa questo SDK Python asincrono per. Amazon Transcribe
- Java
-
L'esempio seguente è un programma Java che trascrive l'audio in streaming.
Per eseguire questo esempio, tieni presente quanto segue:
È necessario utilizzare SDK AWS per Java 2.x.
I client devono utilizzare Java 1.8 per essere compatibili con SDK AWS per Java 2.x.
La frequenza di campionamento specificata deve corrispondere alla frequenza di campionamento effettiva del flusso audio.
Vedi anche: Riprova il client per lo streaming (Java SDK). Amazon Transcribe Questo codice gestisce la connessione ad Amazon Transcribe e i nuovi tentativi di invio di dati quando si verificano errori di connessione. Ad esempio, se si verifica un errore temporaneo sulla rete, questo client invia di nuovo la richiesta non riuscita.
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
-
L'esempio seguente è un programma Python che trascrive l'audio in streaming.
Per eseguire questo esempio, tieni presente quanto segue:
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++
-
Fai riferimento al capitolo sugli esempi di codice per l'esempio di streaming in C++ SDK.
AWS I kit di sviluppo software (SDK) sono disponibili per molti linguaggi di programmazione più diffusi. Ogni SDK fornisce un'API, esempi di codice, e documentazione che facilitano agli sviluppatori la creazione di applicazioni nel loro linguaggio preferito.
Per esempi specifici del servizio, consulta Esempi di codice per Amazon Transcribe tramite SDK AWS.
Non riesci a trovare quello che ti serve? Richiedi un esempio di codice utilizzando il link Provide feedback (Fornisci un feedback) nella parte inferiore di questa pagina.