商城系統(tǒng) 注冊

小程序錄音文件上傳與訊飛語音識(shí)別

2020-09-27|HiShop
導(dǎo)讀:package com.example.service.impl; import com.example.service.XunFeiService; import com.example.utils.FFMPEGUtil; import com.example.utils.SR2Words; import com.example.utils.SRTool; import org.springfr...

 

  package com.example.service.impl;

  import com.example.service.XunFeiService;

  import com.example.utils.FFMPEGUtil;

  import com.example.utils.SR2Words;

  import com.example.utils.SRTool;

  import org.springframework.stereotype.Service;

  import org.springframework.web.multipart.MultipartFile;

  import java.io.File;

  import java.io.IOException;

  import java.util.HashMap;

  import java.util.Map;

  import java.util.UUID;

  /**

  * Created by songzs on 2017/12/12.

  */

  @Service

  public class XunFeiServiceImpl implements XunFeiService {

  @Override

  public Map speechRecognition(MultipartFile multi) {

  Map map =new HashMap<>();

  UUID uuid = UUID.randomUUID();

  String path = "/home/workspace/audio";

  String fileName = uuid.toString()+".silk";

  //臨時(shí)silk文件

  String tempFile = "/home/workspace/audio/"+uuid.toString()+".silk";

  //中間過渡pcm文件

  String pcmFile = "/home/workspace/audio/"+uuid.toString()+".pcm";

  //可識(shí)別的wav文件

  String wavFile = "/home/workspace/audio/"+uuid.toString()+".wav";

  File file = new File(path,fileName);

  try {

  multi.transferTo(file);

  } catch (IOException e) {

  e.printStackTrace();

  }

  /*移除臨時(shí)silk文件首字節(jié)start*/

  //標(biāo)準(zhǔn)silk文件

  String silkFile = FFMPEGUtil.silk_remove_word(tempFile);

  /*移除臨時(shí)silk文件首字節(jié)end*/

  //silk文件轉(zhuǎn)換成pcm文件

  String silk2Pcm = FFMPEGUtil.silk2Pcm(silkFile, pcmFile);

  //pcm文件轉(zhuǎn)換成wav文件

  String pcm2Wav = FFMPEGUtil.pcm2Wav(silk2Pcm, wavFile);

  //訊飛語音識(shí)別接口識(shí)別wav音頻文件,轉(zhuǎn)成文字返回

  SRTool sr = new SRTool();

  String words = null;

  try {

  words = sr.voice2words(pcm2Wav);

  } catch (InterruptedException e) {

  e.printStackTrace();

  } catch (IOException e) {

  e.printStackTrace();

  }

  System.out.println("訊飛識(shí)別的語音結(jié)果:"+words);

  if("".equals(words)){

  System.out.println("訊飛識(shí)別的語音結(jié)果:null");

  map.put("status","error");

  map.put("content","對不起,請您在描述一遍!");

  return map;

  }

  String result = SR2Words.sr2words(words);

  System.out.println("訊飛識(shí)別的語音結(jié)果:"+result);

  map.put("status","success");

  map.put("content",result);

  return map;

  }

  }

電話咨詢 預(yù)約演示 0元開店