将从m3u8流提取的MP3音频发送到IBM Watson Speech To Text

时间:2018-11-14 15:22:33

标签: c# audio ffmpeg ibm-watson m3u8

我正在从M3U8实时URL中提取MP3格式的音频,最终目标是将实时音频流发送到IBM Watson Speech To Text。 m3u8是通过Process调用外部脚本而获得的。然后,我使用FFMPEG脚本在stdout中获取音频。如果我将音频保存在文件中,但我不想保存提取的音频,则可以正常工作,我需要将数据直接发送到STT服务。到目前为止,我是这样进行的:

SpeechToTextService speechToTextService = new SpeechToTextService(sttUsername, sttPassword);
string m3u8Url = "https://something.m3u8";
char[] buffer = new char[48000];
Process ffmpeg = new ProcessHelper(@"ffmpeg\ffmpeg.exe", $"-v 0 -i {m3u8Url} -acodec mp3 -ac 2 -ar 48000 -f mp3 -");

ffmpeg.Start();
int count;
while ((count = ffmpeg.StandardOutput.Read(buffer, 0, 48000)) > 0)
{
    ffmpeg.StandardOutput.Read(buffer, 0, 48000);
    var answer = speechToTextService.RecognizeSessionless(
        audio: buffer.Select(c => (byte)c).ToArray(),
        contentType: "audio/mpeg",
        smartFormatting: true,
        speakerLabels: false,
        model: "en-US_BroadbandModel"
    );
    // Get answer.ResponseJson, deserializing, clean buffer, etc...
}

请求转录音频时出现此错误:

An unhandled exception of type 'System.AggregateException' occurred in IBM.WatsonDeveloperCloud.SpeechToText.v1.dll: 'One or more errors occurred. (The API query failed with status code BadRequest: Bad Request | x-global-transaction-id: bd6cd203720a70d83b9a03451fe28973 | X-DP-Watson-Tran-ID: bd6cd203720a70d83b9a03451fe28973)'
 Inner exceptions found, see $exception in variables window for more details.
 Innermost exception     IBM.WatsonDeveloperCloud.Http.Exceptions.ServiceResponseException : The API query failed with status code BadRequest: Bad Request | x-global-transaction-id: bd6cd203720a70d83b9a03451fe28973 | X-DP-Watson-Tran-ID: bd6cd203720a70d83b9a03451fe28973
   at IBM.WatsonDeveloperCloud.Http.Filters.ErrorFilter.OnResponse(IResponse response, HttpResponseMessage responseMessage)
   at IBM.WatsonDeveloperCloud.Http.Request.<GetResponse>d__30.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IBM.WatsonDeveloperCloud.Http.Request.<AsMessage>d__23.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IBM.WatsonDeveloperCloud.Http.Request.<As>d__24`1.MoveNext()

ProcessHelper只是为了方便:

class ProcessHelper : Process
{
    private string command;
    private string arguments;
    public ProcessHelper(string command, string arguments, bool redirectStandardOutput = true)
    {
        this.command = command;
        this.arguments = arguments;
        StartInfo = new ProcessStartInfo()
        {
            FileName = this.command,
            Arguments = this.arguments,
            UseShellExecute = false,
            RedirectStandardOutput = redirectStandardOutput,
            CreateNoWindow = true
        };
    }
}

可以肯定我做错了,我希望有人对此有所启发。谢谢。

1 个答案:

答案 0 :(得分:0)

我仍然不知道为什么我无法识别没有会话的缓冲区(第二个ffmpeg.StandardOutput.Read(buffer,0,48000);是一个错字顺便说一句),但是我设法使其与websocket一起工作,就像那里解释的那样。 https://gist.github.com/nfriedly/0240e862901474a9447a600e5795d500