语音amr文件转换为mp3文件

2024-06-20 06:18
文章标签 转换 语音 mp3 amr

本文主要是介绍语音amr文件转换为mp3文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


1.安卓前端生成amr语音文件,经过base64加密变成加密字符串


2.把加密后的字符串解密成amr文件

生成amr文件目录
  String fileUrl = System.getProperty("user.dir").replace(
                            "bin", "webapps")
                            + File.separator
                            + "wfCloudRes"
                            + File.separator
                            + "image"
                            + File.separator
                            + dateStr
                            + File.separator + "topic" + File.separator + maxId;

public static String base64ToIo(String strBase64,String fileUrl) throws IOException {
        String string = strBase64;
        String fileName = fileUrl+File.separator+"audio.amr";
        String saveDir = fileUrl;
        File dir = new File(saveDir);
        if(!dir.exists()){
            dir.mkdirs();
        }
        try {
            // 解码,然后将字节转换为文件
            byte[] bytes = new BASE64Decoder().decodeBuffer(string); // 将字符串转换为byte数组
            
            FileOutputStream out = new FileOutputStream(fileName);
            out.write(bytes);
            out.close();
            
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return fileName;
    }



3.amr文件转换为MP3文件

这个转化要使用第三方的jar包jave-1.0.2.jar

http://www.sauronsoftware.it/projects/jave/download.php



// mp3文件目录
                        String fileTargetUrl = System.getProperty("user.dir")
                                .replace("bin", "webapps")
                                + File.separator
                                + "wfCloudRes"
                                + File.separator
                                + "image"
                                + File.separator
                                + dateStr
                                + File.separator
                                + "topic"
                                + File.separator
                                + maxId
                                + File.separator + "audio.mp3";


public static void changeToMp3(String sourcePath, String targetPath) {
        File source = new File(sourcePath);
        File target = new File(targetPath);
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");
//        audio.setBitRate(new Integer(64000));
//        audio.setChannels(new Integer(2));
//        audio.setSamplingRate(new Integer(22050));
        
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);
        
        try {
            Encoder encoder = new Encoder();
            encoder.encode(source, target, attrs);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InputFormatException e) {
            e.printStackTrace();
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }

4.删除amr文件

FileUtils.deleteFile(fileUrl, "amr");


public static boolean deleteFile(String path, String suffixType) {
        boolean flag = false;
        if (StringUtils.isEmpty(path)) {
            return flag;
        }
        File file = new File(path);// 里面输入特定目录
        File temp = null;
        File[] filelist = file.listFiles();
        for (int i = 0; i < filelist.length; i++) {
            temp = filelist[i];

            if (temp.getName().endsWith(suffixType)) {// 获得文件名,如果后缀为“”,这个你自己写,就删除文件
                temp.delete();// 删除文件
                flag = true;
            }
        }
        return flag;
    }



Base64加密和解密工具类

mport java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;



public class Base64Utils {

    public static void main(String[] args) throws Exception {
        

//        String strBase64 = Base64Utils.ioToBase64("d:/bubugao.amr"); // 将 io 转换为 base64编码
//        System.out.println(">>> " + strBase64);
//        System.out.println(Base64Utils.base64ToIo(strBase64,"d:/bubugao.mp3")); // 将 base64编码转换为
                                                                // 文件流,生成一幅新图片
        
//        System.out.println(Base64Utils.encodeBase64File("d:/bubugao.amr"));
        
        
    }

    public static String ioToBase64(String path) throws IOException {
//        String fileName = "D:/u18.jpg"; // 源文件
        String source = path;
        String strBase64 = null;
        try {
            InputStream in = new FileInputStream(source);
            // in.available()返回文件的字节长度
            byte[] bytes = new byte[in.available()];
            // 将文件中的内容读入到数组中
            in.read(bytes);
            strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串
            in.close();
        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return strBase64;
    }

    public static String base64ToIo(String strBase64,String fileUrl) throws IOException {
        String string = strBase64;
        String fileName = fileUrl+File.separator+"audio.amr";
        String saveDir = fileUrl;
        File dir = new File(saveDir);
        if(!dir.exists()){
            dir.mkdirs();
        }
        try {
            // 解码,然后将字节转换为文件
            byte[] bytes = new BASE64Decoder().decodeBuffer(string); // 将字符串转换为byte数组
            
            FileOutputStream out = new FileOutputStream(fileName);
            out.write(bytes);
            out.close();
            
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return fileName;
    }

}
文件转换类的代码如下:

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.InputFormatException;

import java.io.File;

public class Mp3Convert {

    public static void changeToMp3(String sourcePath, String targetPath) {
        File source = new File(sourcePath);
        File target = new File(targetPath);
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");
//        audio.setBitRate(new Integer(64000));
//        audio.setChannels(new Integer(2));
//        audio.setSamplingRate(new Integer(22050));
        
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);
        
        try {
            Encoder encoder = new Encoder();
            encoder.encode(source, target, attrs);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InputFormatException e) {
            e.printStackTrace();
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }

    
}



这篇关于语音amr文件转换为mp3文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1077336

相关文章

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

Python实现视频转换为音频的方法详解

《Python实现视频转换为音频的方法详解》这篇文章主要为大家详细Python如何将视频转换为音频并将音频文件保存到特定文件夹下,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. python需求的任务2. Python代码的实现3. 代码修改的位置4. 运行结果5. 注意事项

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

Python 标准库time时间的访问和转换问题小结

《Python标准库time时间的访问和转换问题小结》time模块为Python提供了处理时间和日期的多种功能,适用于多种与时间相关的场景,包括获取当前时间、格式化时间、暂停程序执行、计算程序运行时... 目录模块介绍使用场景主要类主要函数 - time()- sleep()- localtime()- g

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

Java将时间戳转换为Date对象的方法小结

《Java将时间戳转换为Date对象的方法小结》在Java编程中,处理日期和时间是一个常见需求,特别是在处理网络通信或者数据库操作时,本文主要为大家整理了Java中将时间戳转换为Date对象的方法... 目录1. 理解时间戳2. Date 类的构造函数3. 转换示例4. 处理可能的异常5. 考虑时区问题6.

基于C#实现将图片转换为PDF文档

《基于C#实现将图片转换为PDF文档》将图片(JPG、PNG)转换为PDF文件可以帮助我们更好地保存和分享图片,所以本文将介绍如何使用C#将JPG/PNG图片转换为PDF文档,需要的可以参考下... 目录介绍C# 将单张图片转换为PDF文档C# 将多张图片转换到一个PDF文档介绍将图片(JPG、PNG)转

阿里开源语音识别SenseVoiceWindows环境部署

SenseVoice介绍 SenseVoice 专注于高精度多语言语音识别、情感辨识和音频事件检测多语言识别: 采用超过 40 万小时数据训练,支持超过 50 种语言,识别效果上优于 Whisper 模型。富文本识别:具备优秀的情感识别,能够在测试数据上达到和超过目前最佳情感识别模型的效果。支持声音事件检测能力,支持音乐、掌声、笑声、哭声、咳嗽、喷嚏等多种常见人机交互事件进行检测。高效推