• SpringBoot项目(百度AI整合)——如何在Springboot中使用语音文件识别 & ffmpeg的安装和使用


    在这里插入图片描述

    前言

    前言:在实际使用中,经常要参考官方的案例,但有时候因为工具的不一样,比如idea 和 eclipse,普通项目和spring项目等的差别;还有时候因为水平有限,难以在散布于官方的各个文档读懂;还有些时候因为自己测试使用的demo的文件不符合官网要求。。。总是实现不了相关功能。

    本系列博客尝试结合官网案例,阐述百度 AI 开放平台里的组件使用方式,核心是如何在spring项目中快速上手应用。

    本文介绍如何在Springboot中使用语音文件识别 & ffmpeg的安装和使用

    在这里插入图片描述

    引出


    1.从官网demo到idea中使用;
    2.从idea中使用到springboot项目整合;
    3.ffmpeg的安装和使用初步;

    在这里插入图片描述

    一、如何使用语音识别

    1.官网的sdk

    https://ai.baidu.com/ai-doc/SPEECH/plbxfq24s

    有时候找参考文档看到这个,点击git后有时候又打不卡

    在这里插入图片描述

    https://github.com/Baidu-AIP/java-sdk

    打开GitHub其实有比较好的说明文档,但GitHub能否顺利打开又是不确定的

    在这里插入图片描述

    https://ai.baidu.com/sdk

    这里可以下载各种sdk资源

    在这里插入图片描述

    2.下载sdk

    下载压缩包,解压

    官方文档提供了eclipse的使用: 3.在Eclipse右键“工程 -> Properties -> Java Build Path -> Add JARs”。

    但我用idea比较顺手,eclipse是在不熟。

    在这里插入图片描述

    二、如何在idea中跑通demo

    在这里插入图片描述

    1.新建项目,新建lib文件夹

    用来存放刚刚下载后解压获得的jar包

    在这里插入图片描述

    复制粘贴到lib目录下

    在这里插入图片描述

    2.选中作为lib引入

    在这里插入图片描述

    点击确定,导入成功

    在这里插入图片描述

    3.拷贝官网的案例

    拷贝官网的案例,导入jar包里面的包

    在这里插入图片描述

    加入配置文件,日志相关

    在这里插入图片描述

    4.获得语音识别案例文件

    然后我发现语音识别需要的文件格式是pcm的格式,所以又找了工具去转其他格式的文件为pcm,后来发现每次调用总是出各种文件;最后经过一番波折,终于在官网的python案例中找到一个示例文件。。。。

    在这里插入图片描述

    中间各种bug,最后终于用找到的这个案例跑通

    在这里插入图片描述

    三、如何结合springboot使用

    jar包的理解

    在这里插入图片描述

    简易spring项目搭建总览

    在这里插入图片描述

    1.导入依赖

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>com.tianjugroupId>
        <artifactId>baidu-apiartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        properties>
    
        
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.6.13version>
        parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
    
            <dependency>
                <groupId>com.baidu.aipgroupId>
                <artifactId>java-sdkartifactId>
                <version>4.16.16version>
            dependency>
    
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>2.0.12version>
            dependency>
    
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
    
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.12version>
                <scope>testscope>
            dependency>
    
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    2.进行配置

    BaiduPro配置类

    package com.tianju.config.baidu;
    
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    /**
     * 专门用来获取配置文件里的值
     */
    @Component
    @ConfigurationProperties(prefix = "baidu")
    @PropertySource("classpath:config/baiduAip.properties")
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    
    public class BaiduPro {
        private String appId;
        private String apiKey;
        private String secretKey;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    配置类,放入容器中

    package com.tianju.config.baidu;
    
    import com.baidu.aip.speech.AipSpeech;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * 百度相关的配置文件
     */
    @Configuration
    public class BaiduConfig {
    
        @Autowired
        private BaiduPro baiduPro;
    
        /**
         * 语音相关 AipSpeech
         * @return AipSpeech放容器中
         */
        @Bean
        public AipSpeech aipSpeech(){
    
            // 初始化一个AipSpeech
            AipSpeech client = new AipSpeech(baiduPro.getAppId(), baiduPro.getApiKey(), baiduPro.getSecretKey());
    
            // 可选:设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            return client;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    3.controller层进行调用

    GET http://localhost:10050/api/baidu/hello

    package com.tianju.config.controller;
    
    import com.baidu.aip.speech.AipSpeech;
    import com.tianju.config.resp.HttpResp;
    
    import lombok.extern.slf4j.Slf4j;
    import org.json.JSONObject;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/api/baidu")
    
    @Slf4j
    public class BaiduApiController {
    
        @Autowired
        private AipSpeech aipSpeech;
    
        @GetMapping("/hello")
        public HttpResp hello(){
            JSONObject pcm = aipSpeech.asr(
                    "D:\\Myprogram\\springboot-workspace\\spring-project\\baidu-api\\src\\main\\resources\\static\\helloAipSpeech.pcm",
                    "pcm", 16000, null);
            log.debug("get response:"+pcm.toString());
            return HttpResp.success(pcm.toString());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    四、ffmpeg的使用简介

    在这里插入图片描述

    1.基础知识

    官网: https://ffmpeg.org/

    FFMPEG简介

    FFmpeg的名称来自MPEG视频编码标准,前面的“FF”代表“Fast Forward”,FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。可以轻易地实现多种视频格式之间的相互转换。
    FFmpeg的用户有Google,Facebook,Youtube,优酷,爱奇艺,土豆等

    基础知识

    1.容器/文件(Conainer/File):即特定格式的多媒体文件,比如mp4、flv、mkv等。

    2.媒体流(Stream):表示时间轴上的一段连续数据,如一段声音数据、一段视频数据或一段字幕数据,可以是压缩的,也可以是非压缩的,压缩的数据需要关联特定的编解码器。

    3.数据帧/数据包(Frame/Packet):通常,一个媒体流是由大量的数据帧组成的,对于压缩数据,帧对应着编解码器的最小处理单元,分属于不同媒体流的数据帧交错存储于容器之中。

    一般情况下:

    Frame对应压缩前的数据,Packet对应压缩后的数据。

    4.编解码器(Codec):以帧为单位实现压缩数据和原始数据之间的相互转换的

    5.复用(mux):把不同的流按照某种容器的规则放入容器,这种行为叫做复用(mux)

    6.解复用(mux):把不同的流从某种容器中解析出来,这种行为叫做解复用(demux)

    \7. 码率和帧率是视频文件的最重要的基本特征,对于他们的特有设置会决定视频质量。如果我们知道码率和时长那么可以很容易计算出输出文件的大小。

    8.帧率:帧率也叫帧频率,帧率是视频文件中每一秒的帧数,肉眼想看到连续移动图像至少需要15帧。

    9.码率:比特率(也叫码率,数据率)是一个确定整体视频/音频质量的参数,秒为单位处理的位数,码率和视频质量成正比,在视频文件中中比特率用bps来表达。

    2.安装和命令的使用

    在这里插入图片描述

    下载后,到指定位置解压,然后进行环境变量的配置

    在这里插入图片描述

    cmd命令测试是否成功

    在这里插入图片描述

    3.使用命令

    ffmpeg -i wjs.aac -acodec pcm_s16le -ar 44100 output.pcm
    ffmpeg -i wjs.aac -acodec pcm_s16le -f s16le -ac 2 -ar 16000 16k.pcm
    
    • 1
    • 2

    在这里插入图片描述

    ffmpeg -i wjs.aac -ss 00:00:10 -to 00:00:59 -f s16le -ar 16000 16.pcm
    
    • 1

    这是一个使用FFmpeg命令行工具的命令,用于将音频文件wjs.aac从第10秒到第59秒的部分提取出来,并将其转换为16位有符号的PCM格式,采样率为16000Hz,保存为16.pcm文件。

    具体参数解释如下:

    • -i wjs.aac:指定输入文件为wjs.aac。
    • -ss 00:00:10:指定从第10秒开始提取。
    • -to 00:00:59:指定提取到第59秒结束。
    • -f s16le:指定输出格式为16位有符号的PCM。
    • -ar 16000:指定输出音频的采样率为16000Hz。
    • 16.pcm:指定输出文件名为16.pcm。

    所以,这个命令的意思是提取wjs.aac文件中从第10秒到第59秒的音频部分,并将其转换为16位有符号的PCM格式,采样率为16000Hz,保存为16.pcm文件。

    在这里插入图片描述

    提取1分钟的pcm音频文件命令

    在这里插入图片描述


    总结

    1.从官网demo到idea中使用;
    2.从idea中使用到springboot项目整合;
    3.ffmpeg的安装和使用初步;

  • 相关阅读:
    【后端框架】MyBatis(3)
    使用Puppeteer进行数据抓取保存为JSON
    opencv-Canny 边缘检测
    Leetcode 1124. 表现良好的最长时间段
    png转gif怎么操作?png动图怎么制作
    代码随想录刷题记录 6 - 二叉树(1)
    u-boot命令2
    一些与“区别“相关的知识点
    Proteus8仿真:51单片机使用ULN2003A控制步进电机
    win10默认浏览器改不了怎么办,解决方法详解
  • 原文地址:https://blog.csdn.net/Pireley/article/details/133211336