如何从音频文件中提取文本

时间:2017-11-24 10:23:00

标签: android

我正在创建一个将音频转换为文字的应用。我尝试了谷歌的语音转文本API,但是当你按下一个图标时它会同时识别语音,但我有一个音频文件,我想将其转换为文本。 我搜索了很多,但我只收到了发言权。

2 个答案:

答案 0 :(得分:1)

您可以使用Google的Cloud Speech API。

将其添加到您的gradle文件中:

compile 'com.google.cloud:google-cloud-speech:0.30.0-alpha'

并使用此代码:

    // Instantiates a client
    SpeechClient speech = SpeechClient.create();

    // The path to the audio file to transcribe
    String fileName = "./resources/audio.raw";

    // Reads the audio file into memory
    Path path = Paths.get(fileName);
    byte[] data = Files.readAllBytes(path);
    ByteString audioBytes = ByteString.copyFrom(data);

    // Builds the sync recognize request
    RecognitionConfig config = RecognitionConfig.newBuilder()
        .setEncoding(AudioEncoding.LINEAR16)
        .setSampleRateHertz(16000)
        .setLanguageCode("en-US")
        .build();
    RecognitionAudio audio = RecognitionAudio.newBuilder()
        .setContent(audioBytes)
        .build();

    // Performs speech recognition on the audio file
    RecognizeResponse response = speech.recognize(config, audio);
    List<SpeechRecognitionResult> results = response.getResultsList();

    for (SpeechRecognitionResult result: results) {
      // There can be several alternative transcripts for a given chunk of speech. Just use the
      // first (most likely) one here.
      SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
      System.out.printf("Transcription: %s%n", alternative.getTranscript());
    }
    speech.close();

有关更多信息,请参阅此链接: https://cloud.google.com/speech/docs/reference/libraries#client-libraries-install-java

答案 1 :(得分:0)

您可以从this stackoverflow question找到您想要的内容。您可以参考一个示例github project来确定解决方法。