前回はアカウントの登録から、サンプル音声のコマンドプロンプトへの
テキストの出力まで行っているため、その続きです。
(この記録は2/21作業分です。)
今回は、次の対応を追加しました。
1.テキストファイルへ出力。
2.1アクションで複数ファイルを処理。
3.srt形式(※)で出力。
苦労した点は以下です。
1.動画の形式(mp3、wav、など)が合わずにエラー出力。
2.動画のサイジング(8000hz、16000hzなど)が合わずにエラー出力。
3.特定サイズ以上の動画を受け付けずエラー出力。
4.特定時間沈黙があるとエラー出力。(デフォルト30秒)
5.字幕出力のエンジン(?)がいくつかあり、
デフォルトのものが精度が悪く、精度が良いものを設定する方法の調査。
主な対応は以下です。
1ー2.FFMPEGで動画形式をmp3へ。
3.同じくFFMPEGで複数ファイルに分割。
4ー5.pythonでパラメータを設定。
特に1-2.は詳しくもない音声のレート設定などを調べたりしたため、
当日の目標だった動画のアップロードまでできませんでした。
こだわりを持ちすぎるのも考え物でですね。
具体的な手順や詳細は後日纏めていきます。
(※)FFMPEGで動画にテキストを張り付けるのに適した形式。
何秒から何秒まで表示させるかという細かい指定ができる為、
自動化する際などには大変便利な形式です。
出来上がったコードは以下です。
[]内は自身で取得した情報を入力してください。
ソースコードの解説は、後日行おうと思います。
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 |
# coding: UTF-8 import json from os.path import join, dirname from ibm_watson import SpeechToTextV1 from ibm_watson.websocket import RecognizeCallback, AudioSource from ibm_cloud_sdk_core.authenticators import IAMAuthenticator import glob import os # FIX_VALUE INPUT_FOLDER_PATH='[任意のフォルダ]*.mp3' cont_type = "audio/mp3" lang = "ja-JP_BroadbandModel" TIME_STAMP_HEAD1='00:0' TIME_STAMP_HEAD2=':' # authenticator = IAMAuthenticator('[apiキー]') speech_to_text = SpeechToTextV1( authenticator=authenticator ) speech_to_text.set_service_url('[URL]') files = glob.glob(INPUT_FOLDER_PATH) fileCnt = 1 seqNo = 1 for file in files: audio_file = open(os.path.abspath(file), "rb") result_json = speech_to_text.recognize(audio=audio_file, content_type=cont_type, model=lang, keywords=['colorado'], keywords_threshold=0.5, max_alternatives=1, inactivity_timeout=60, timestamps=True).get_result() # cnt = 1 for i in range(len(result_json["results"])): header = TIME_STAMP_HEAD1 + str(fileCnt-1) + TIME_STAMP_HEAD2 result_p = result_json["results"][i]["alternatives"][0]["transcript"] timestamps = result_json["results"][i]["alternatives"][0]["timestamps"] if lang == "ja-JP_BroadbandModel": result_p = result_p.replace(' ','') print(str(seqNo)) print(header + str(timestamps[0][1]).replace('.',',') + " --> " + header + str(timestamps[len(timestamps)-1][2]).replace('.',',')) print(result_p + "\n") cnt = cnt + 1 seqNo = seqNo + 1 # with open("result.json","w") as f: json.dump(result_json,f,ensure_ascii=False,indent=2) fileCnt = fileCnt + 1 |
コメント