日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

录音转文字python实现

發(fā)布時(shí)間:2023/12/10 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 录音转文字python实现 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 前言
  • 具體過程
    • 1.訊飛開放平臺(tái)創(chuàng)建應(yīng)用
    • 2.代碼示例
    • 3.代碼說明
    • 4.測(cè)試結(jié)果


前言

今天朋友在群里面問有沒有錄音轉(zhuǎn)文字的免費(fèi)軟件?我就去網(wǎng)上搜了一下,發(fā)現(xiàn)網(wǎng)上大多數(shù)是超出一定時(shí)長就收費(fèi)的,而我朋友的音頻比較多,加起來有幾個(gè)小時(shí),于是我想著能不能調(diào)用接口,用python實(shí)現(xiàn)這個(gè)功能。

上圖是某在線平臺(tái)的音頻轉(zhuǎn)文字功能,可以看到超出時(shí)長收費(fèi)也是比較貴的。
因?yàn)橹傲私獾接嶏w主要做這方面,所以我選擇了訊飛的開發(fā)平臺(tái)。

具體過程

1.訊飛開放平臺(tái)創(chuàng)建應(yīng)用

進(jìn)入官網(wǎng)訊飛開放平臺(tái)
注冊(cè)登錄后點(diǎn)擊語音轉(zhuǎn)寫

再點(diǎn)擊控制臺(tái)創(chuàng)建應(yīng)用即可。
創(chuàng)建完后會(huì)得到APPID和密鑰(這個(gè)代碼要用)。

創(chuàng)建完應(yīng)用后我們可以先領(lǐng)取5小時(shí)體驗(yàn)包。

2.代碼示例

找到開發(fā)文檔,里面有代碼示例。


我用的是python示例,并按照需求改了一下代碼。

