Audio transcoding tool, mainly used to convert wechat voice AMR format to MP3 format in order to play in HTML5 audio tag

Public InputStream getInputStream(String mediaId) {InputStream is = null; try { String URL_DOWNLOAD_TEMP_MEDIA = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID"; String url = url_download_temp_media. replace("ACCESS_TOKEN", "write your own code to get accessToken").replace("MEDIA_ID", mediaId); URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod("GET"); HTTP. SetRequestProperty (" content-type ", "application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000"); / / connection timeout System. 30 seconds setProperty (" sun.net.client.defaultReadTimeout ", "30000"); // Read timeout is 30 seconds http.connect(); Is = http.getinputStream (); } catch (Exception e) { e.printStackTrace(); } return is; }Copy the code
Public String downloadMediaId(HttpServletRequest Request, HttpServletRequest Request, String mediaId) { String relfilePath = null; InputStream inputStream = getInputStream(mediaId); FileOutputStream fileOutputStream = null; String savePath = request.getSession().getServletContext().getrealPath ("/") + "upload/" + DateUtil.getYear() + "/wxmedia/audio/"; savePath = savePath + "audio/"; String filename = String.valueOf(System.currentTimeMillis()) + ".amr"; relfilePath = "upload/" + DateUtil.getYear() + "/wxmedia/audio/" + filename; File file = new File(savePath); if (! file.exists()) { file.mkdirs(); } byte[] data = new byte[1024]; int len = 0; fileOutputStream = new FileOutputStream(savePath + filename); while ((len = inputStream.read(data)) ! If (new String(data).indexof ("errmsg") > -1) {return null; } fileOutputStream.write(data, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream ! = null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream ! = null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return relfilePath; }Copy the code

3. Convert the saved AMR file to an MP3 file

public void amrToMp3(String sourcePath, String targetPath) {
    File source = new File(sourcePath);
    File target = new File(targetPath);
    AudioUtils.amrToMp3(source, target);
}
Copy the code

4. Required JAR package dependencies

<! <groupId> <artifactId>jave</artifactId> </artifactId> The < version > 1.0.3 < / version > < / dependency >Copy the code

Audio transcoding tool

Supports Linux, Windows, and Mac platforms

Since this is a modification based on the JAVE project, and JAVE relies on FFMPEG, it can be applied to all the file formats supported by FFMPEG. For details, see the JAVE official document

The principle of

  1. When initialization, determine the current running environment and copy the corresponding FFmpeg executable file in the bin directory to the temporary directory
  2. Run runtime.geTruntime ().exec(CMD) to transcode ffmpeg based on the file type and configuration

Problems with the JAVE project

Ffmpeg is runtime dependent. The JAVE project encapsulates FFMPEG by using the above principles to enable Java to call FFMPEG and support it across platforms.

  1. The project is old and no longer maintained. The most recent version of the website was released in 2009, and it relies on ffMPEG, which is obsolete and in many cases unusable.
  2. EncoderException: Stream mapping
  3. No Maven repository has been released, and JAVE itself is not a Maven project
  4. Does not support MAC

Features of this project

This project is designed to solve these problems.

  • This is a Maven project and has been published to a central repository.
  • The ffMPEG executable that the project depends on is verified to be available (a simple way to verify this is provided in the unit tests)
  • Fixed EncoderException: Stream mapping for amR to MP3
  • Supports Linux, Windows, and Mac platforms

extension

If the program cannot get the FFMPEG executable by copying the resource file or the built-in FFMPEG does not support the operating system you are using

You can specify the directory of available FFmpeg files installed on your System by setting system.setProperty (“ffmpeg. Home “, “ffmpeg executable directory “) in Java

Such as System. SetProperty (” ffmpeg. Home “, “/ usr/local/bin/”)