先用Java CV的拉流對象實現對音視頻的格式轉換:
1、音視頻轉純音頻
2、音頻指定采樣率和聲道
接着手動搞定采用率的位深,對音頻做16位位深和小端點轉換。最后把轉好的字節數組寫入文件,這個簡單,用一個IO處理流對象搞定,看代碼:
import org.bytedeco.javacv.FFmpegFrameGrabber; import org.bytedeco.javacv.Frame; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.ShortBuffer; /** * 音視頻轉單聲道16位16K赫茲小端點pcm音頻 */ public class PCMFileProduce { public static void main(String[] args) throws Exception { String path = "E:\\BaiduNetdiskDownload\\roundDeskS02E01.mp4"; //待轉音視頻 File file = new File("E:\\BaiduNetdiskDownload\\pcmTest.pcm"); //目標文件 OutputStream os = new BufferedOutputStream(new FileOutputStream(file)); FFmpegFrameGrabber frameGrabber = FFmpegFrameGrabber.createDefault(path); frameGrabber.setSampleRate(16000); //16K赫茲采樣率 frameGrabber.setAudioChannels(1); //單聲道 frameGrabber.start(); Frame frame; Buffer buffer; short[] shorts; byte[] bytes; System.out.println("開始轉換文件"); while ((frame = frameGrabber.grabSamples()) != null) { if (frame.samples == null) { continue; } buffer = frame.samples[0]; shorts = new short[buffer.limit()]; ((ShortBuffer) buffer).get(shorts); bytes = shortArr2byteArr(shorts, buffer.limit()); os.write(bytes); } os.close(); // 關閉寫文件 frameGrabber.close(); // 直接關閉拉流 System.out.println("轉換結束"); } /** * 8位字節數組轉16位字節數組,也就是16位的采樣位深,轉小端點字節數組 * * @param shortArr * @param shortArrLen * @return */ private static byte[] shortArr2byteArr(short[] shortArr, int shortArrLen) { byte[] byteArr = new byte[shortArrLen * 2]; ByteBuffer.wrap(byteArr).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(shortArr); return byteArr; } }
看輸出:
Connected to the target VM, address: '127.0.0.1:50289', transport: 'socket' 開始轉換文件 轉換結束 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'E:\BaiduNetdiskDownload\roundDeskS02E01.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.20.100 Duration: 00:39:54.27, start: 0.000000, bitrate: 815 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1080x606, 744 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc (default) Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 64 kb/s (default) Metadata: handler_name : SoundHandler Disconnected from the target VM, address: '127.0.0.1:50289', transport: 'socket' Process finished with exit code 0
比較下ffmpeg和上面代碼轉換的兩個文件:
命令行轉換參見:ffmpeg音視頻轉單聲道16位16K赫茲小端點pcm音頻