Seleziona le tue preferenze relative ai cookie

Utilizziamo cookie essenziali e strumenti simili necessari per fornire il nostro sito e i nostri servizi. Utilizziamo i cookie prestazionali per raccogliere statistiche anonime in modo da poter capire come i clienti utilizzano il nostro sito e apportare miglioramenti. I cookie essenziali non possono essere disattivati, ma puoi fare clic su \"Personalizza\" o \"Rifiuta\" per rifiutare i cookie prestazionali.

Se sei d'accordo, AWS e le terze parti approvate utilizzeranno i cookie anche per fornire utili funzionalità del sito, ricordare le tue preferenze e visualizzare contenuti pertinenti, inclusa la pubblicità pertinente. Per continuare senza accettare questi cookie, fai clic su \"Continua\" o \"Rifiuta\". Per effettuare scelte più dettagliate o saperne di più, fai clic su \"Personalizza\".

Genera una trascrizione di un file utilizzando un SDK AWS - AWS Esempi di codice SDK

Sono disponibili altri esempi AWS SDK nel repository AWS Doc SDK Examples. GitHub

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à.

Sono disponibili altri esempi AWS SDK nel repository AWS Doc SDK Examples. GitHub

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à.

Genera una trascrizione di un file utilizzando un SDK AWS

I seguenti esempi di codice mostrano come generare una trascrizione di un file audio sorgente utilizzando lo streaming di Amazon Transcribe.

C++
SDK per C++
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

