2017-01-21 3 views
1

자바 8 프로젝트에서 javax.sound * 라이브러리를 mp3 파일로 읽을 수 있도록 mp3spi를 설치했습니다. 이제 내 목표는 wav 파일에 mp3를 쓰는 것입니다. 그러나 결과가 올바르지 않습니다. 이 4-5메가바이트의 압축 된 MP3에서 30메가바이트 동안의 파일을 생성mp3 to wav in Java

File mp3 = new File("C:\\music\\greatest-songs-of-all-time\\RebeccaBlack-Friday.mp3"); 
if(!mp3.exists()) { 
    throw new FileNotFoundException("couldn't find mp3"); 
} 
FileInputStream fis = new FileInputStream(mp3); 
BufferedInputStream bis = new BufferedInputStream(fis); 

AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(bis); 
AudioFormat sourceFormat = mp3Stream.getFormat(); 
AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
     sourceFormat.getSampleRate(), 16, 
     sourceFormat.getChannels(), 
     sourceFormat.getChannels() * 2, 
     sourceFormat.getSampleRate(), 
     false); 

try (final AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(mp3Stream)) { 
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    final AudioInputStream convert2AIS = AudioSystem.getAudioInputStream(convertFormat, convert1AIS); 
    System.out.println("Length is: " + mp3Stream.getFrameLength() + " div by " + mp3Stream.getFormat().getFrameRate()); 
    byte [] buffer = new byte[8192]; 
    int iteration = 0; 
    while(true){ 
     int readCount = convert2AIS.read(buffer, 0, buffer.length); 
     if(readCount == -1){ 
      break; 
     } 
     baos.write(buffer, 0, readCount); 
     iteration++; 
    } 

    System.out.println("completed with iteration: " + iteration); 

    FileOutputStream fw = new FileOutputStream("C:\\temp\\out-2.wav"); 
    fw.write(baos.toByteArray()); 
    fw.close(); 
} 

bis.close(); 
fis.close(); 

:

public static void mp3ToWav(InputStream mp3Data) throws UnsupportedAudioFileException, IOException { 
    AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(mp3Data); 
    AudioFormat format = mp3Stream.getFormat(); 
    AudioFormat convertFormat = new AudioSystem.write(mp3Stream, Type.WAVE, new File("C:\\temp\\out.wav")); 
} 

여기에 설명 된대로 하나의 다른 방법 (mp3 to wav conversion in java)있다 : 여기에 간단한 형식의 코드는 , 그러나 그것은 유효한 WAV 파일로 작동하지 않습니다.

나를 위해 작동하는 방법은 오디오 부분을 잘라내어 볼륨과 재생 속도를 수정하는 등의 다른 처리를하고 싶기 때문에 JLayer Converter 클래스를 사용하는 것과 관련이 있습니다. 네이티브 라이브러리로 작업하는 것이 좋습니다.

+0

나는 mp3Data의 존재를 도전; 당신은 그것에서 어디에서 그것을 얻고 당신은 그것을 포함합니까? 어쨌든 당신은 바이트와 함께 시작해야합니다 - 바이트 배열 - 대답과 마찬가지로 - 바이트의 배열을 가지고 있습니까? – gpasch

+1

'AudioFormat을 convertFormat = AudioFormat을 새로운 (AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16 sourceFormat.getChannels() sourceFormat.getChannels() * 2 sourceFormat.getSampleRate() 거짓); '이 값들을 하드 코딩하지 마라. 원본 형식과 동일한 값을 사용하면 광고 된대로 작동합니다. –

+0

gspasch 당신은 mp3 데이터의 존재에 도전 할 수 있지만, 그것이 존재한다는 것을 보증합니다! 코드의 해당 부분을 포함하도록 질문을 수정하겠습니다. – IcedDante

답변

1

mp3 스트림을 연 후에는 일반적으로 파일로 변환하기 전에 변환해야합니다. 이처럼

(안된) :

public static void mp3ToWav(File mp3Data) throws UnsupportedAudioFileException, IOException { 
    // open stream 
    AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(mp3Data); 
    AudioFormat sourceFormat = mp3Stream.getFormat(); 
    // create audio format object for the desired stream/audio format 
    // this is *not* the same as the file format (wav) 
    AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
     sourceFormat.getSampleRate(), 16, 
     sourceFormat.getChannels(), 
     sourceFormat.getChannels() * 2, 
     sourceFormat.getSampleRate(), 
     false); 
    // create stream that delivers the desired format 
    AudioInputStream converted = AudioSystem.getAudioInputStream(convertFormat, mp3Stream); 
    // write stream into a file with file format wav 
    AudioSystem.write(converted, Type.WAVE, new File("C:\\temp\\out.wav")); 
} 
+0

나는 이것을 테스트하지 않은 것으로 표시 했으므로 나는 그것을 시도했지만 효과가 없다고 말하고 싶었다. 그것은 약간의 조정 없이는 작동하지 않는다고 말할 수는 없지만, 지금은 그것을 실현할 수 없었습니다. – IcedDante

+2

그것은 저에게 효과적이었습니다. 나는 mp3 서비스가 작동하도록 플러그인을 몇 개 추가해야했습니다. – vict