我编写了以下使用 AWSSDK.Transcribe 转录音频文件的代码。 代码执行并返回状态为 OK 的响应,但对象 response.TranscriptionJob.Transcript 为 null 我想出的解释是,它只表示工作开始,一位同事告诉我,我需要现在轮询结果或使用 AWS Step Functions 或 AWS Lambda 来监控工作完成...还有其他选择吗供我使用以便知道转录何时完成?
public static string Transcribe(string inputAudioFilePath)
{
var awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
// North CA, for streaming "us-west-2"
var config = new AmazonTranscribeServiceConfig { RegionEndpoint = RegionEndpoint.GetBySystemName("us-west-2") };
AmazonTranscribeServiceClient client = new AmazonTranscribeServiceClient(awsCredentials, config);
client.AfterResponseEvent += Client_AfterResponseEvent;
var jobName = String.Format("AWSSTT-{0}", DateTime.Now.ToString("yyyy-MM-dd-hh-mm-t"));
var inputLanguage = "en-US";
#region Upload File to S3 Bucket
string s3BucketName = "tts-experiment";
string s3KeyName = inputAudioFilePath;
mAmazonS3Client = new AmazonS3Client(accessKey, secretKey, mRegionEndpoint);
string s3MediaUrl = UploadFileToS3(inputAudioFilePath, s3BucketName, s3KeyName);
string ext = Path.GetExtension(inputAudioFilePath).ToUpper();
MediaFormat mediaFormat = MediaFormat.Wav;
if (String.Compare(ext, ".MP3", true) == 0)
mediaFormat = MediaFormat.Mp3;
#endregion
var media = new Media() { MediaFileUri = s3MediaUrl };
var request = new StartTranscriptionJobRequest
{
TranscriptionJobName = jobName,
LanguageCode = inputLanguage,
Media = media,
MediaFormat = MediaFormat.Mp3,
};
try
{
string text = "";
StartTranscriptionJobResponse response = client.StartTranscriptionJob(request);
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
Console.WriteLine("Transcription completed successfully");
if (response.TranscriptionJob.Transcript != null && response.TranscriptionJob.Transcript.TranscriptFileUri != null)
{
string transcriptUrl = response.TranscriptionJob.Transcript.TranscriptFileUri.ToString();
WebClient webClient = new WebClient();
string targetDir = Path.Combine(Program.InstallDir, @"TTSExperiment\Transcribed");
if (!Directory.Exists(targetDir))
{
Directory.CreateDirectory(targetDir);
}
string localFilePath = Path.Combine(targetDir, Path.GetFileNameWithoutExtension(transcriptUrl) +".txt");
webClient.DownloadFile(transcriptUrl, localFilePath);
text = File.ReadAllText(localFilePath);
Console.WriteLine($"Transcription job {jobName} completed.");
}
else
{
Console.WriteLine("No Transcript created");
}
return text;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error starting transcription job: {ex.Message}");
}
return "";
}