Last active
November 16, 2022 03:18
-
-
Save dfaker/fef969e096c096736d7e0193d6a00d05 to your computer and use it in GitHub Desktop.
initial_whisper_script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess as sp | |
import whisper | |
#replace this with the path to the video. | |
sourcefile = r'Z:\completeTorrents\Ai Uehara MEGAPACK\SHKD525.FHD\SHKD525.FHD.wmv' | |
start_time_minutes = 0.0 | |
model = whisper.load_model("small") | |
slice_size = 30 | |
i=0 | |
with open('subs.srt','w') as subsfile: | |
subsfile.write('\n') | |
for st in range(int(start_time_minutes*60), 900000, slice_size): | |
proc = sp.Popen(['ffmpeg','-y','-ss',str(st),'-i',sourcefile, '-t', str(slice_size), 'out.wav'], stderr=sp.DEVNULL) | |
proc.communicate() | |
result = model.transcribe("out.wav",language='Japanese', task='translate') | |
for sv in result['segments']: | |
i+=1 | |
time_start = st+sv['start'] | |
hours, minutes, seconds = int(time_start/3600), (time_start/60) % 60, (time_start) % 60 | |
timestamp_start = "%02d:%02d:%06.3f" % (hours, minutes, seconds) | |
timestamp_start = timestamp_start.replace('.', ',') | |
time_end = st+sv['end'] | |
hours, minutes, seconds = int(time_end/3600), (time_end/60) % 60, (time_end) % 60 | |
timestamp_end = "%02d:%02d:%06.3f" % (hours, minutes, seconds) | |
timestamp_end = timestamp_end.replace('.', ',') | |
subsline = "{i}\n{s} --> {e}\n{text}".format(i=i, s=timestamp_start, e=timestamp_end, text=sv['text'].strip()) | |
print(subsline) | |
try: | |
subsfile.write(subsline+'\n\n') | |
except Exception as e: | |
pass | |
subsfile.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment