首页   >   心得技巧

利用ffmpeg实现微信语音文件.amr格式与.mp3格式互相转换

微信的语音文件都是amr格式的,直接是打不开的,如何把微信语音信息的.amr文件转为.mp3文件?微信里面有一些比较重要的语音文件,想要备份下来,但是又不能直观的打开,必须借助格式工厂之类的软件才可以,但是作为一名java开发人员,还是想方设法用代码来解决一下。

要实现这个功能,可以利用FFmpeg(http://ffmpeg.org),有一堆很叼的介绍:

FFmpeg是领先的多媒体框架,能够解码,编码, 转码,复用,解复用,流式传输,过滤和播放人类和机器创建的任何内容。它支持最晦涩的古代格式,直至最前沿。无论它们是由某些标准委员会,社区还是公司设计的。它还具有高度可移植性:FFmpeg 在各种构建环境,机器架构和配置下编译,运行并通过Linux,Mac OS X,Microsoft Windows,BSD,Solaris等测试基础架构 FATE。

利用ffmpeg实现微信语音文件.amr格式与.mp3格式互相转换

1、安装gcc ffmpeg

[root@JD local]# wget http://downloads.sourceforge.net/lame/lame-3.100.tar.gz

[root@JD local]# tar -zxvf lame-3.100.tar.gz

[root@JD local]# cd lame-3.100

[root@JD local]# ./configure --prefix=/usr/local

[root@JD local]# make && make install

[root@JD local]# ln -s /usr/local/lib/libmp3lame.so.0.0.0 /usr/lib64/libmp3lame.so.0

lame装完了之后,就可以来安装ffmpeg了

[root@JD local]# wget http://ffmpeg.org/releases/ffmpeg-3.4.1.tar.bz2

[root@JD local]# tar -jxvf ffmpeg-3.4.1.tar.bz2

[root@JD local]# cd ffmpeg-3.4.1

[root@JD local]# ./configure --prefix=/usr/local --pkg-config-flags=--static --enable-libmp3lame --disable-x86asm

[root@JD local]# make && make install

2、安装silk-v3-decoder

[root@JD local]# wget https://github.com/kn007/silk-v3-decoder/archive/master.zip

[root@JD local]# unzip master.zip

[root@JD local]# chmod -R +x /usr/local/silk-v3-decoder-master/

下载、解压、授权之后,就可以运行脚本来进行转换amr文件了

[root@JD local]# cd silk-v3-decoder

[root@JD silk-v3-decoder]# ./converter.sh <微信amr文章文件路径名> mp3

Tips:最后一个参数,非必填,上面的脚本效果等同于 ./converter.sh <微信amr文章文件路径名>

3、改造一个encoder脚本,decoder已经有了不用改造了

converter-encoder.sh(请拷贝以下脚本放到silk-v3-decoder-master目录下)

#!/bin/bash

# File: converter.sh

# Date: August 19th, 2016

# Time: 18:56:52 +0800

# Author: kn007 <kn007@126.com>

# Blog: https://kn007.net

# Link: https://github.com/kn007/silk-v3-encoder

# Usage: sh converter.sh silk_v3_file/input_folder output_format/output_folder flag(format)

# Flag: not define   ----  not define, convert a file

#       other value  ----  format, convert a folder, batch conversion support

# Requirement: gcc ffmpeg

# Colors

RED="$(tput setaf 1 2>/dev/null || echo '\e[0;31m')"

GREEN="$(tput setaf 2 2>/dev/null || echo '\e[0;32m')"

YELLOW="$(tput setaf 3 2>/dev/null || echo '\e[0;33m')"

WHITE="$(tput setaf 7 2>/dev/null || echo '\e[0;37m')"

RESET="$(tput sgr 0 2>/dev/null || echo '\e[0m')"

# Main

cur_dir=$(cd `dirname $0`; pwd)

if [ ! -r "$cur_dir/silk/encoder" ]; then

echo -e "${WHITE}[Notice]${RESET} Silk v3 Encoder is not found, compile it."

cd $cur_dir/silk

make && make encoder

[ ! -r "$cur_dir/silk/encoder" ]&&echo -e "${RED}[Error]${RESET} Silk v3 Encoder Compile False, Please Check Your System For GCC."&&exit

echo -e "${WHITE}========= Silk v3 Encoder Compile Finish =========${RESET}"

fi

cd $cur_dir

while [ $3 ]; do

[[ ! -z "$(pidof ffmpeg)" ]]&&echo -e "${RED}[Error]${RESET} ffmpeg is occupied by another application, please check it."&&exit

[ ! -d "$1" ]&&echo -e "${RED}[Error]${RESET} Input folder not found, please check it."&&exit

TOTAL=$(ls $1|wc -l)

[ ! -d "$2" ]&&mkdir "$2"&&echo -e "${WHITE}[Notice]${RESET} Output folder not found, create it."

[ ! -d "$2" ]&&echo -e "${RED}[Error]${RESET} Output folder could not be created, please check it."&&exit

CURRENT=0

echo -e "${WHITE}========= Batch Conversion Start ==========${RESET}"

ls $1 | while read line; do

let CURRENT+=1

ffmpeg -f s16le -ar 24000 -ac 1 -acodec pcm_s16le -i "$1/$line" "$2/$line.pcm" > /dev/null 2>&1 

$cur_dir/silk/encoder "$2/$line.pcm" "$2/${line%.*}.$3" -tencent > /dev/null 2>&1

rm "$2/$line.pcm"

[ ! -f "$2/${line%.*}.$3" ]&&echo -e "[$CURRENT/$TOTAL]${YELLOW}[Warning]${RESET} Convert $line false, maybe ffmpeg no format handler for $3."&&continue

echo -e "[$CURRENT/$TOTAL]${GREEN}[OK]${RESET} Convert $line To ${line%.*}.$3 Finish."

done

echo -e "${WHITE}========= Batch Conversion Finish =========${RESET}"

exit

done

ffmpeg -f s16le -ar 24000 -ac 1 -acodec pcm_s16le -i "$1" "$1.pcm" > /dev/null 2>&1

$cur_dir/silk/encoder "$1.pcm" "${1%.*}.amr" -tencent > /dev/null 2>&1

rm "$1.pcm"

[ ! -f "${1%.*}.amr" ]&&echo -e "${YELLOW}[Warning]${RESET} Convert $1 false, maybe ffmpeg no format handler for $2."&&exit

echo -e "${GREEN}[OK]${RESET} Convert $1 To ${1%.*}.$2 Finish."

exit

4、java调用脚本

package com.wolffy.jwcz;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Amr和Mp3音频文件互转
 * Created by Felix on 2019/06/20.
 */
public class Transfer {

    /**
     * silk-v3根路径
     */
    private static final String silkv3Path = "/usr/local/silk-v3-decoder-master/";

    /**
     * 将amr转成mp3(decoder)
     *
     * @param decoderPath encoder.sh脚本的路径
     * @param amrfilePath amr文件的路径
     */
    public static void converterAmrToMp3(String decoderPath, String amrfilePath) {
        if (StringUtils.isBlank(decoderPath)) {
            decoderPath = silkv3Path;
        }
        String decoderCmd = decoderPath + "converter.sh " + amrfilePath;
        exeCmd(decoderCmd);
    }

    /**
     * 将mp3转成amr(encoder)
     *
     * @param encoderPath encoder.sh脚本的路径
     * @param mp3filePath mp3文件的路径
     */
    public static void converterMp3ToAmr(String encoderPath, String mp3filePath) {
        if (StringUtils.isBlank(encoderPath)) {
            encoderPath = silkv3Path;
        }
        String encoderCmd = encoderPath + "converter-encoder.sh " + mp3filePath;
        exeCmd(encoderCmd);
    }

    /**
     * 执行linux命令
     *
     * @param commandStr 命令
     */
    private static void exeCmd(String commandStr) {
        BufferedReader br = null;
        try {
            Process p = Runtime.getRuntime().exec(commandStr);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            System.out.println(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {

        String amrfilePath = "/usr/local/1.amr";

        String mp3filePath = "/usr/local/3.mp3";

        String coderPath = silkv3Path;

        converterAmrToMp3(coderPath, amrfilePath);

        converterMp3ToAmr(coderPath, mp3filePath);

    }

}

搞定!!!(参考文章地址:http://www.wityx.com/post/824_1_1.html)

文中用到的几个工具,下载速度不算太快,我把它们打包上传到百度网盘,大家可以来网盘下载:

链接: https://pan.baidu.com/s/1aI8tZXVQS9moT9Vk7QB6Gg 

提取码: tqiy

QQ群: 686430774  /  718410762

站长Q: 1347384268

如果文章有帮到你,可以考虑请博主喝杯咖啡!

分享到:

欢迎分享本文,转载请注明出处!

作者:不忘初心

发布时间:2019-06-20

永久地址:https://www.jiweichengzhu.com/article/7f8f6b9ce88948f5b49d5eceecaf98b6

评论

相关阅读