MP3Test.java
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cn.org.hentai.jtt1078.test;
import de.sciss.jump3r.lowlevel.LameEncoder;
import de.sciss.jump3r.mp3.Lame;
import javax.sound.sampled.AudioFormat;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* Created by matrixy on 2020/4/27.
*/
public class MP3Test
{
public static void main(String[] args) throws Exception
{
AudioFormat sourceFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000, 16, 1, 1 * 2, -1, false);
LameEncoder encoder = new LameEncoder(sourceFormat, 256, 3, Lame.MEDIUM, false);
byte[] block = new byte[320];
int len = -1;
FileInputStream fis = new FileInputStream("d:\\temp\\hello.pcm");
ByteArrayOutputStream mp3 = new ByteArrayOutputStream(encoder.getOutputBufferSize());
byte[] buffer = new byte[encoder.getPCMBufferSize()];
int bytesToTransfer = 0;
int bytesWritten;
int currentPcmPosition = 0;
FileOutputStream fos = new FileOutputStream("d:\\temp\\fuck.mp3");
while ((len = fis.read(block)) > -1)
{
bytesToTransfer = len;
currentPcmPosition = 0;
while (0 < (bytesWritten = encoder.encodeBuffer(block, currentPcmPosition, bytesToTransfer, buffer)))
{
currentPcmPosition += bytesToTransfer;
bytesToTransfer = Math.min(buffer.length, len - currentPcmPosition);
mp3.write(buffer, 0, bytesWritten);
fos.write(buffer, 0, bytesWritten);
System.out.println(String.format("pcm data: %4d, written: %4d, pos: %4d", len, bytesWritten, currentPcmPosition));
}
System.out.println();
}
fos.close();
fis.close();
encoder.close();
}
}