目录
项目结构
准备音频
接口Demo
准备代码(完整修改后)
测试提取中文文字代码
结果
下载链接:
这是上周打算试试,提取视频文字之后,制作视频字幕,从而想用大模型来实现,基本的demo可以在讯飞对应官网找到,之后就是api对接即可
项目结构
准备音频
接口Demo
示例demo请点击 这里 下载。
准备代码(完整修改后)
Ifasrdemo.java
package cn.xfyun;import cn.hutool.json.JSONUtil;
import cn.xfyun.sign.LfasrSignature;
import cn.xfyun.utils.HttpUtil;
import com.google.gson.Gson;
import org.apache.commons.lang.StringEscapeUtils;import java.io.*;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.SignatureException;
import java.util.HashMap;public class Ifasrdemo {private static final String HOST = "https://raasr.xfyun.cn";private static String AUDIO_FILE_PATH;private static final String appid = "xxxxx";private static final String keySecret = "xxxxxxxx";private static final Gson gson = new Gson();static {try {AUDIO_FILE_PATH = Ifasrdemo.class.getResource("/").toURI().getPath() + "/audio/合成音频.wav";} catch (URISyntaxException e) {e.printStackTrace();}}public static void main(String[] args) throws Exception {String result = upload();String jsonStr = StringEscapeUtils.unescapeJavaScript(result);String orderId = String.valueOf(JSONUtil.getByPath(JSONUtil.parse(jsonStr), "content.orderId"));getResult(orderId);}private static String upload() throws SignatureException, FileNotFoundException {HashMap<String, Object> map = new HashMap<>(16);File audio = new File(AUDIO_FILE_PATH);String fileName = audio.getName();long fileSize = audio.length();map.put("appId", appid);map.put("fileSize", fileSize);map.put("fileName", fileName);map.put("duration", "200");LfasrSignature lfasrSignature = new LfasrSignature(appid, keySecret);map.put("signa", lfasrSignature.getSigna());map.put("ts", lfasrSignature.getTs());String paramString = HttpUtil.parseMapToPathParam(map);System.out.println("upload paramString:" + paramString);String url = HOST + "/v2/api/upload" + "?" + paramString;System.out.println("upload_url:" + url);String response = HttpUtil.iflyrecUpload(url, new FileInputStream(audio));System.out.println("upload response:" + response);return response;}private static String getResult(String orderId) throws SignatureException, InterruptedException, IOException {HashMap<String, Object> map = new HashMap<>(16);map.put("orderId", orderId);LfasrSignature lfasrSignature = new LfasrSignature(appid, keySecret);map.put("signa", lfasrSignature.getSigna());map.put("ts", lfasrSignature.getTs());map.put("appId", appid);map.put("resultType", "transfer,predict");String paramString = HttpUtil.parseMapToPathParam(map);String url = HOST + "/v2/api/getResult" + "?" + paramString;System.out.println("\nget_result_url:" + url);while (true) {String response = HttpUtil.iflyrecGet(url);JsonParse jsonParse = gson.fromJson(response, JsonParse.class);if (jsonParse.content.orderInfo.status == 4 || jsonParse.content.orderInfo.status == -1) {System.out.println("订单完成:" + response);write(response);return response;} else {System.out.println("进行中...,状态为:" + jsonParse.content.orderInfo.status);//建议使用回调的方式查询结果,查询接口有请求频率限制Thread.sleep(7000);}}}public static void write(String resp) throws IOException {// 使用 try-with-resources 确保资源被正确关闭try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/main/resources/output/test.txt"), "UTF-8"))) {String ss = resp;// 用 UTF-8 写入bw.write(ss);System.out.println("写入txt成功");} catch (IOException e) {e.printStackTrace();}}class JsonParse {Content content;}class Content {OrderInfo orderInfo;}class OrderInfo {Integer status;}
}
测试提取中文文字代码
ChineseTextExtractor.java
package cn.xfyun;import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class ChineseTextExtractor {public static void main(String[] args) {String inputFilePath = "src/main/resources/output/test.txt"; // 输入文件路径String outputFilePath = "src/main/resources/output/out.txt"; // 输出文件路径extractAndWriteChineseText(inputFilePath, outputFilePath);}public static void extractAndWriteChineseText(String inputFilePath, String outputFilePath) {Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+"); // 中文字符的正则表达式try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath));BufferedWriter bw = new BufferedWriter(new FileWriter(outputFilePath))) {String line;while ((line = br.readLine()) != null) {Matcher matcher = pattern.matcher(line);while (matcher.find()) {bw.write(matcher.group()); // 写入匹配的中文字符bw.newLine(); // 每个匹配项后面添加一个新行}}System.out.println("中文字符提取完成,已写入 " + outputFilePath);} catch (IOException e) {e.printStackTrace();}}
}
结果
这样就实现了文字的提取,但是我还是不满足,我想要的效果是直接做出字幕的效果,我只需要导入这个字幕文件就可以了,于是我发现了下面的这个神器
下载链接:
下载客户端-33字幕
真正意义上实现免费算力制作字幕,十分良心!
看图
看看那几天转换的效果,主要是需要在自己下载一个模型即可,460多M的模型就可以适用很多场景
如果实现人声伴奏分离:推荐这两个网站!
不免费:https://www.yinziai.com/personal
新用户注册就可以用,着急的话可以试试,效果也很好
免费:Vocal Remover and Isolation [AI]
再看看这个歌词字幕
是的,我最后要的就是这样的效果
@ky 最后吐槽一下,自己最近心肝十分易上火,动不动就生气,昨晚跟女朋友打游戏,还对她发火……,而且明明是自己的问题,简单点处理不就好了吗,事后十分后悔,所以今天早上被人制裁了,用伞把我胳膊上的皮给戳破了,像被蛇咬了一样吗,还有我们这学校真的多蛇,大小操场都能看到,就到期末了,需要赶紧复习了!