wave.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. import sys
  13. from soundfile import SoundFile, LibsndfileError # pyright: ignore
  14. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  15. QMessageBox,
  16. )
  17. def get_wave_duration_in_frames(file_name: str) -> int:
  18. try:
  19. wav = SoundFile(file_name)
  20. return int(wav.frames / wav.samplerate * 75)
  21. except LibsndfileError:
  22. QMessageBox.critical(
  23. None,
  24. "Error",
  25. f"Error: Could not get duration of '{file_name}'",
  26. )
  27. sys.exit(1)
  28. def get_wave_duration_in_secs(file_name: str) -> int:
  29. return int(get_wave_duration_in_frames(file_name) / 75)
  30. def get_ffmpeg_timestamp_from_frame(frames: int) -> str:
  31. milis = int(frames / 75 * 1000)
  32. return f"{milis}ms"
  33. def get_index_line_as_frames(line: str) -> int:
  34. stripped_line = line.strip()
  35. frames = 75 * 60 * int(stripped_line[9:11])
  36. frames += 75 * int(stripped_line[12:14])
  37. frames += int(stripped_line[15:17])
  38. return frames