File indexing completed on 2024-05-19 08:43:40
0001 #!/usr/bin/env python3 0002 # SPDX-FileCopyrightText: 2023 Jean-Baptiste Mardelle <jb@kdenlive.org> 0003 # SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 0005 import datetime 0006 import srt 0007 import sys 0008 0009 import whispertotext 0010 0011 # Call this script with the following arguments 0012 # 1. source av file 0013 # 2. model name (tiny, base, small, medium, large) 0014 # 3. output .srt file 0015 # 4. Device (cpu, cuda) 0016 # 5. translate or transcribe 0017 # 6. Language 0018 # 7. in point (optional) 0019 # 8. out point 0020 # 9. tmp file name to extract a clip's part 0021 0022 def main(): 0023 source = sys.argv[1] 0024 model = sys.argv[2] 0025 outfile = sys.argv[3] 0026 device = sys.argv[4] 0027 task = sys.argv[5] 0028 language = sys.argv[6] 0029 if len(sys.argv) > 8: 0030 whispertotext.extract_zone(source, sys.argv[7], sys.argv[8], sys.argv[9]) 0031 source = sys.argv[9] 0032 0033 result = whispertotext.run_whisper(source, model, device, task, language) 0034 0035 subs = [] 0036 for i in range(len(result["segments"])): 0037 start_time = result["segments"][i]["start"] 0038 end_time = result["segments"][i]["end"] 0039 duration = end_time - start_time 0040 timestamp = f"{start_time:.3f} - {end_time:.3f}" 0041 text = result["segments"][i]["text"] 0042 0043 sub = srt.Subtitle(index=len(subs), content=text, start=datetime.timedelta(seconds=start_time), end=datetime.timedelta(seconds=end_time)) 0044 subs.append(sub) 0045 0046 subtitle = srt.compose(subs) 0047 0048 with open(outfile, 'w', encoding='utf8') as f: 0049 f.writelines(subtitle) 0050 return 0 0051 0052 0053 if __name__ == "__main__": 0054 sys.exit(main())