main.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os
  2. import warnings
  3. import tempfile
  4. import time
  5. from utils.files import filename, write_srt
  6. from utils.ffmpeg import get_audio, add_subtitles_to_mp4
  7. from utils.bazarr import get_wanted_episodes, get_episode_details, sync_series
  8. from utils.sonarr import update_show_in_soarr
  9. from utils.whisper import WhisperAI
  10. def process(args: dict):
  11. model_name: str = args.pop("model")
  12. language: str = args.pop("language")
  13. sample_interval: str = args.pop("sample_interval")
  14. audio_channel: str = args.pop('audio_channel')
  15. if model_name.endswith(".en"):
  16. warnings.warn(
  17. f"{model_name} is an English-only model, forcing English detection.")
  18. args["language"] = "en"
  19. # if translate task used and language argument is set, then use it
  20. elif language != "auto":
  21. args["language"] = language
  22. model_args = {}
  23. model_args["model_size_or_path"] = model_name
  24. model_args["device"] = args.pop("device")
  25. model_args["compute_type"] = args.pop("compute_type")
  26. list_of_episodes_needing_subtitles = get_wanted_episodes()
  27. print(f"Found {list_of_episodes_needing_subtitles['total']} episodes needing subtitles.")
  28. for episode in list_of_episodes_needing_subtitles['data']:
  29. print(f"Processing {episode['seriesTitle']} - {episode['episode_number']}")
  30. episode_data = get_episode_details(episode['sonarrEpisodeId'])
  31. audios = get_audio([episode_data['path']], audio_channel, sample_interval)
  32. subtitles = get_subtitles(audios, tempfile.gettempdir(), model_args, args)
  33. add_subtitles_to_mp4(subtitles)
  34. update_show_in_soarr(episode['sonarrSeriesId'])
  35. time.sleep(5)
  36. sync_series()
  37. def get_subtitles(audio_paths: list, output_dir: str,
  38. model_args: dict, transcribe_args: dict):
  39. model = WhisperAI(model_args, transcribe_args)
  40. subtitles_path = {}
  41. for path, audio_path in audio_paths.items():
  42. print(
  43. f"Generating subtitles for {filename(path)}... This might take a while."
  44. )
  45. srt_path = os.path.join(output_dir, f"{filename(path)}.srt")
  46. segments = model.transcribe(audio_path)
  47. with open(srt_path, "w", encoding="utf-8") as srt:
  48. write_srt(segments, file=srt)
  49. subtitles_path[path] = srt_path
  50. return subtitles_path