在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;
    }
}

 

 

      

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM