003_create_recordings_table.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """Peewee migrations -- 003_create_recordings_table.py.
  2. Some examples (model - class or model name)::
  3. > Model = migrator.orm['model_name'] # Return model in current state by name
  4. > migrator.sql(sql) # Run custom SQL
  5. > migrator.python(func, *args, **kwargs) # Run python code
  6. > migrator.create_model(Model) # Create a model (could be used as decorator)
  7. > migrator.remove_model(model, cascade=True) # Remove a model
  8. > migrator.add_fields(model, **fields) # Add fields to a model
  9. > migrator.change_fields(model, **fields) # Change fields
  10. > migrator.remove_fields(model, *field_names, cascade=True)
  11. > migrator.rename_field(model, old_field_name, new_field_name)
  12. > migrator.rename_table(model, new_table_name)
  13. > migrator.add_index(model, *col_names, unique=False)
  14. > migrator.drop_index(model, *col_names)
  15. > migrator.add_not_null(model, *field_names)
  16. > migrator.drop_not_null(model, *field_names)
  17. > migrator.add_default(model, field_name, default)
  18. """
  19. from concurrent.futures import as_completed, ThreadPoolExecutor
  20. import datetime as dt
  21. import peewee as pw
  22. from decimal import ROUND_HALF_EVEN
  23. import random
  24. import string
  25. import os
  26. import subprocess as sp
  27. import glob
  28. import re
  29. try:
  30. import playhouse.postgres_ext as pw_pext
  31. except ImportError:
  32. pass
  33. from frigate.const import RECORD_DIR
  34. from frigate.models import Recordings
  35. SQL = pw.SQL
  36. def migrate(migrator, database, fake=False, **kwargs):
  37. migrator.create_model(Recordings)
  38. def backfill():
  39. # First add the index here, because there is a bug in peewee_migrate
  40. # when trying to create an multi-column index in the same migration
  41. # as the table: https://github.com/klen/peewee_migrate/issues/19
  42. Recordings.add_index("start_time", "end_time")
  43. Recordings.create_table()
  44. # Backfill existing recordings
  45. files = glob.glob(f"{RECORD_DIR}/*/*/*/*/*.mp4")
  46. def probe(path):
  47. ffprobe_cmd = [
  48. "ffprobe",
  49. "-v",
  50. "error",
  51. "-show_entries",
  52. "format=duration",
  53. "-of",
  54. "default=noprint_wrappers=1:nokey=1",
  55. path,
  56. ]
  57. p = sp.run(ffprobe_cmd, capture_output=True)
  58. if p.returncode == 0:
  59. return float(p.stdout.decode().strip())
  60. else:
  61. os.remove(path)
  62. return 0
  63. with ThreadPoolExecutor() as executor:
  64. future_to_path = {executor.submit(probe, path): path for path in files}
  65. for future in as_completed(future_to_path):
  66. path = future_to_path[future]
  67. duration = future.result()
  68. rand_id = "".join(
  69. random.choices(string.ascii_lowercase + string.digits, k=6)
  70. )
  71. search = re.search(
  72. r".+/(\d{4}[-]\d{2})/(\d{2})/(\d{2})/(.+)/(\d{2})\.(\d{2}).mp4",
  73. path,
  74. )
  75. if not search:
  76. return False
  77. date = f"{search.group(1)}-{search.group(2)} {search.group(3)}:{search.group(5)}:{search.group(6)}"
  78. start = dt.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
  79. end = start + dt.timedelta(seconds=duration)
  80. Recordings.create(
  81. id=f"{start.timestamp()}-{rand_id}",
  82. camera=search.group(4),
  83. path=path,
  84. start_time=start.timestamp(),
  85. end_time=end.timestamp(),
  86. duration=duration,
  87. )
  88. migrator.python(backfill)
  89. def rollback(migrator, database, fake=False, **kwargs):
  90. migrator.remove_model(Recordings)