利用百度AI及ffmpeg进行英语媒体自动转译

1 篇文章 0 订阅

准备工作:

1.首先在百度注册账号(https://ai.baidu.com),并申请创建一个asr应用,这样你才有appid ,api_key, secret_ke

2.下载安装ffmpeg ,下载网址http://ffmpeg.org/download.html

特别注意要下载安装包,里面有3个exe文件,ffmpeg.exe,ffplay.exe,ffprobe.exe,记住不要少,要不然会报错。

最好配置一下ffmpeg环境变量,要不然需要再代码里手动指定路径。

3.pip安装pydub,ffmpeg-python,baidu-aip

思路:

使用ffmpeg把文件切割成小于60秒的片段,然后用百度asr进行转译。

百度AI特别要注意音频格式,必须采样频率16000,单声道,否则识别不了,我就走了这个弯路,大家可以用格式工厂进行转换,也可以用ffmpeg转换。WAV 效果比MP3要好些。

注意:注意不同音频语言要用不同dev_pid,百度开发文档里面有。

示例

下面这段代码将把一个名为bush.mp3的文件根据里面演讲语音的停顿情况,用split_on_silence进行分割,同时对第一个片段进行了转译,如果要转译所有片段,

可以使用多线程来加快转译速度(注意百度QPS限制,否则会报错,免费用户QPS是2)

里面有不少print是为了调试方便大家自己修改。

下面这行代码的参数,大家要根据自己音频特征进行调节。

chunks = split_on_silence( clip, min_silence_len=1000, silence_thresh=-25, keep_silence=100 

大家重点要注意下参数,设置非常关键,因为自己在做智能字幕,一直存在一个问题,就是切割后的长度比原来小,导致字幕不同步,后来仔细看了github上的代码和说明,沉默部分被切掉了,可以用detect_silence,检测补偿,或者自己修改函数。

(别忘记from pydub.silence import detect_silence

"""
audio_segment - original pydub.AudioSegment() object
min_silence_len - (in ms) minimum length of a silence to be used for
    a split. default: 1000ms
silence_thresh - (in dBFS) anything quieter than this will be
    considered silence. default: -16dBFS
keep_silence - (in ms or True/False) leave some silence at the beginning
    and end of the chunks. Keeps the sound from sounding like it
    is abruptly cut off.
    When the length of the silence is less than the keep_silence duration
    it is split evenly between the preceding and following non-silent
    segments.
    If True is specified, all the silence is kept, if False none is kept.
    default: 100ms
"""

最后上代码:

from pydub import AudioSegment
from pydub.silence import split_on_silence
from aip import AipSpeech
import os


def baidu_asr(file_content):
    """ 有兴趣可以看下这篇文章https://www.jianshu.com/p/b93c18f69dbd"""
    app_id = '你的app_id'
    api_key = '你的api_key'
    secret_key = '你的secret_key'
    client = AipSpeech(app_id, api_key, secret_key)
    # 普通话示例
    # print(client.asr(file_content, 'wav', 16000, {'dev_pid': 1536, }))
    # 英文示例
    res_dict = client.asr(file_content, 'wav', 16000, {'dev_pid': 1737, })
    return res_dict['result'][0]


def cut_file_by_ffmpeg(filename):
    sound = AudioSegment.from_mp3(filename)
    clip = sound

    # "graph" the volume in 1 second increments
    # for x in range(0, int(len(clip) / 1000)):
    #     print(x, clip[x * 1000:(x + 1) * 1000].max_dBFS)

    print("start cut file by split_on_silence:")
    chunks = split_on_silence(
        clip,
        min_silence_len=1000,
        silence_thresh=-30,
        keep_silence=100
    )
    print("number of chunks", len(chunks))
    print(chunks)
    chunks_path = './chunks/'
    if not os.path.exists(chunks_path):
        os.mkdir(chunks_path)
    # 保存所有分段
    print('开始保存')
    for i in range(len(chunks)):
        new = chunks[i]
        save_name = chunks_path + '%04d.%s' % (i, 'wav')
        new.export(save_name, format='wav')
        print('%04d' % i, len(new))
    print('保存完毕')
    print(baidu_asr(chunks[0].raw_data))


cut_file_by_ffmpeg('bush.mp3')

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值