__init__.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import asyncio
  2. from utils import os
  3. class Time:
  4. def __init__(self, timepoint) -> None:
  5. if isinstance(timepoint, str):
  6. split = list(map(int, timepoint.split(":")))
  7. split = [0]*(3 - len(split)) + split
  8. self.split = split + [0]
  9. timepoint = split[0] * 60 * 60 + split[1] * 60 + split[2]
  10. self.time = int(timepoint * 1000)
  11. self.split = (
  12. int(timepoint / (1000 * 60 * 60)),
  13. int(timepoint / (1000*60)) % 60,
  14. int(timepoint / 1000) % 60,
  15. int(timepoint % 1000)
  16. )
  17. def __str__(self) -> str:
  18. h, m, s, ms = self.split
  19. return f"{h}:{m}:{s}.{ms}"
  20. def __add__(self, other):
  21. if isinstance(other, Time):
  22. return Time(self.time + other.time)
  23. return Time(self.time + other)
  24. def __sub__(self, other):
  25. if isinstance(other, Time):
  26. return Time(self.time - other.time)
  27. return Time(self.time - other)
  28. def __lt__(self, other):
  29. if isinstance(other, Time):
  30. return self.time < other.time
  31. return self.time < other
  32. def __gt__(self, other):
  33. if isinstance(other, Time):
  34. return self.time > other.time
  35. return self.time > other
  36. async def slice(path, filename, intervals: list, margin: int=0):
  37. tasks = []
  38. for interval in intervals:
  39. start = Time(interval["start"]) + margin / 2
  40. if start.time < 0:
  41. start = Time(0)
  42. end = Time(interval["end"]) - margin / 2
  43. title = interval["title"]
  44. command = f'ffmpeg -ss {start} -to {end} -i "{path}/{filename}" -qscale 0 "{path}/{title}.mp3"'
  45. task = asyncio.create_task(os.system(command))
  46. tasks.append(task)
  47. await asyncio.gather(*tasks)