zoukankan      html  css  js  c++  java
  • 在java中使用ffmpeg将amr格式的语音转为mp3格式

    ffmpeg是一个非常强大的音视频处理工具,官网是:http://ffmpeg.org/ 

    由于ffmpeg在windows上和linux系统上的执行文件不一样(Windows上不需要安装ffmpeg,只需要下载Windows版本的ffmpeg就行。linux上需要用户自己安装ffmpeg---> 参考链接:http://linux.it.net.cn/e/Linuxit/2014/0828/3980.html

      最近最项目是遇到一个需求,就是将安卓端amr格式的录音文件转为mp3格式,然后在网页上播放。

      一、 Windows系统和linux系统的处理方式

      1、首先在Windows系统上倒好解决。方案有2个,一个是使用jave.jar工具包,另一种是直接将下载好的Windows版本的ffmpeg解压,然后将其中bin目录下ffmpeg.exe文件导入到项目中(或者直接使用代码读取本地的ffmpeg.exe执行文件)。

        1.1、 使用jave.jar工具包

          http://mfan.iteye.com/blog/2032454

        1.2、使用ffmpeg.exe执行文件

          1.2.1、使用本地的ffmpeg.exe执行文件,直接通过File获取

          

          1.2.2、将ffmpeg.exe执行文件导入到项目中,通过 URL url = Thread.currentThread().getContextClassLoader().getResource("ffmpeg/windows/"); 来获取

          

         

        1.3、linux服务器上使用ffmpeg将amr转为mp3

          1.3.1、首先要在linux服务器上安装ffmpeg工具,安装方式见上方  

          

      二、utils工具类(代码具体实现)

      

    /**
     * Create By yxl on 2018/6/5
     */
    public class AmrToMP3Utils {
    
        private static Logger logger =Logger.getLogger(AmrToMP3Utils.class);
    
        /**
         * 将amr文件输入转为mp3格式
         * @param file
         * @return
         */
        public static InputStream amrToMP3(MultipartFile file) {
            String ffmpegPath = getLinuxOrWindowsFfmpegPath();
            Runtime runtime = Runtime.getRuntime();
            try {
                String filePath = copyFile(file.getInputStream(), file.getOriginalFilename());
    
                String substring = filePath.substring(0, filePath.lastIndexOf("."));
    
                String mp3FilePath = substring + ".mp3";
    
                //执行ffmpeg文件,将amr格式转为mp3
                //filePath ----> amr文件在临时文件夹中的地址
                //mp3FilePath  ----> 转换后的mp3文件地址
                Process p = runtime.exec(ffmpegPath + "ffmpeg -i " + filePath + " " + mp3FilePath);//执行ffmpeg.exe,前面是ffmpeg.exe的地址,中间是需要转换的文件地址,后面是转换后的文件地址。-i是转换方式,意思是可编码解码,mp3编码方式采用的是libmp3lame
    
                //释放进程
                p.getOutputStream().close();
                p.getInputStream().close();
                p.getErrorStream().close();
                p.waitFor();
    
                File mp3File = new File(mp3FilePath);
                InputStream fileInputStream = new FileInputStream(mp3File);
    
                //应该在调用该方法的地方关闭该input流(使用完后),并且要删除掉临时文件夹下的相应文件
                /*File amrFile = new File(filePath);
                File mp3File = new File(mp3FilePath);
                if (amrFile.exists()) {
                    boolean delete = amrFile.delete();
                    System.out.println("删除源文件:"+delete);
                }
                if (mp3File.exists()) {
                    boolean delete = mp3File.delete();
                    System.out.println("删除mp3文件:"+delete);
                }*/
    
                return fileInputStream;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                runtime.freeMemory();
            }
            return null;
        }
    
        /**
         * 将amr文件输入流转为mp3格式
         * @param inputStream  amr文件的输入流(也可以是其它的文件流)
         * @param fileName  文件名(包含后缀)
         * @return
         */
        public static InputStream amrToMP3(InputStream inputStream, String fileName) {
            String ffmpegPath = getLinuxOrWindowsFfmpegPath();
            Runtime runtime = Runtime.getRuntime();
            try {
                String filePath = copyFile(inputStream, fileName);
                String substring = filePath.substring(0, filePath.lastIndexOf("."));
                String mp3FilePath = substring + ".mp3";
    
                //执行ffmpeg文件,将amr格式转为mp3
                //filePath ----> amr文件在临时文件夹中的地址
                //mp3FilePath  ----> 转换后的mp3文件地址
                Process p = runtime.exec(ffmpegPath + "ffmpeg -i" + " " +filePath + " " + mp3FilePath);//执行ffmpeg.exe,前面是ffmpeg.exe的地址,中间是需要转换的文件地址,后面是转换后的文件地址。-i是转换方式,意思是可编码解码,mp3编码方式采用的是libmp3lame
    
                //释放进程
                p.getOutputStream().close();
                p.getInputStream().close();
                p.getErrorStream().close();
                p.waitFor();
    
                File file = new File(mp3FilePath);
                InputStream fileInputStream = new FileInputStream(file);
    
                //应该在调用该方法的地方关闭该input流(使用完后),并且要删除掉临时文件夹下的相应文件
                /*File amrFile = new File(filePath);
                File mp3File = new File(mp3FilePath);
                if (amrFile.exists()) {
                    boolean delete = amrFile.delete();
                    System.out.println("删除源文件:"+delete);
                }
                if (mp3File.exists()) {
                    boolean delete = mp3File.delete();
                    System.out.println("删除mp3文件:"+delete);
                }*/
                return fileInputStream;
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                runtime.freeMemory();
            }
            return null;
        }
    
        /**
         * 将用户输入的amr音频文件流转为音频文件并存入临时文件夹中
         * @param inputStream  输入流
         * @param fileName  文件姓名
         * @return  amr临时文件存放地址
         * @throws IOException
         */
        private static String copyFile(InputStream inputStream, String fileName) throws IOException {
            Properties props = System.getProperties();
            String filePath = props.getProperty("user.home") + File.separator + "MP3TempFile"; //创建临时目录
            File dir = new File(filePath);
            if (!dir.exists()) {
                dir.mkdir();
            }
    
            String outPutFile = dir + File.separator + fileName;
    
            OutputStream outputStream = new FileOutputStream(outPutFile);
            int bytesRead;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
    
            return outPutFile;
        }
    
        /**
         * 判断系统是Windows还是linux并且拼接ffmpegPath
         * @return
         */
        private static String getLinuxOrWindowsFfmpegPath() {
            String ffmpegPath = "";
            String osName = System.getProperties().getProperty("os.name");
            if (osName.toLowerCase().indexOf("linux") >= 0) {
                ffmpegPath = "";
            } else {
                URL url = Thread.currentThread().getContextClassLoader().getResource("ffmpeg/windows/");
                if (url != null) {
                    ffmpegPath = url.getFile();
                }
            }
            return ffmpegPath;
        }
    }

          

      

  • 相关阅读:
    mybatis 错误 Invalid bound statement (not found)
    Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
    bug 记录 Unable to start ServletWebServerApplicationContext due to multiple ServletWebServerFactory beans
    解决:The Tomcat connector configured to listen on port 8182 failed to start. The port may already be in use or the connector may be misconfigured.
    jquery validate 验证插件 解决多个相同的Name 只验证第一个的方案
    phpStorm+xdebug调试(php7.3)
    小程序视频多个视频播放与暂停
    CSS实现单行、多行文本溢出显示省略号(…)
    Packet for query is too large (4,544,730 > 4,194,304). You can change this value on the server by setting the 'max_allowed_packet' variable.
    idea自动在文件头中添加作者和创建时间
  • 原文地址:https://www.cnblogs.com/Amaris-Lin/p/9186888.html
Copyright © 2011-2022 走看看