JavaDemo——wav转mp3

本文介绍如何使用JAVE工具包将WAV文件转换为MP3格式,包括Maven依赖配置和示例代码,展示了设置音频属性、编码属性及使用Encoder进行转换的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

wav转mp3比较简单,使用JAVE工具包就ok,JAVE可以转换各种视频音频。

JAVE网站:http://www.sauronsoftware.it/projects/jave/manual.php

 

maven依赖:

<!-- https://mvnrepository.com/artifact/ws.schild/jave-core -->
	<dependency>
	    <groupId>ws.schild</groupId>
	    <artifactId>jave-core</artifactId>
	    <version>2.4.5</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/ws.schild/jave-native-win64 -->
	<dependency>
	    <groupId>ws.schild</groupId>
	    <artifactId>jave-native-win64</artifactId>
	    <version>2.4.5</version>
	</dependency>

Demo:

/**
 * 2019年2月22日下午2:21:41
 */
package wavToMp3;

import java.io.File;
import java.io.IOException;

import ws.schild.jave.AudioAttributes;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.EncodingAttributes;
import ws.schild.jave.InputFormatException;
import ws.schild.jave.MultimediaObject;

/**
 * @author XWF
 *
 */
public class Wav2Mp3 {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws EncoderException 
	 * @throws InputFormatException 
	 * @throws IllegalArgumentException 
	 */
	public static void main(String[] args) throws IOException, IllegalArgumentException, InputFormatException, EncoderException {
		File source = new File("test.wav");
		File target = new File("test.mp3");
		if(!target.exists()) {
			target.createNewFile();
		}
		AudioAttributes audio = new AudioAttributes();
		audio.setCodec("libmp3lame");
		audio.setBitRate(new Integer(16000));
		audio.setChannels(new Integer(1));
		audio.setSamplingRate(new Integer(8000));
		EncodingAttributes attrs = new EncodingAttributes();
		attrs.setFormat("mp3");
		attrs.setAudioAttributes(audio);
		Encoder encoder = new Encoder();
		MultimediaObject sourceObj = new MultimediaObject(source);
		encoder.encode(sourceObj, target, attrs);
		System.out.println("finished.");
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值