Audio transcoding tool, mainly used to convert WeChat voice amr format to mp3 format so that it can be played in the audio tag of html5 1. Call the interface provided by WeChat to obtain the InputStream byte stream of the recording 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"); // Must be a get request http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000"); // Connection timeout 30 seconds System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // Read timeout 30 seconds http.connect(); // Get the file and convert it into byte stream is = http.getInputStream(); } catch (Exception e) { e.printStackTrace(); } return is; } 2. Save the obtained byte stream as an amr file public String downloadMediaId(HttpServletRequest request, String mediaId) { String relfilePath = null; InputStream inputStream = getInputStream(mediaId); FileOutputStream fileOutputStream = null; try { //Server resource save path 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)) != -1) { // Determine whether the result is wrong 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; } 3. Convert the saved amr file to mp3 file public void amrToMp3(String sourcePath, String targetPath) { File source = new File(sourcePath); File target = new File(targetPath); AudioUtils.amrToMp3(source, target); } 4. Required jar package dependencies <!--amr file to audio map file--> <dependency> <groupId>com.github.dadiyang</groupId> <artifactId>jave</artifactId> <version>1.0.3</version> </dependency> Audio transcoding tools It supports Linux/Windows/Mac platforms because it is based on the modification of the JAVE project, and JAVE relies on ffmpeg, so it can be applied to the conversion of all file formats supported by ffmpeg. For details, please refer to the JAVE official documentation principleDuring initialization, determine the current operating environment, copy the corresponding ffmpeg executable file in the bin directory to the temporary directory, and execute the corresponding transcoding command of ffmpeg through Runtime.getRuntime().exec(cmd) according to the file type and configuration. Problems with the JAVE Projectffmpeg depends on the operating environment. The JAVE project encapsulates ffmpeg. Through the above principles, it enables java to call ffmpeg and supports cross-platform.
Features of this projectThis project was created to solve the above problems.
Extensions If the program cannot obtain the ffmpeg executable file by copying the resource file or the built-in ffmpeg does not support your operating system, you can specify the directory of available ffmpeg files installed in your system through environment variables or by setting System.setProperty("ffmpeg.home", "directory where the ffmpeg executable file is located") in Java. Such as System.setProperty("ffmpeg.home", "/usr/local/bin/") This is the end of this article about the playback and preservation of WeChat public account recording files (converting amr files to mp3). For more relevant WeChat public account recording content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Tutorial on installing the unpacked version of mysql5.7 on CentOS 7
>>: Tutorial on installing Tomcat server under Windows
Clickhouse Introduction ClickHouse is a column-or...
Table of contents rc.local method chkconfig metho...
1. INSERT INTO SELECT statement The statement for...
Apache Log4j2 reported a nuclear-level vulnerabil...
Table of contents Exporting Docker containers Imp...
Definition of Float Sets the element out of the n...
1. Use floating method Effect picture: The code i...
Mysqldump is used for logical backup in MySQL. Al...
Table of contents Constructor new Operator Implem...
Table of contents Introduction: Installation of e...
Table of contents Join syntax: 1. InnerJOIN: (Inn...
The operating environment of this tutorial: Windo...
<br />Preface: Before reading this tutorial,...
mysql copies the files in the data directory to r...
Table of contents Preface 1. Image Optimization 2...