File indexing completed on 2024-05-05 04:52:35

0001 #!/usr/bin/env python3
0002 # SPDX-FileCopyrightText: 2021 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003 # SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #pip3 install vosk
0006 #pip3 install srt
0007 
0008 from vosk import Model, KaldiRecognizer, SetLogLevel
0009 import sys
0010 import os
0011 import wave
0012 import subprocess
0013 import codecs
0014 import datetime
0015 
0016 SetLogLevel(-1)
0017 
0018 os.chdir(sys.argv[1])
0019 
0020 if not os.path.exists(sys.argv[2]):
0021     print ("Please download the model from https://alphacephei.com/vosk/models and unpack as ",sys.argv[2]," in the current folder.")
0022     exit (1)
0023 
0024 if sys.platform == 'darwin':
0025     from os.path import abspath, dirname, join
0026     path = abspath(join(dirname(__file__), '../../MacOS/ffmpeg'))
0027 else:
0028     path = 'ffmpeg'
0029 
0030 sample_rate=16000
0031 model = Model(sys.argv[2])
0032 rec = KaldiRecognizer(model, sample_rate)
0033 rec.SetWords(True)
0034 
0035 # zone rendering
0036 if len(sys.argv) > 4 and (float(sys.argv[4])>0 or float(sys.argv[5])>0):
0037     process = subprocess.Popen([path, '-loglevel', 'quiet', '-i',
0038                             sys.argv[3], '-ss', sys.argv[4], '-t', sys.argv[5],
0039                             '-ar', str(sample_rate) , '-ac', '1', '-f', 's16le', '-'],
0040                             stdout=subprocess.PIPE)
0041 else:
0042     process = subprocess.Popen([path, '-loglevel', 'quiet', '-i',
0043                             sys.argv[3],
0044                             '-ar', str(sample_rate) , '-ac', '1', '-f', 's16le', '-'],
0045                             stdout=subprocess.PIPE)
0046 WORDS_PER_LINE = 7
0047 
0048 def transcribe():
0049     while True:
0050        data = process.stdout.read(4000)
0051        if len(data) == 0:
0052            sys.stdout.buffer.write(rec.FinalResult().encode('utf-8'))
0053            sys.stdout.flush()
0054            break
0055        if rec.AcceptWaveform(data):
0056            sys.stdout.buffer.write(rec.Result().encode('utf-8'))
0057            sys.stdout.flush()
0058 
0059 transcribe()
0060 #with open(sys.argv[3], 'w') as f:
0061 #    f.writelines(subtitle)
0062 #f.close()