mirror of
https://gitee.com/mymagicpower/AIAS.git
synced 2024-12-03 12:48:12 +08:00
.. | ||
src | ||
ffmpeg_audio_sdk.iml | ||
pom.xml | ||
README_cn.md | ||
README.md |
ffmpeg Audio Toolbox
Current features include:
- Audio parameter conversion (including sample rate, encoding, bit depth, and number of channels)
- Audio format conversion (e.g. wav to mp3 encoding, mp3 to wav encoding)
- Get audio array (float audio array)
Basic concepts of audio data processing
Java uses big-endian byte order by default, while actual encoded audio data is in little-endian byte order. If processing single 8-bit audio, no conversion is needed, but if it is 16-bit or higher, it needs to be processed into little-endian byte order.
Little-endian: the high byte of the data is stored at the low end of the address, and the low byte is stored at the high end of the address. Big-endian: the high byte of the data is stored at the high end of the address, and the low byte is stored at the low end of the address. Big-endian stores data in the same order as numbers are written, while little-endian stores data in the reverse order.
ByteBuffer byteBuffer=ByteBuffer.wrap(data, 0,dataLength);
// Convert byteBuffer to little-endian and get shortBuffer
ShortBuffer shortBuffer=byteBuffer.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
// Get the short array from shortBuffer
short[] shortSamples=new short[dataLength/2];
shortBuffer.get(shortSamples,0,shortLength);
Common ffmpeg commands for audio format conversion
The Java implementation is provided in the program.
1. Convert MP3 to wav
ffmpeg -i input.mp3 -acodec pcm_s16le -ac 2 -ar 44100 output.wav
2. Convert m4a to wav
ffmpeg -i input.m4a -acodec pcm_s16le -ac 2 -ar 44100 output.wav
3. Convert wav to PCM
ffmpeg -i input.wav -f s16le -ar 44100 -acodec pcm_s16le output.raw
4. Convert PCM to wav
ffmpeg -i input.raw -f s16le -ar 44100 -ac 2 -acodec pcm_s16le output.wav
Parameter explanation:
# ffmpeg -i INPUT -ac CHANNELS -ar FREQUENCY -acodec PCMFORMAT OUTPUT
-i sets the input stream
-f sets the output format (s16le for 16 bits, f32le for 32 bits)
-ar sets the sample rate
-ac sets the number of audio channels (1 for mono)
-acodec sets the audio codec. If not set, the same codec as the input stream will be used.
Example - AudioExample
...
// Convert wav sample rate parameter
AudioConversionUtils.convert("src/test/resources/test.wav", "build/output/test_.wav", avcodec.AV_CODEC_ID_PCM_S16LE, 8000, 1);
// Convert wav to mp3 encoding
AudioConversionUtils.convert("src/test/resources/test.wav", "build/output/test.mp3", avcodec.AV_CODEC_ID_MP3, 8000, 1);
// Convert mp3 to wav encoding
AudioConversionUtils.convert("src/test/resources/test.mp3", "build/output/test.wav", avcodec.AV_CODEC_ID_PCM_S16LE, 16000, 1);
// Audio float array
logger.info("audio float array: {}", Arrays.toString(AudioArrayUtils.frameData("src/test/resources/test.wav")));
...