AWSSDK.转录StartTranscriptionJob完成



我编写了以下使用AWSSDK的代码。转录转录音频文件。代码执行并返回状态为OK的响应,但是对象response. transcriptionjob . transcript为空我想到的解释是,它只表明了工作的开始,一位同事告诉我,我要么现在就需要轮询结果,要么使用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, @"TTSExperimentTranscribed");
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 "";
}

您的同事是正确的,您确实需要轮询结果或以其他方式检查,因为根据媒体的长度,转录可能需要很长时间。AWS SDK示例库包含一个。net示例,其中使用了一些您可能会觉得有用的简单轮询逻辑。

Console.WriteLine("Start a transcription.");
var transcriptionName = $"ExampleTranscription_{DateTime.Now.Ticks}";
var transcriptionJob = await transcribeWrapper.StartTranscriptionJob(
transcriptionName, transcriptionMediaLocation, MediaFormat.Mp3,
LanguageCode.EnUS, null);
Console.WriteLine($"Transcription started: {transcriptionJob.TranscriptionJobName}, " +
$"status: {transcriptionJob.TranscriptionJobStatus}.");
Console.WriteLine($"Waiting for transcription {transcriptionName}.");
while (transcriptionJob.TranscriptionJobStatus != TranscriptionJobStatus.COMPLETED
&& transcriptionJob.TranscriptionJobStatus != TranscriptionJobStatus.FAILED)
{
transcriptionJob =
await transcribeWrapper.GetTranscriptionJob(transcriptionName);
Thread.Sleep(5000);
}
Console.WriteLine($"Transcription status:  {transcriptionJob.TranscriptionJobStatus}, " +
$"completed at: {transcriptionJob.CompletionTime}.");

相关内容

  • 没有找到相关文章

最新更新