JAVA切割wav语音

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**

  • wav音频文件截取工具
  • (适用于比特率为128kbps的wav音频文件,此类音频文件的头部信息占用长度44字节)
  • @author lwj

*/
public class WavCut {

/** 
 * 截取wav音频文件 
 * @param sourcepath  源文件地址 
 * @param targetpath  目标文件地址 
 * @param start  截取开始时间(秒) 
 * @param end  截取结束时间(秒) 
 *  
 * return  截取成功返回true,否则返回false 
 */  
public static boolean cut(String sourcefile, String targetfile, int start, int end) {  
    try{  
        if(!sourcefile.toLowerCase().endsWith(".wav") || !targetfile.toLowerCase().endsWith(".wav")){  
            return false;  
        }  
        File wav = new File(sourcefile);  
        if(!wav.exists()){  
            return false;  
        }  
        long t1 = getTimeLen(wav);  //总时长(秒)  
        if(start<0 || end<=0 || start>=t1 || end>t1 || start>=end){  
            return false;  
        }  
        FileInputStream fis = new FileInputStream(wav);  
        long wavSize = wav.length()-44;  //音频数据大小(44为128kbps比特率wav文件头长度)  
        long splitSize = (wavSize/t1)*(end-start);  //截取的音频数据大小  
        long skipSize = (wavSize/t1)*start;  //截取时跳过的音频数据大小  
        int splitSizeInt = Integer.parseInt(String.valueOf(splitSize));  
        int skipSizeInt = Integer.parseInt(String.valueOf(skipSize));  
          
        ByteBuffer buf1 = ByteBuffer.allocate(4);  //存放文件大小,4代表一个int占用字节数  
        buf1.putInt(splitSizeInt+36);  //放入文件长度信息  
        byte[] flen = buf1.array();  //代表文件长度  
        ByteBuffer buf2 = ByteBuffer.allocate(4);  //存放音频数据大小,4代表一个int占用字节数  
        buf2.putInt(splitSizeInt);  //放入数据长度信息  
        byte[] dlen = buf2.array();  //代表数据长度  
        flen = reverse(flen);  //数组反转  
        dlen = reverse(dlen);  
        byte[] head = new byte[44];  //定义wav头部信息数组  
        fis.read(head, 0, head.length);  //读取源wav文件头部信息  
        for(int i=0; i<4; i++){  //4代表一个int占用字节数  
            head[i+4] = flen[i];  //替换原头部信息里的文件长度  
            head[i+40] = dlen[i];  //替换原头部信息里的数据长度  
        }  
        byte[] fbyte = new byte[splitSizeInt+head.length];  //存放截取的音频数据  
        for(int i=0; i<head.length; i++){  //放入修改后的头部信息  
            fbyte[i] = head[i];  
        }  
        byte[] skipBytes = new byte[skipSizeInt];  //存放截取时跳过的音频数据  
        fis.read(skipBytes, 0, skipBytes.length);  //跳过不需要截取的数据  
        fis.read(fbyte, head.length, fbyte.length-head.length);  //读取要截取的数据到目标数组  
        fis.close();  
          
        File target = new File(targetfile);  
        if(target.exists()){  //如果目标文件已存在,则删除目标文件  
            target.delete();  
        }  
        FileOutputStream fos = new FileOutputStream(target);  
        fos.write(fbyte);  
        fos.flush();  
        fos.close();  
    }catch(IOException e){  
        e.printStackTrace();  
        return false;  
    }  
    return true;  
}  
  
/** 
 * 获取音频文件总时长 
 * @param filePath  文件路径 
 * @return 
 */  
public static long getTimeLen(File file){  
    long tlen = 0;  
    if(file!=null && file.exists()){  
        Encoder encoder = new Encoder();  
        try {  
             MultimediaInfo m = encoder.getInfo(file);  
             long ls = m.getDuration();  
             tlen = ls/1000;  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    return tlen;  
}  
  
/** 
* 数组反转 
* @param array 
*/  
public static byte[] reverse(byte[] array){  
    byte temp;  
    int len=array.length;  
    for(int i=0;i<len/2;i++){  
        temp=array[i];  
        array[i]=array[len-1-i];  
        array[len-1-i]=temp;  
    }  
    return array;  
}  
  
public static void main(String[] args){  
    System.out.println(cut("f:\\111.wav","f:\\111-cut_0_10.wav",0,10));  
    System.out.println(cut("f:\\111.wav","f:\\111-cut_10_20.wav",10,20));  
    System.out.println(cut("f:\\111.wav","f:\\111-cut_20_28.wav",20,28));  
}  

}

要实现Python截取WAV音频,你可以按照以下步骤进行操作: 1. 导入必要的库:使用Python的wave库来读取WAV文件。 2. 打开WAV文件:使用wave.open()函数打开指定的WAV文件。 3. 获取音频信息:通过读取WAV文件的getparams()方法来获取音频的参数信息,如采样频率、声道数等。 4. 设置截取的起止时分秒:根据需要,设置起止时分秒来确定要截取的音频段落。 5. 计算截取的帧数:根据音频的采样率和设置的起止时分秒,计算出对应的帧数范围。 6. 设置输出音频信息:根据需要,设置截取出来的音频的名称、专辑名称、歌手名称等信息。 7. 创建输出音频文件:使用wave.open()函数创建一个新的WAV文件来存储截取出来的音频。 8. 读取并写入截取的音频数据:使用WAV文件的readframes()方法读取指定范围的音频帧,并使用writeframes()方法将这些帧写入到新的WAV文件中。 9. 关闭文件:使用WAV文件的close()方法关闭输入和输出文件。 以下是一个示例代码,演示了如何使用Python截取WAV音频: ```python import wave # 打开WAV文件 input_file = wave.open("input.wav", "rb") output_file = wave.open("output.wav", "wb") # 获取音频信息 params = input_file.getparams() # 设置截取的起止时分秒 start_time = 0 # 设置起始时间(秒) end_time = 10 # 设置结束时间(秒) # 计算截取的帧数 start_frame = int(start_time * params.framerate) end_frame = int(end_time * params.framerate) # 设置输出音频信息 output_file.setparams(params) # 创建输出音频文件 output_file.writeframes(input_file.readframes(start_frame)) output_file.writeframes(input_file.readframes(end_frame - start_frame)) # 关闭文件 input_file.close() output_file.close() ``` 请注意,上述示例代码仅展示了截取WAV音频的基本步骤,你可以根据实际需求进行更进一步的处理和扩展。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值