您当前的位置: 首页 > 知识百科 > 怎么开发将录音转化为文字的微信小程序?

怎么开发将录音转化为文字的微信小程序?

时间:2023-07-01 14:05 阅读数:87 人阅读 分类:知识百科

  相信足够了解小程序功能的人都知道微信小程序是可以录音的,但是有时候保存下来的文件并不能顺利播放,想要获取准确信息,就要将小程序语音转化为文字,那么将录音转化为文字的微信小程序怎么开发呢?以下是步骤。

  1、上传silk文件

  2、下载silk-v3-decoder,通过名称把silk转换成讯飞可识别的wav文件

  3、获取讯飞转换后的文字信息

  

  public static final String FFMPEG = " export PATH=/usr/local/ffmpeg/bin/:$PATH; source /etc/profile;";

  public static final String CONVERTER = " sh /home/deployer/silk-v3-decoder-master/converter.sh ";

  public static final String OXM_VOICE_UPLOAD_BASE_PTAH = "/home/deployer/tomcat7/webapps/xxxx/";

  用到了讯飞的jar:json-jena-1.0-1.0.jar、Msc-1.0.jar

  讯飞的插件:libmsc32.so、libmsc64.so、msc32.dll、msc64.dll,插件我全部放在了工程的根目录下

  后来又要求文字需要转换成,一个痛苦的过程啊,各种文件格式转换

  [java] view plain copyimport java.io.IOException;

  import java.io.InputStreamReader;

  import java.util.Date;

  import java.util.Properties;

  public class ShellExec {

  public static void execShell(String command) {

  InputStreamReader stdISR = null;

  InputStreamReader errISR = null;

  Process process = null;

  // String command = "/home/Lance/workspace/someTest/testbash.sh";

  long timeout = 10 * 1000;

  try {

  process = Runtime.getRuntime().exec(command);

  // CommandStreamGobbler errorGobbler = new CommandStreamGobbler(

  // process.getErrorStream(), command, "ERR");

  // CommandStreamGobbler outputGobbler = new CommandStreamGobbler(

  // process.getInputStream(), command, "STD");

  // errorGobbler.start();

  // 必须先等待错误输出ready再建立标准输出

  // while (!errorGobbler.isReady()) {

  // Thread.sleep(10);

  // }

  // outputGobbler.start();

  // while (!outputGobbler.isReady()) {

  // Thread.sleep(10);

  // }

  CommandWaitForThread commandThread = new CommandWaitForThread(

  process);

  commandThread.start();

  long commandTime = new Date().getTime();

  long nowTime = new Date().getTime();

  boolean timeoutFlag = false;

  while (!commandThread.isFinish()) {

  if (nowTime - commandTime > timeout) {

  timeoutFlag = true;

  break;

  } else {

  Thread.sleep(1000);

  nowTime = new Date().getTime();

  }

  }

  if (timeoutFlag) {

  // 命令超时

  // errorGobbler.setTimeout(1);

  // outputGobbler.setTimeout(1);

  System.out.println("正式执行命令:" + command + "超时");

  }

  // while (true) {

  // if (errorGobbler.isReadFinish() && outputGobbler.isReadFinish()) {

  // break;

  // }

  // Thread.sleep(10);

  // }

  } catch (IOException | InterruptedException e) {

  e.printStackTrace();

  } finally {

  if (process != null) {

  process.destroy();

  }

  }

  }

  public static void convertAudio(String sourcePath,int sourceHZ,String targetPath,int targetHZ){//ffmpeg参数设置的坑好大啊

  Properties props=System.getProperties(); //获得系统属*集

  String osName = props.getProperty("os.name"); //操作系统名称

  String command = null;

  if(osName.contains("Windows")){

  // ffmpeg -y -f s16le -ar 24000 -ac 1 -i "$1.pcm" -f wav -ar 16000 -b:a 16 -ac 1 "${1%.*}.$2"

  command = "C:ffmpeg.exe -y -f s16le -ar "+sourceHZ+" -i "+sourcePath+" -f wav -ar "+targetHZ+" -b:a 8 -ac 1 "+targetPath;

  }else{

  command = "/usr/local/ffmpeg/bin/ffmpeg -y -f s16le -ar "+sourceHZ+" -ac 1 -i "+sourcePath+" -f wav -ar "+targetHZ+" -b:a 8 -ac 1 "+targetPath;

  }

  System.out.println("格式转换:"+command);

  ShellExec.execShell(command);

  }

  public static void pcmToSilk(String pcmPath,String silkPath) throws InterruptedException{

  //首先 pcm转换成8000的wav,然后wav转成silk

  // ShellExec.convertAudio(pcmPath, 16000, pcmPath+".wav", 16000);

  // Thread.sleep(1000);

  // ShellExec.convertAudio(pcmPath+".wav", 16000, silkPath, 8000);

  ShellExec.convertAudio(pcmPath, 16000, silkPath, 8000);

  }

  public static void pcmToWav(String pcmPath,String wavPath){

  Properties props=System.getProperties(); //获得系统属*集

  String osName = props.getProperty("os.name"); //操作系统名称

  String command = null;

  if(osName.contains("Windows")){

  command = "C:ffmpeg.exe -f s16le -ar 16000 -i "+pcmPath+" -f wav -ar 16000 -b:a 8 -ac 1 "+wavPath;

  }else{

  command = "/usr/local/ffmpeg/bin/ffmpeg -f s16le -ar 16000 -i "+pcmPath+" -f wav -ar 16000 -b:a 16 -ac 1 "+wavPath;

  }

  System.out.println("格式转换:"+command);

  ShellExec.execShell(command);

  }

  public static void silkToWav(String silkPath,String wavPath){

  Properties props=System.getProperties(); //获得系统属*集

  String osName = props.getProperty("os.name"); //操作系统名称

  String command = null;

  if(osName.contains("Windows")){

  // ffmpeg -y -f s16le -ar 24000 -ac 1 -i "$1.pcm" -f wav -ar 16000 -b:a 16 -ac 1 "${1%.*}.$2"

  command = "C:ffmpeg.exe -y -f s16le -ar 8000 -ac 1 -i "+silkPath+" -f wav -ar 16000 -b:a 16 -ac 1 "+wavPath;

  }else{

  command = "/usr/local/ffmpeg/bin/ffmpeg -y -f s16le -ar 8000 -ac 1 -i "+silkPath+" -f wav -ar 16000 -b:a 16 -ac 1 "+wavPath;

  }

  System.out.println("格式转换:"+command);

  ShellExec.execShell(command);

  }

  public static void main(String[] args) throws Exception{

  ShellExec.pcmToSilk("/home/deployer/tomcat7/webapps/omoFile/gc3e72fbe-1a27-479f-ae4f-d3aacc6276ed.pcm", "/home/deployer/tomcat7/webapps/omoFile/gc3e72fbe-1a27-479f-ae4f-d3aacc6276ed.pcm.silk");

  // ShellExec.pcmToWav("/home/deployer/tomcat7/webapps/omoFile/gdd68fe07-35b3-46f1-af08-97100caba179.pcm", "/home/deployer/tomcat7/webapps/omoFile/gdd68fe07-35b3-46f1-af08-97100caba179.pcm.wav");

  }

  参照以上的流程和相关代码就可以开发出将录音转化为文字的微信小程序了,大家都学会了吗?将代码复制到小程序开发系统中,应该很容易就能成功,更多相关资料请关注微信小程序商店。

  

  微信小程序电话录音功能怎么弄?

  微信小程序录音界面怎么修改?

  微信小程序录音播放怎么弄?