get_wav_files.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from tts_webui.history_tab.get_hash_memoized import get_hash_memoized
  2. from tts_webui.history_tab.generate_pretty_name import generate_pretty_name
  3. from tts_webui.history_tab.generate_relative_date import generate_relative_date
  4. from tts_webui.history_tab.parse_time import extract_and_parse_time
  5. import glob
  6. import os
  7. import datetime
  8. def get_wav_files(directory: str):
  9. def get_wav_in_dir(directory: str):
  10. return os.path.join(directory, f"{os.path.basename(directory)}.wav")
  11. list_of_directories = glob.glob(f"{directory}/*")
  12. file_date_list = [
  13. [
  14. extract_and_parse_time(directory),
  15. generate_pretty_name(directory),
  16. generate_relative_date(extract_and_parse_time(directory)), # type: ignore
  17. get_wav_in_dir(directory),
  18. ]
  19. for directory in list_of_directories
  20. ]
  21. # order by date
  22. file_date_list.sort(key=lambda x: x[0], reverse=True)
  23. return file_date_list
  24. def generate_pretty_name_npz(name: str):
  25. return name.replace(".npz", "").strip().capitalize()
  26. if __name__ == "__main__":
  27. print(generate_pretty_name_npz("Alice.npz"))
  28. def get_npz_file_data(file: os.DirEntry):
  29. date = datetime.datetime.fromtimestamp(file.stat().st_mtime)
  30. # remove milliseconds
  31. date = date.replace(microsecond=0)
  32. return [
  33. date,
  34. generate_pretty_name_npz(file.name),
  35. generate_relative_date(date),
  36. get_hash_memoized(file.path),
  37. file.path,
  38. ]
  39. # TODO: add hash column
  40. def get_npz_files_voices(directory: str = "voices"):
  41. file_list = list(os.scandir(directory))
  42. data_list = [
  43. get_npz_file_data(file) for file in file_list if file.name.endswith(".npz")
  44. ]
  45. data_list.sort(key=lambda x: x[0], reverse=True)
  46. return data_list