Java调用百度云语音识别【音频转写】

2024-01-08 16:28:43

百度云文档  ttps://ai.baidu.com/ai-doc/SPEECH/Bk5difx01

依赖:
<!--JSONObject-->
<dependency>
	<groupId>org.json</groupId>
	<artifactId>json</artifactId>
	<version>20210307</version>
</dependency>

示例代码: 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.json.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * 语音识别
 */
@Slf4j
@Component
public class AasrUtils {


    public static final String API_KEY = "U92RRV****ag9xZv";
    public static final String SECRET_KEY = "SU05xD****0ziDkM";


    static final OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS) // Set the connection timeout
            .readTimeout(300, TimeUnit.SECONDS)    // Set the read timeout
            .build();


    /**
     * 音频转写
     *
     * @param speechUrl  音频url, 音频大小不超过500MB
     * @return
     * @throws IOException
     */
    public static String createAasr(String speechUrl) throws IOException {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\n" +
                "    \"speech_url\": \"" + speechUrl + "\",\n" +
                "    \"format\": \"mp3\",\n" +
                "    \"pid\": 80001,\n" +
                "    \"rate\": 16000\n" +
                "}");

        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rpc/2.0/aasr/v1/create?access_token=" + getAccessToken())
                .post(body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .build();

        try (Response response = HTTP_CLIENT.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseBody = response.body().string();
                log.info("创建AACR任务响应: {}", responseBody);

                String taskId = JSON.parseObject(responseBody).getString("task_id");
                return taskId;
            } else {
                log.info("创建AACR任务失败: {}", response.code());
                return null;
            }
        }
    }


    public static String checkASRStatus(String taskId) throws IOException {
        if (StringUtils.isEmpty(taskId)) {
            return "";
        }

        String taskStatus = "";

        while (!("Success".equals(taskStatus) || "Failed".equals(taskStatus))) {
            // Add a delay before the next query
            try {
                Thread.sleep(1000); // Sleep for 1 seconds (adjust as needed)
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            HashMap<String, Object> map = new HashMap<>();
            ArrayList<Object> list = new ArrayList<>();
            list.add(taskId);
            map.put("task_ids", list);

            JSONObject jsonObject = new JSONObject(map);
            String toJSON = jsonObject.toString();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, toJSON);
            Request request = new Request.Builder()
                    .url("https://aip.baidubce.com/rpc/2.0/aasr/v1/query?access_token=" + getAccessToken())
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "application/json")
                    .build();

            try (Response response = HTTP_CLIENT.newCall(request).execute()) {
                if (response.isSuccessful()) {
                    String responseBody = response.body().string();
                    log.info("创建AACR任务响应: {}", responseBody);

                    JSONArray tasksInfo = JSON.parseObject(responseBody).getJSONArray("tasks_info");

                    if (tasksInfo.size() > 0) {
                        com.alibaba.fastjson.JSONObject taskInfo = tasksInfo.getJSONObject(0);
                        taskStatus = taskInfo.getString("task_status");

                        if ("Success".equals(taskStatus)) {
                            String resultText = taskInfo.getJSONObject("task_result").getString("result");
                            // 使用正则表达式替换[""]
                            resultText = resultText.replaceAll("\\[\"|\"\\]", "");
                            log.info("语音转文字: {}", resultText);
                            return resultText;
                        } else if ("Failed".equals(taskStatus)) {
                            log.info("任务失败: {}", taskStatus);
                        }
                    } else {
                        log.info("未找到任务信息");
                    }
                } else {
                    log.info("检查任务状态失败: {}", response.code());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }


    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    static String getAccessToken() throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
                + "&client_secret=" + SECRET_KEY);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String access_token = new JSONObject(response.body().string()).getString("access_token");
        return access_token;
    }


    public static void main(String[] args) throws IOException {
        checkASRStatus(createAasr("https://openai-demo-1319322874.cos.na-siliconvalley.myqcloud.com/audio/2023-12-05/1701764504763878.mp3"));
        checkASRStatus(createAasr("https://aipe-speech.bj.bcebos.com/text_to_speech/2024-01-04/65966e5370172800014d6524/speech/0.mp3?authorization=bce-auth-v1%2F8a6ca9b78c124d89bb6bca18c6fc5944%2F2024-01-04T08%3A38%3A04Z%2F259200%2F%2F7ca69d814bacad356114a0ff7573734c5ceb20b363dee6c2e971b16617787ac4"));
    }

}

文章来源:https://blog.csdn.net/weixin_43652507/article/details/135398994
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。