完美解决个人微信音频amr文件与mp3格式互转

完美解决个人微信音频amr文件与mp3格式互转
1、安装gcc ffmpeg

wget http://downloads.sourceforge.net/lame/lame-3.100.tar.gz
tar -zxvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix=/usr/local
make && make install
ln -s /usr/local/lib/libmp3lame.so.0.0.0 /usr/lib64/libmp3lame.so.0
wget http://ffmpeg.org/releases/ffmpeg-3.4.1.tar.bz2
tar -jxvf ffmpeg-3.4.1.tar.bz2
cd ffmpeg-3.4.1
./configure --prefix=/usr/local --pkg-config-flags=--static --enable-libmp3lame --disable-x86asm
make && make install


2、安装/silk-v3-decoder
 

cd /usr/local/soft
wget https://github.com/kn007/silk-v3-decoder/archive/master.zip
unzip master.zip
chmod -R +x /usr/local/soft/silk-v3-decoder-master/
silk-v3-decoder/converter.sh 微信amr文章文件路径名 mp3


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 -i "$1/$line" -f s16le -ar 24000 -ac 1 -acodec pcm_s16le "$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 -i "$1" -f s16le -ar 24000 -ac 1 -acodec pcm_s16le "$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 amr."&&exit
echo -e "${GREEN}[OK]${RESET} Convert $1 To ${1%.*}.amr Finish."
exit


4、用java调用脚本
 

private static String silkv3Path="/usr/local/soft/silk-v3-decoder-master/";

/**
* 将amr转成mp3
* @param coderPath
* @param amrfilePath
*/
public static void converterDecoderMp3(String coderPath, String amrfilePath) {
if(StringUtils.isBlank(coderPath)){
coderPath = silkv3Path;
}
String decoderCmd = coderPath + "converter.sh " + amrfilePath;
exeCmd(decoderCmd);
}

/**
* 将mp3转成amr
* @param coderPath
* @param mp3filePath
*/
public static void converterEncoderAmr(String coderPath, String mp3filePath) {
if(StringUtils.isBlank(coderPath)){
coderPath = silkv3Path;
}
String encoderCmd= coderPath + "converter-encoder.sh " + mp3filePath;
exeCmd(encoderCmd);
}

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 = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\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 = "/home/1.amr";
String mp3filePath = "/home/3.mp3";
String coderPath = silkv3Path;
converterDecoderMp3(coderPath, mp3filePath);
converterEncoderAmr(coderPath, amrfilePath);
}


完美解决个人微信音频amr文件与mp3格式互转