下一秒我就是野生字幕君
最近劇荒,偶然翻出了曾經下載的電視劇回味一番,經典就是經典,不論是劇情還是台詞,都那么有魅力,咦?等等,台詞,台詞……作為一個IT從業者,我忽然靈光一現——現在語音識別技術這么發達,能否有什么辦法能幫我保存下一些精彩橋段的台詞呢?或許我也可以是個野生字幕君:p ,似乎也可以在此基礎上順手再翻譯一下個別難懂的台詞!
略加思索,我大概有了個想法——做個視頻中提取音頻的程序,而后去請求一個開放的語音識別API來幫我把語音轉為文字。鑒於之前調用有道智雲的愉快經驗,我決定再次拿來為我所用,很快做出了這個demo(請忽略這丑丑的界面布局,能用就行……)。
調用API接口的准備工作
首先,是需要在有道智雲的個人頁面上創建實例、創建應用、綁定應用和實例,獲取調用接口用到的應用的id和密鑰。具體個人注冊的過程和應用創建過程詳見文章分享一次批量文件翻譯的開發過程

開發過程詳細介紹
下面介紹具體的代碼開發過程。
首先分析有道智雲的API輸入輸出規范。根據文檔來看,調用接口格式如下:
有道語音識別API HTTPS地址:
https://openapi.youdao.com/asrapi
接口調用參數:
字段名 | 類型 | 含義 | 必填 | 備注 |
---|---|---|---|---|
q | text | 要翻譯的音頻文件的Base64編碼字符串 | True | 必須是Base64編碼 |
langType | text | 源語言 | True | 支持語言 |
appKey | text | 應用 ID | True | 可在 應用管理 查看 |
salt | text | UUID | True | UUID |
curtime | text | 時間戳(秒) | true | 秒數 |
sign | text | 簽名,通過md5(應用ID+q+salt+curTime+密鑰)生成 | True | 應用ID+q+salt+curTime+密鑰的MD5值 |
signType | text | 簽名版本 | True | v2 |
format | text | 語音文件的格式,wav | true | wav |
rate | text | 采樣率, 推薦 16000 采用率 | true | 16000 |
channel | text | 聲道數, 僅支持單聲道,請填寫固定值1 | true | 1 |
type | text | 上傳類型, 僅支持base64上傳,請填寫固定值1 | true | 1 |
其中q為base64編碼的待識別音頻文件,“上傳的文件時長不能超過120s,文件大小不能超過10M”,這點需要注意一下。
API的返回內容較為簡單:
字段 | 含義 |
---|---|
errorCode | 識別結果錯誤碼,一定存在。 詳細信息參加 錯誤代碼列表 |
result | 識別結果,識別成功一定存在 |
Demo開發:
這個demo使用python3開發,包括maindow.py,videoprocess.py,srbynetease.py三個文件。界面部分,使用python自帶的tkinter庫,提供視頻文件選擇、時間輸入框和確認按鈕;videoprocess.py來實現在視頻的指定時間區間提取音頻和處理API返回信息的功能;srbynetease.py將處理好的音頻發送到短語音識別API並返回結果。
-
界面部分:
界面部分代碼如下,比較簡單。
root=tk.Tk() root.title("netease youdao sr test") frm = tk.Frame(root) frm.grid(padx='50', pady='50') btn_get_file = tk.Button(frm, text='選擇待識別視頻', command=get_file) btn_get_file.grid(row=0, column=0, padx='10', pady='20') path_text = tk.Entry(frm, width='40') path_text.grid(row=0, column=1) start_label=tk.Label(frm,text='開始時刻:') start_label.grid(row=1,column=0) start_input=tk.Entry(frm) start_input.grid(row=1,column=1) end_label=tk.Label(frm,text='結束時刻:') end_label.grid(row=2,column=0) end_input=tk.Entry(frm) end_input.grid(row=2,column=1) sure_btn=tk.Button(frm, text='開始識別', command=start_sr) sure_btn.grid(row=3,column=0,columnspan=3) root.mainloop()
其中sure_btn的綁定事件start_sr()做了簡單的異常處理,並通過彈窗打印最終的識別結果:
def start_sr():
print(video.video_full_path)
if len(path_text.get())==0:
sr_result = '未選擇文件'
else:
video.start_time = int(start_input.get())
video.end_time = int(end_input.get())
sr_result=video.do_sr()
tk.messagebox.showinfo("識別結果", sr_result)
2. 在videoprocess.py中,我用到了python的moviepy庫來處理視頻,按指定起止時間截取視頻,提取音頻,並按API要求轉為base64編碼形式:
```python
def get_audio_base64(self):
video_clip=VideoFileClip(self.video_full_path).subclip(self.start_time,self.end_time)
audio=video_clip.audio
result_path=self.video_full_path.split('.')[0]+'_clip.mp3'
audio.write_audiofile(result_path)
audio_base64 = base64.b64encode(open(result_path,'rb').read()).decode('utf-8')
return audio_base64
處理好的音頻文件編碼傳到封裝好的有道智雲API調用方法中:
def do_sr(self):
audio_base64=self.get_audio_base64()
sr_result=srbynetease.connect(audio_base64)
print(sr_result)
if sr_result['errorCode']=='0':
return sr_result['result']
else:
return "Something wrong , errorCode:"+sr_result['errorCode']
-
srbynetease.py中封裝的調用方法比較簡單,按API文檔“組裝”好data{}發送即可:
def connect(audio_base64): data = {} curtime = str(int(time.time())) data['curtime'] = curtime salt = str(uuid.uuid1()) signStr = APP_KEY + truncate(audio_base64) + salt + curtime + APP_SECRET sign = encrypt(signStr) data['appKey'] = APP_KEY data['q'] = audio_base64 data['salt'] = salt data['sign'] = sign data['signType'] = "v2" data['langType'] = 'zh-CHS' data['rate'] = 16000 data['format'] = 'mp3' data['channel'] = 1 data['type'] = 1 response = do_request(data) return json.loads(str(response.content,'utf-8'))
效果展示
隨手打開《甄嬛傳》第一集的某一小段試試:
效果可以,斷句的一點小瑕疵可以忽略。沒想到這短語音識別API博古通今,古文語音識別也這么溜,厲害厲害!
總結
一番嘗試帶我打開了新世界的大門,從今天開始我可以是一個不打字卻能搬運字幕的野生字幕君了,后面再有時間可以試試識別完翻譯成其他語言的操作,嗯,是技術的力量!