file.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from pathlib import Path
  2. from typing import Union
  3. from loguru import logger
  4. from natsort import natsorted
  5. AUDIO_EXTENSIONS = {
  6. ".mp3",
  7. ".wav",
  8. ".flac",
  9. ".ogg",
  10. ".m4a",
  11. ".wma",
  12. ".aac",
  13. ".aiff",
  14. ".aif",
  15. ".aifc",
  16. }
  17. VIDEO_EXTENSIONS = {
  18. ".mp4",
  19. ".avi",
  20. }
  21. def list_files(
  22. path: Union[Path, str],
  23. extensions: set[str] = None,
  24. recursive: bool = False,
  25. sort: bool = True,
  26. ) -> list[Path]:
  27. """List files in a directory.
  28. Args:
  29. path (Path): Path to the directory.
  30. extensions (set, optional): Extensions to filter. Defaults to None.
  31. recursive (bool, optional): Whether to search recursively. Defaults to False.
  32. sort (bool, optional): Whether to sort the files. Defaults to True.
  33. Returns:
  34. list: List of files.
  35. """
  36. if isinstance(path, str):
  37. path = Path(path)
  38. if not path.exists():
  39. raise FileNotFoundError(f"Directory {path} does not exist.")
  40. files = [file for ext in extensions for file in path.rglob(f"*{ext}")]
  41. if sort:
  42. files = natsorted(files)
  43. return files
  44. def load_filelist(path: Path | str) -> list[tuple[Path, str, str, str]]:
  45. """
  46. Load a Bert-VITS2 style filelist.
  47. """
  48. files = set()
  49. results = []
  50. count_duplicated, count_not_found = 0, 0
  51. LANGUAGE_TO_LANGUAGES = {
  52. "zh": ["zh", "en"],
  53. "jp": ["jp", "en"],
  54. "en": ["en"],
  55. }
  56. with open(path, "r", encoding="utf-8") as f:
  57. for line in f.readlines():
  58. splits = line.strip().split("|", maxsplit=3)
  59. if len(splits) != 4:
  60. logger.warning(f"Invalid line: {line}")
  61. continue
  62. filename, speaker, language, text = splits
  63. file = Path(filename)
  64. language = language.strip().lower()
  65. if language == "ja":
  66. language = "jp"
  67. assert language in ["zh", "jp", "en"], f"Invalid language {language}"
  68. languages = LANGUAGE_TO_LANGUAGES[language]
  69. if file in files:
  70. logger.warning(f"Duplicated file: {file}")
  71. count_duplicated += 1
  72. continue
  73. if not file.exists():
  74. logger.warning(f"File not found: {file}")
  75. count_not_found += 1
  76. continue
  77. results.append((file, speaker, languages, text))
  78. if count_duplicated > 0:
  79. logger.warning(f"Total duplicated files: {count_duplicated}")
  80. if count_not_found > 0:
  81. logger.warning(f"Total files not found: {count_not_found}")
  82. return results