映像(mp4)をmp3に変換し、wavに変換します。
wavに変換します。
wavに変換するときは、monoオーディオに変換することと、サンプルレートを忘れないようにしておく必要があります。
Audio Auditionでmp3からwavに変換しました。
変換する際の設定は以下のようにしました。
import io
import os
# Imports the Google Cloud client library
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
# Instantiates a client
client = speech.SpeechClient()
# The name of the audio file to transcribe
file_name = os.path.join(
os.path.dirname(__file__),
'resources',
'audio.wav')
with io.open(file_name, 'rb') as audio_file:
content = audio_file.read()
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000, # サンプルレート
language_code='ja-JP') # 日本語指定
response = client.recognize(config, audio)
for result in response.results:
print('Transcript: {}'.format(result.alternatives[0].transcript))
コンソールに日本語が表示されました!