int main() { Aws::SDKOptions options; Aws::InitAPI(options); { //TODO(User): Set to the region of your AWS account. const Aws::String region = Aws::Region::US_WEST_2; //Load a profile that has been granted AmazonTranscribeFullAccess AWS managed permission policy. Aws::Client::ClientConfiguration config; #ifdef _WIN32 // ATTENTION: On Windows with the AWS C++ SDK, this example only runs if the SDK is built // with the curl library. // For more information, see the accompanying ReadMe. // For more information, see "Building the SDK for Windows with curl". // https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/setup-windows.html //TODO(User): Update to the location of your .crt file. config.caFile = "C:/curl/bin/curl-ca-bundle.crt"; #endif config.region = region; TranscribeStreamingServiceClient client(config); StartStreamTranscriptionHandler handler; handler.SetOnErrorCallback( [](const Aws::Client::AWSError<TranscribeStreamingServiceErrors> &error) { std::cerr << "ERROR: " + error.GetMessage() << std::endl; }); //SetTranscriptEventCallback called for every 'chunk' of file transcripted. // Partial results are returned in real time. handler.SetTranscriptEventCallback([](const TranscriptEvent &ev) { for (auto &&r: ev.GetTranscript().GetResults()) { if (r.GetIsPartial()) { std::cout << "[partial] "; } else { std::cout << "[Final] "; } for (auto &&alt: r.GetAlternatives()) { std::cout << alt.GetTranscript() << std::endl; } } }); StartStreamTranscriptionRequest request; request.SetMediaSampleRateHertz(SAMPLE_RATE); request.SetLanguageCode(LanguageCode::en_US); request.SetMediaEncoding( MediaEncoding::pcm); // wav and aiff files are PCM formats. request.SetEventStreamHandler(handler); auto OnStreamReady = [](AudioStream &stream) { Aws::FStream file(FILE_NAME, std::ios_base::in | std::ios_base::binary); if (!file.is_open()) { std::cerr << "Failed to open " << FILE_NAME << '\n'; } std::array<char, BUFFER_SIZE> buf; int i = 0; while (file) { file.read(&buf[0], buf.size()); if (!file) std::cout << "File: only " << file.gcount() << " could be read" << std::endl; Aws::Vector<unsigned char> bits{buf.begin(), buf.end()}; AudioEvent event(std::move(bits)); if (!stream) { std::cerr << "Failed to create a stream" << std::endl; break; } //The std::basic_istream::gcount() is used to count the characters in the given string. It returns //the number of characters extracted by the last read() operation. if (file.gcount() > 0) { if (!stream.WriteAudioEvent(event)) { std::cerr << "Failed to write an audio event" << std::endl; break; } } else { break; } std::this_thread::sleep_for(std::chrono::milliseconds( 25)); // Slow down because we are streaming from a file. } if (!stream.WriteAudioEvent( AudioEvent())) { // Per the spec, we have to send an empty event (an event without a payload) at the end. std::cerr << "Failed to send an empty frame" << std::endl; } else { std::cout << "Successfully sent the empty frame" << std::endl; } stream.flush(); stream.Close(); }; Aws::Utils::Threading::Semaphore signaling(0 /*initialCount*/, 1 /*maxCount*/); auto OnResponseCallback = [&signaling]( const TranscribeStreamingServiceClient * /*unused*/, const Model::StartStreamTranscriptionRequest & /*unused*/, const Model::StartStreamTranscriptionOutcome &outcome, const std::shared_ptr<const Aws::Client::AsyncCallerContext> & /*unused*/) { if (!outcome.IsSuccess()) { std::cerr << "Transcribe streaming error " << outcome.GetError().GetMessage() << std::endl; } signaling.Release(); }; std::cout << "Starting..." << std::endl; client.StartStreamTranscriptionAsync(request, OnStreamReady, OnResponseCallback, nullptr /*context*/); signaling.WaitOne(); // Prevent the application from exiting until we're done. std::cout << "Done" << std::endl; } Aws::ShutdownAPI(options); return 0; }
Java
SDK per Java 2.x
Nota

C'è altro su GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

/** * To run this AWS code example, ensure that you have set up your development * environment, including your AWS credentials. * * For information, see this documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class TranscribeStreamingDemoFile { private static final Region REGION = Region.US_EAST_1; private static TranscribeStreamingAsyncClient client; public static void main(String args[]) throws ExecutionException, InterruptedException { final String USAGE = "\n" + "Usage:\n" + " <file> \n\n" + "Where:\n" + " file - the location of a PCM file to transcribe. In this example, ensure the PCM file is 16 hertz (Hz). \n"; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String file = args[0]; client = TranscribeStreamingAsyncClient.builder() .region(REGION) .build(); CompletableFuture<Void> result = client.startStreamTranscription(getRequest(16_000), new AudioStreamPublisher(getStreamFromFile(file)), getResponseHandler()); result.get(); client.close(); } private static InputStream getStreamFromFile(String file) { try { File inputFile = new File(file); InputStream audioStream = new FileInputStream(inputFile); return audioStream; } catch (FileNotFoundException e) { throw new RuntimeException(e); } } private static StartStreamTranscriptionRequest getRequest(Integer mediaSampleRateHertz) { return StartStreamTranscriptionRequest.builder() .languageCode(LanguageCode.EN_US) .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 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(); } } }
Swift
SDK per Swift
Nota

C'è altro su GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Usa lo streaming di Amazon Transcribe per trascrivere la lingua parlata in un file audio.

/// An example that demonstrates how to watch an transcribe event stream to /// transcribe audio from a file to the console. import ArgumentParser import AWSClientRuntime import AWSTranscribeStreaming import Foundation /// Identify one of the media file formats supported by Amazon Transcribe. enum TranscribeFormat: String, ExpressibleByArgument { case ogg = "ogg" case pcm = "pcm" case flac = "flac" } // -MARK: - Async command line tool struct ExampleCommand: ParsableCommand { // -MARK: Command arguments @Flag(help: "Show partial results") var showPartial = false @Option(help: "Language code to transcribe into") var lang: String = "en-US" @Option(help: "Format of the source audio file") var format: TranscribeFormat @Option(help: "Sample rate of the source audio file in Hertz") var sampleRate: Int = 16000 @Option(help: "Path of the source audio file") var path: String @Option(help: "Name of the Amazon S3 Region to use (default: us-east-1)") var region = "us-east-1" static var configuration = CommandConfiguration( commandName: "tsevents", abstract: """ This example shows how to use event streaming with Amazon Transcribe. """, discussion: """ """ ) /// Create and return an Amazon Transcribe audio stream from the file /// specified in the arguments. /// /// - Throws: Errors from `TranscribeError`. /// /// - Returns: `AsyncThrowingStream<TranscribeStreamingClientTypes.AudioStream, Error>` func createAudioStream() async throws -> AsyncThrowingStream<TranscribeStreamingClientTypes.AudioStream, Error> { let fileURL: URL = URL(fileURLWithPath: path) let audioData = try Data(contentsOf: fileURL) // Properties defining the size of audio chunks and the total size of // the audio file in bytes. You should try to send chunks that last on // average 125 milliseconds. let chunkSizeInMilliseconds = 125.0 let chunkSize = Int(chunkSizeInMilliseconds / 1000.0 * Double(sampleRate) * 2.0) let audioDataSize = audioData.count // Create an audio stream from the source data. The stream's job is // to send the audio in chunks to Amazon Transcribe as // `AudioStream.audioevent` events. let audioStream = AsyncThrowingStream<TranscribeStreamingClientTypes.AudioStream, Error> { continuation in Task { var currentStart = 0 var currentEnd = min(chunkSize, audioDataSize - currentStart) // Generate and send chunks of audio data as `audioevent` // events until the entire file has been sent. Each event is // yielded to the SDK after being created. while currentStart < audioDataSize { let dataChunk = audioData[currentStart ..< currentEnd] let audioEvent = TranscribeStreamingClientTypes.AudioStream.audioevent( .init(audioChunk: dataChunk) ) let yieldResult = continuation.yield(audioEvent) switch yieldResult { case .enqueued(_): // The chunk was successfully enqueued into the // stream. The `remaining` parameter estimates how // much room is left in the queue, but is ignored here. break case .dropped(_): // The chunk was dropped because the queue buffer // is full. This will cause transcription errors. print("Warning: Dropped audio! The transcription will be incomplete.") case .terminated: print("Audio stream terminated.") continuation.finish() return default: print("Warning: Unrecognized response during audio streaming.") } currentStart = currentEnd currentEnd = min(currentStart + chunkSize, audioDataSize) } // Let the SDK's continuation block know the stream is over. continuation.finish() } } return audioStream } /// Run the transcription process. /// /// - Throws: An error from `TranscribeError`. func transcribe(encoding: TranscribeStreamingClientTypes.MediaEncoding) async throws { // Create the Transcribe Streaming client. let client = TranscribeStreamingClient( config: try await TranscribeStreamingClient.TranscribeStreamingClientConfiguration( region: region ) ) // Start the transcription running on the audio stream. let output = try await client.startStreamTranscription( input: StartStreamTranscriptionInput( audioStream: try await createAudioStream(), languageCode: TranscribeStreamingClientTypes.LanguageCode(rawValue: lang), mediaEncoding: encoding, mediaSampleRateHertz: sampleRate ) ) // Iterate over the events in the returned transcript result stream. // Each `transcriptevent` contains a list of result fragments which // need to be concatenated together to build the final transcript. for try await event in output.transcriptResultStream! { switch event { case .transcriptevent(let event): for result in event.transcript?.results ?? [] { guard let transcript = result.alternatives?.first?.transcript else { continue } // If showing partial results is enabled and the result is // partial, show it. Partial results may be incomplete, and // may be inaccurate, with upcoming audio making the // transcription complete or by giving more context to make // transcription make more sense. if (result.isPartial && showPartial) { print("[Partial] \(transcript)") } // When the complete fragment of transcribed text is ready, // print it. This could just as easily be used to draw the // text as a subtitle over a playing video, though timing // would need to be managed. if !result.isPartial { if (showPartial) { print("[Final ] ", terminator: "") } print(transcript) } } default: print("Error: Unexpected message from Amazon Transcribe:") } } } /// Convert the value of the `--format` command line option into the /// corresponding Transcribe Streaming `MediaEncoding` type. /// /// - Returns: The `MediaEncoding` equivalent of the format specified on /// the command line. func getMediaEncoding() -> TranscribeStreamingClientTypes.MediaEncoding { let mediaEncoding: TranscribeStreamingClientTypes.MediaEncoding switch format { case .flac: mediaEncoding = .flac case .ogg: mediaEncoding = .oggOpus case .pcm: mediaEncoding = .pcm } return mediaEncoding } } // -MARK: - Entry point /// The program's asynchronous entry point. @main struct Main { static func main() async { let args = Array(CommandLine.arguments.dropFirst()) do { let command = try ExampleCommand.parse(args) try await command.transcribe(encoding: command.getMediaEncoding()) } catch let error as TranscribeError { print("ERROR: \(error.errorDescription ?? "Unknown error")") } catch { ExampleCommand.exit(withError: error) } } } /// Errors thrown by the example's functions. enum TranscribeError: Error { /// No transcription stream available. case noTranscriptionStream /// The source media file couldn't be read. case readError var errorDescription: String? { switch self { case .noTranscriptionStream: return "No transcription stream returned by Amazon Transcribe." case .readError: return "Unable to read the source audio file." } } }
SDK per C++
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

int main() { Aws::SDKOptions options; Aws::InitAPI(options); { //TODO(User): Set to the region of your AWS account. const Aws::String region = Aws::Region::US_WEST_2; //Load a profile that has been granted AmazonTranscribeFullAccess AWS managed permission policy. Aws::Client::ClientConfiguration config; #ifdef _WIN32 // ATTENTION: On Windows with the AWS C++ SDK, this example only runs if the SDK is built // with the curl library. // For more information, see the accompanying ReadMe. // For more information, see "Building the SDK for Windows with curl". // https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/setup-windows.html //TODO(User): Update to the location of your .crt file. config.caFile = "C:/curl/bin/curl-ca-bundle.crt"; #endif config.region = region; TranscribeStreamingServiceClient client(config); StartStreamTranscriptionHandler handler; handler.SetOnErrorCallback( [](const Aws::Client::AWSError<TranscribeStreamingServiceErrors> &error) { std::cerr << "ERROR: " + error.GetMessage() << std::endl; }); //SetTranscriptEventCallback called for every 'chunk' of file transcripted. // Partial results are returned in real time. handler.SetTranscriptEventCallback([](const TranscriptEvent &ev) { for (auto &&r: ev.GetTranscript().GetResults()) { if (r.GetIsPartial()) { std::cout << "[partial] "; } else { std::cout << "[Final] "; } for (auto &&alt: r.GetAlternatives()) { std::cout << alt.GetTranscript() << std::endl; } } }); StartStreamTranscriptionRequest request; request.SetMediaSampleRateHertz(SAMPLE_RATE); request.SetLanguageCode(LanguageCode::en_US); request.SetMediaEncoding( MediaEncoding::pcm); // wav and aiff files are PCM formats. request.SetEventStreamHandler(handler); auto OnStreamReady = [](AudioStream &stream) { Aws::FStream file(FILE_NAME, std::ios_base::in | std::ios_base::binary); if (!file.is_open()) { std::cerr << "Failed to open " << FILE_NAME << '\n'; } std::array<char, BUFFER_SIZE> buf; int i = 0; while (file) { file.read(&buf[0], buf.size()); if (!file) std::cout << "File: only " << file.gcount() << " could be read" << std::endl; Aws::Vector<unsigned char> bits{buf.begin(), buf.end()}; AudioEvent event(std::move(bits)); if (!stream) { std::cerr << "Failed to create a stream" << std::endl; break; } //The std::basic_istream::gcount() is used to count the characters in the given string. It returns //the number of characters extracted by the last read() operation. if (file.gcount() > 0) { if (!stream.WriteAudioEvent(event)) { std::cerr << "Failed to write an audio event" << std::endl; break; } } else { break; } std::this_thread::sleep_for(std::chrono::milliseconds( 25)); // Slow down because we are streaming from a file. } if (!stream.WriteAudioEvent( AudioEvent())) { // Per the spec, we have to send an empty event (an event without a payload) at the end. std::cerr << "Failed to send an empty frame" << std::endl; } else { std::cout << "Successfully sent the empty frame" << std::endl; } stream.flush(); stream.Close(); }; Aws::Utils::Threading::Semaphore signaling(0 /*initialCount*/, 1 /*maxCount*/); auto OnResponseCallback = [&signaling]( const TranscribeStreamingServiceClient * /*unused*/, const Model::StartStreamTranscriptionRequest & /*unused*/, const Model::StartStreamTranscriptionOutcome &outcome, const std::shared_ptr<const Aws::Client::AsyncCallerContext> & /*unused*/) { if (!outcome.IsSuccess()) { std::cerr << "Transcribe streaming error " << outcome.GetError().GetMessage() << std::endl; } signaling.Release(); }; std::cout << "Starting..." << std::endl; client.StartStreamTranscriptionAsync(request, OnStreamReady, OnResponseCallback, nullptr /*context*/); signaling.WaitOne(); // Prevent the application from exiting until we're done. std::cout << "Done" << std::endl; } Aws::ShutdownAPI(options); return 0; }
PrivacyCondizioni del sitoPreferenze cookie
© 2025, Amazon Web Services, Inc. o società affiliate. Tutti i diritti riservati.