Created
January 7, 2020 08:17
-
-
Save kentyeh/7718a4e2993fce2cbf5c9643c93cd3c1 to your computer and use it in GitHub Desktop.
轉換Mp3為Wav
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.InputStream; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import javax.sound.sampled.AudioFormat; | |
import javax.sound.sampled.AudioFileFormat; | |
import javax.sound.sampled.AudioInputStream; | |
import javax.sound.sampled.AudioSystem; | |
import javax.sound.sampled.UnsupportedAudioFileException; | |
import edu.cmu.sphinx.api.Configuration; | |
import edu.cmu.sphinx.api.SpeechResult; | |
import edu.cmu.sphinx.api.StreamSpeechRecognizer; | |
public class Main { | |
/* | |
<dependency> | |
<groupId>com.googlecode.soundlibs</groupId> | |
<artifactId>mp3spi</artifactId> | |
<version>1.9.5.4</version> | |
</dependency> | |
<dependency> | |
<groupId>com.googlecode.soundlibs</groupId> | |
<artifactId>jlayer</artifactId> | |
<version>1.0.1.4</version> | |
<exclusions> | |
<exclusion> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
*/ | |
public static Path converMp3toWav(InputStream is, Path ouput) throws UnsupportedAudioFileException, IllegalArgumentException, Exception { | |
try (AudioInputStream sourceAIS = AudioSystem.getAudioInputStream(is)) { | |
AudioFormat sourceFormat = sourceAIS.getFormat(); | |
AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false); | |
AudioFormat targetFormat = new AudioFormat(44100, 8, 1, true, false); | |
try (final AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS); | |
final AudioInputStream convert2AIS = AudioSystem.getAudioInputStream(targetFormat, convert1AIS); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | |
byte[] buffer = new byte[8192]; | |
while (true) { | |
int readCount = convert2AIS.read(buffer, 0, buffer.length); | |
if (readCount == -1) { | |
break; | |
} | |
baos.write(buffer, 0, readCount); | |
} | |
final byte[] rawResult = baos.toByteArray(); | |
final AudioInputStream audioInputStream = new AudioInputStream(new ByteArrayInputStream(rawResult), | |
targetFormat, rawResult.length); | |
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, Files.newOutputStream(ouput,StandardOpenOption.CREATE)); | |
return ouput; | |
} finally { | |
is.close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment