使用Java代碼修改音頻文件比特率(碼率)


在公司上班的時候,領導要求將上傳的文件比特率統一修改為128kpbs,查找了好多資料都不行,最后使出了畢生功力解決這個問題。

由於使用同步的方式上傳太慢,用戶體驗度太差,所以使用了異步的方式。

見公式(百度上到處都是,但是會報錯,各位大佬只是把自己的代碼粘貼上來,都沒給解決問題)

java.lang.IllegalArgumentException

/**
*inputPath:需要處理的文件地址
*outputPath:處理后要輸出的文件地址
*/
public void Mp3Test(String inputPath,String outputPath){
       AudioAttributes audio = new AudioAttributes();
       audio.setCodec("libmp3lame");
       audio.setBitRate(new Integer(128000));//設置比特率
       audio.setSamplingRate(new Integer(44100));//設置采樣率
       audio.setChannels(new Integer(2));//設置音頻通道數
       EncodingAttributes attrs = new EncodingAttributes();
       attrs.setFormat("mp3");//設置格式,我的文件原本就是mp3格式的
       attrs.setAudioAttributes(audio);
       //attrs.setDuration(360f); // 設置截取的時長
       Encoder encoder = new Encoder();
       File inputFile = new File(inputPath);
       File outputFile = new File(outputPath);
       encoder.encode(inputFile,outputFile, attrs);
}

  自己經過幾天的百度,以及查看jave官方問題,最終解決報錯如何處理,將最后一個方法(encoder.encoder()方法)try-catch一下(

} catch (IllegalArgumentException e) {
e.printStackTrace();


 

有的時候頁面傳過來的是MultipartFile類型,我們需將MultipartFile類型轉換成File類型文件

public static File multipartFileToFile(MultipartFile file) throws Exception {

        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    //獲取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  重點都在這里了,順便說一下異步吧,公司使用的是SpringBoot框架,Spring Boot框架異步處理方式只需要在啟動類添加@EnableAsync注解,然后在異步方法上使用@Async注解就可以了,但是在我的項目中,只要使用@EnableAsync注解,整個項目都無法啟動(想了好久不知道為啥。。。。),最后只有使用線程的方式

@Component
public class AsyncTask {

    private ExecutorService executor = Executors.newCachedThreadPool() ;

    public void fun(String inputPath, String outputPath){
        executor.submit(new Runnable() {
            @Override
            public void run() {
                //修改音頻文件比特率
                AudioAttributes audio = new AudioAttributes();
                audio.setCodec("libmp3lame");
                audio.setBitRate(new Integer(128000));//設置比特率
                audio.setSamplingRate(new Integer(44100));
                audio.setChannels(new Integer(2));
                EncodingAttributes attrs = new EncodingAttributes();
                attrs.setFormat("mp3");//設置格式,我的文件原本就是mp3格式的
                attrs.setAudioAttributes(audio);
                //attrs.setDuration(360f); // 設置截取的時長
                Encoder encoder = new Encoder();
                File inputFile = new File(inputPath);
                File outputFile = new File(outputPath);
                try {
                    encoder.encode(inputFile,outputFile, attrs);//就是這個地方搞了好久
                    MP3File mp3File = new MP3File(outputFile);
                    System.out.println(mp3File.getMP3AudioHeader().getBitRate());//輸出轉換后的文件比特率
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InputFormatException e) {
                    e.printStackTrace();
                } catch (EncoderException e) {
                    e.printStackTrace();
                } catch (ReadOnlyFileException e) {
                    e.printStackTrace();
                } catch (TagException e) {
                    e.printStackTrace();
                } catch (InvalidAudioFrameException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

還有個問題就是當上傳的音頻文件超過10M的時候,可以正常上傳成功,但是無法轉換比特率,會報一個NA問題,百度了好久也是沒有解決,上班好累啊。。抓狂。。。

這個問題使用的是jave解決的,原本想直接在pom文件中添加坐標自己下載jar包,但是pom下載不下來,最后使用的是引入第三方jar的反射。引入第三方jar在本地可以使用,當打包發到生產環境的時候啟動就會報錯了,這個時候就需要引入jar包的同時修改pom文件。

修改pom文件的時候有兩個地方需要注意

1.雖然使用的是引入第三方jar的方式,但還是需要在pom文件中添加坐標,同時還需要使用scope和systemPath標簽

 

 

<dependency>
	<groupId>it.sauronsoftware</groupId>
	<artifactId>jave</artifactId>
	<version>1.0.2</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/src/main/resources/lib/jave-1.0.2.jar</systemPath>
</dependency>

2.在build標簽中需要使用configuration標簽

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
				<includeSystemScope>true</includeSystemScope>
			</configuration>
		</plugin>
	<plugins>
<build>

  

 


免責聲明!

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



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