# -*- coding: utf-8 -*- # # author: yanmeng2 # # 非實(shí)時(shí)轉(zhuǎn)寫調(diào)用demoimport base64 import hashlib import hmac import json import os import time import reimport requestslfasr_host = 'http://raasr.xfyun.cn/api'# 請(qǐng)求的接口名 api_prepare = '/prepare' api_upload = '/upload' api_merge = '/merge' api_get_progress = '/getProgress' api_get_result = '/getResult' # 文件分片大小10M file_piece_sice = 10485760# ——————————————————轉(zhuǎn)寫可配置參數(shù)———————————————— # 參數(shù)可在官網(wǎng)界面(https://doc.xfyun.cn/rest_api/%E8%AF%AD%E9%9F%B3%E8%BD%AC%E5%86%99.html)查看,根據(jù)需求可自行在gene_params方法里添加修改 # 轉(zhuǎn)寫類型 lfasr_type = 0 # 是否開啟分詞 has_participle = 'false' has_seperate = 'true' # 多候選詞個(gè)數(shù) max_alternatives = 0 # 子用戶標(biāo)識(shí) suid = ''class SliceIdGenerator:"""slice id生成器"""def __init__(self):self.__ch = 'aaaaaaaaa`'def getNextSliceId(self):ch = self.__chj = len(ch) - 1while j >= 0:cj = ch[j]if cj != 'z':ch = ch[:j] + chr(ord(cj) + 1) + ch[j + 1:]breakelse:ch = ch[:j] + 'a' + ch[j + 1:]j = j - 1self.__ch = chreturn self.__chclass RequestApi(object):def __init__(self, appid, secret_key, upload_file_path):self.appid = appidself.secret_key = secret_keyself.upload_file_path = upload_file_path# 根據(jù)不同的apiname生成不同的參數(shù),本示例中未使用全部參數(shù)您可在官網(wǎng)(https://doc.xfyun.cn/rest_api/%E8%AF%AD%E9%9F%B3%E8%BD%AC%E5%86%99.html)查看后選擇適合業(yè)務(wù)場(chǎng)景的進(jìn)行更換def gene_params(self, apiname, taskid=None, slice_id=None):appid = self.appidsecret_key = self.secret_keyupload_file_path = self.upload_file_pathts = str(int(time.time()))m2 = hashlib.md5()m2.update((appid + ts).encode('utf-8'))md5 = m2.hexdigest()md5 = bytes(md5, encoding='utf-8')# 以secret_key為key, 上面的md5為msg, 使用hashlib.sha1加密結(jié)果為signasigna = hmac.new(secret_key.encode('utf-8'), md5, hashlib.sha1).digest()signa = base64.b64encode(signa)signa = str(signa, 'utf-8')file_len = os.path.getsize(upload_file_path)file_name = os.path.basename(upload_file_path)param_dict = {}if apiname == api_prepare:# slice_num是指分片數(shù)量,如果您使用的音頻都是較短音頻也可以不分片,直接將slice_num指定為1即可slice_num = int(file_len / file_piece_sice) + (0 if (file_len % file_piece_sice == 0) else 1)param_dict['app_id'] = appidparam_dict['signa'] = signaparam_dict['ts'] = tsparam_dict['file_len'] = str(file_len)param_dict['file_name'] = file_nameparam_dict['slice_num'] = str(slice_num)elif apiname == api_upload:param_dict['app_id'] = appidparam_dict['signa'] = signaparam_dict['ts'] = tsparam_dict['task_id'] = taskidparam_dict['slice_id'] = slice_idelif apiname == api_merge:param_dict['app_id'] = appidparam_dict['signa'] = signaparam_dict['ts'] = tsparam_dict['task_id'] = taskidparam_dict['file_name'] = file_nameelif apiname == api_get_progress or apiname == api_get_result:param_dict['app_id'] = appidparam_dict['signa'] = signaparam_dict['ts'] = tsparam_dict['task_id'] = taskidreturn param_dict# 請(qǐng)求和結(jié)果解析,結(jié)果中各個(gè)字段的含義可參考:https://doc.xfyun.cn/rest_api/%E8%AF%AD%E9%9F%B3%E8%BD%AC%E5%86%99.htmldef gene_request(self, apiname, data, files=None, headers=None):response = requests.post(lfasr_host + apiname, data=data, files=files, headers=headers)result = json.loads(response.text)if result["ok"] == 0:if apiname=='/getResult':results=re.findall(r"\"onebest\":\"(.+?)\",",result['data'])print(results)upload_file_path = self.upload_file_pathwith open(str(upload_file_path)+".txt", "a", encoding='utf-8') as f:for i in results:f.write(i)print("{} success:".format(apiname) + str(result))return resultelse:print("{} error:".format(apiname) + str(result))exit(0)return result# 預(yù)處理def prepare_request(self):return self.gene_request(apiname=api_prepare,data=self.gene_params(api_prepare))# 上傳def upload_request(self, taskid, upload_file_path):file_object = open(upload_file_path, 'rb')try:index = 1sig = SliceIdGenerator()while True:content = file_object.read(file_piece_sice)if not content or len(content) == 0:breakfiles = {"filename": self.gene_params(api_upload).get("slice_id"),"content": content}response = self.gene_request(api_upload,data=self.gene_params(api_upload, taskid=taskid,slice_id=sig.getNextSliceId()),files=files)if response.get('ok') != 0:# 上傳分片失敗print('upload slice fail, response: ' + str(response))return Falseprint('upload slice ' + str(index) + ' success')index += 1finally:'file index:' + str(file_object.tell())file_object.close()return True# 合并def merge_request(self, taskid):return self.gene_request(api_merge, data=self.gene_params(api_merge, taskid=taskid))# 獲取進(jìn)度def get_progress_request(self, taskid):return self.gene_request(api_get_progress, data=self.gene_params(api_get_progress, taskid=taskid))# 獲取結(jié)果def get_result_request(self, taskid):return self.gene_request(api_get_result, data=self.gene_params(api_get_result, taskid=taskid))def all_api_request(self):# 1. 預(yù)處理pre_result = self.prepare_request()taskid = pre_result["data"]# 2 . 分片上傳self.upload_request(taskid=taskid, upload_file_path=self.upload_file_path)# 3 . 文件合并self.merge_request(taskid=taskid)# 4 . 獲取任務(wù)進(jìn)度while True:# 每隔20秒獲取一次任務(wù)進(jìn)度progress = self.get_progress_request(taskid)progress_dic = progressif progress_dic['err_no'] != 0 and progress_dic['err_no'] != 26605:print('task error: ' + progress_dic['failed'])returnelse:data = progress_dic['data']task_status = json.loads(data)if task_status['status'] == 9:print('task ' + taskid + ' finished')breakprint('The task ' + taskid + ' is in processing, task status: ' + str(data))# 每次獲取進(jìn)度間隔20Stime.sleep(20)# 5 . 獲取結(jié)果self.get_result_request(taskid=taskid)# 注意:如果出現(xiàn)requests模塊報(bào)錯(cuò):"NoneType" object has no attribute 'read', 請(qǐng)嘗試將requests模塊更新到2.20.0或以上版本(本demo測(cè)試版本為2.20.0) # 輸入訊飛開放平臺(tái)的appid,secret_key和待轉(zhuǎn)寫的文件路徑 if __name__ == '__main__':api = RequestApi(appid="", secret_key="", upload_file_path=r"")api.all_api_request()

3.代碼說明

使用上面的代碼需要加三處地方。
代碼倒數(shù)第二行的appid和密鑰用你自己的,文件路徑是音頻的文件路徑。

4.測(cè)試結(jié)果


上面是我的文件目錄(音頻時(shí)長大概五十多分鐘)。

上面即為生成的TXT文本。

體驗(yàn)包的時(shí)長也被扣了,不過5小時(shí)是夠用的(新用戶最多可以領(lǐng)50小時(shí))。

總結(jié)

以上是生活随笔為你收集整理的录音转文字python实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。