Search Posts

Visits: 285

前回の記事で紹介したAudio identificationでの話者識別分離が思わしくないので、Zoomで話者別に記録された複数ファイルをそれぞれ認識させてマージすることにした。
ファイル名がaudioLTY-SAC41347073001.m4aのような形式でAudio Record/に記録されているのでそれぞれを認識させてLYT.jsonといった名称で保存した。
話者が複数なので例えば、Asd.json Zxc.jsonといった形でディレクトリに保存し、次のように指定して実行するとtranscribe.txtファイルを出力します。

transcriptChannel.py 2022/05-12/Leo.json+2022/05-12/LTY.json+2022/05-12/Nobu.json+2022/05-12/Frans.jso

#!/usr/bin/env python3
# 
# Modified by SAMBUICHI, Nobuyuki 
# 2022-05-18
# 
# original program transcript.py by AWS
# 
def main():
	import sys
	import json
	import datetime
	import codecs

	filenames=sys.argv[1]
	filenames= filenames.split('+')
	lines=[]	
	for filename in filenames:
		speaker=filename.split('/')[-1].split('.')[0]
		print ("Filename: ", filename)
		with codecs.open(filename, 'r', 'utf-8') as f:
			data=json.loads(f.read())
			items = data['results']['items']
			line=''
			time=0
			i=0
			for item in items:
				i=i+1
				content = item['alternatives'][0]['content']
				if item.get('start_time'):
					current_time=float(item['start_time'])
				elif item['type'] == 'punctuation':
					line = line+content
				if current_time > time+120:
					if speaker:
						lines.append({'speaker':speaker,'line':line, 'time':time})
					line=content
					time=current_time
				elif item['type'] != 'punctuation':
					line = line + ' ' + content
			lines.append({'speaker':speaker,'line':line,'time':time})
	sorted_lines = sorted(lines,key=lambda k: float(k['time']))
	with codecs.open('/'.join(filename.split('/')[:-1])+'/transcribe.txt', 'w', 'utf-8') as w:
		for line_data in sorted_lines:
			line='[' + str(datetime.timedelta(seconds=int(round(float(line_data['time']))))) + '] ' + line_data['speaker'] + ': ' + line_data.get('line')
			w.write(line + '\n\n')


if __name__ == '__main__':
	main()