flac音频文件到文本文件java程序

时间:2017-08-07 17:09:26

标签: java speech-to-text

我有可以将.wav转换为文本文件的代码。但我想将.flac转换为文本。看起来AudioInputStream似乎不支持.flac类型。我应该在mycode中进行哪些修改,以便将.flac文件转换为文本?这是完整的代码。我添加这一行是因为堆栈溢出要求我这样做。

import java.io.File;

import javax.sound.sampled.*;

public class AudioFileConvert01 {
     public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Usage: java AudioFileConvert01 " +   "inputFile outputFile");
            System.exit(0);
        }
        System.out.println("Input file: " + args[0]);
        System.out.println("Output file: " + args[1]);
        // Output file type depends on outputfile name extension.

        String outputTypeStr = args[1].substring(args[1].lastIndexOf(".") + 1);
        System.out.println("Output type: " + outputTypeStr);
    AudioFileFormat.Type outputType = getTargetType(outputTypeStr);
    if (outputType != null) {
        System.out.println("Output type is supported");
    } else {
        System.out.println("Output type not supported.");
        getTargetTypesSupported();
        System.exit(0);
    }
    // Note that input file type does not depend on file name or extension.
    File inputFileObj = new File(args[0]);
    AudioInputStream audioInputStream = null;
    try {
        audioInputStream = AudioSystem.getAudioInputStream(inputFileObj);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    } // end catch
    System.out.println("Input file format:");
    showFileType(inputFileObj);
    int bytesWritten = 0;
    try {
        bytesWritten = AudioSystem.write(audioInputStream, outputType, new File(args[1]));
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
    System.out.println("Bytes written: " + bytesWritten);
    System.out.println("Output file format:");
    showFileType(new File(args[1]));
}

private static void getTargetTypesSupported() {
    AudioFileFormat.Type[] typesSupported = AudioSystem.getAudioFileTypes();
    System.out.print("Supported audio file types:");
    for (int i = 0; i < typesSupported.length; i++) {
        System.out.print(" " + typesSupported[i].getExtension());
    }
    // for
    // loop
    System.out.println();

}

private static AudioFileFormat.Type getTargetType(String extension) {
    AudioFileFormat.Type[] typesSupported = AudioSystem.getAudioFileTypes();
    for (int i = 0; i < typesSupported.length; i++) {
        if (typesSupported[i].getExtension().equals(extension)) {
            return typesSupported[i];
        }
    }
    return null;// no match
}

private static void showFileType(File file) {
    try {
        System.out.println(AudioSystem.getAudioFileFormat(file));
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
   }
}

0 个答案:

没有答案