instant-replay.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. obs = obslua
  2. source_name = ""
  3. hotkey_id = obs.OBS_INVALID_HOTKEY_ID
  4. attempts = 0
  5. ----------------------------------------------------------
  6. function try_play()
  7. local replay_buffer = obs.obs_frontend_get_replay_buffer_output()
  8. if replay_buffer == nil then
  9. obs.remove_current_callback()
  10. return
  11. end
  12. -- Call the procedure of the replay buffer named "get_last_replay" to
  13. -- get the last replay created by the replay buffer
  14. local cd = obs.calldata_create()
  15. local ph = obs.obs_output_get_proc_handler(replay_buffer)
  16. obs.proc_handler_call(ph, "get_last_replay", cd)
  17. local path = obs.calldata_string(cd, "path")
  18. obs.calldata_destroy(cd)
  19. obs.obs_output_release(replay_buffer)
  20. -- If the path is valid and the source exists, update it with the
  21. -- replay file to play back the replay. Otherwise, stop attempting to
  22. -- replay after 10 seconds
  23. if path == nil then
  24. attempts = attempts + 1
  25. if attempts >= 10 then
  26. obs.remove_current_callback()
  27. end
  28. else
  29. local source = obs.obs_get_source_by_name(source_name)
  30. if source ~= nil then
  31. local settings = obs.obs_data_create()
  32. obs.obs_data_set_string(settings, "local_file", path)
  33. obs.obs_data_set_bool(settings, "is_local_file", true)
  34. obs.obs_data_set_bool(settings, "close_when_inactive", true)
  35. obs.obs_data_set_bool(settings, "restart_on_activate", true)
  36. -- updating will automatically cause the source to
  37. -- refresh if the source is currently active, otherwise
  38. -- the source will play whenever its scene is activated
  39. obs.obs_source_update(source, settings)
  40. obs.obs_data_release(settings)
  41. obs.obs_source_release(source)
  42. end
  43. obs.remove_current_callback()
  44. end
  45. end
  46. -- The "Instant Replay" hotkey callback
  47. function instant_replay(pressed)
  48. if not pressed then
  49. return
  50. end
  51. local replay_buffer = obs.obs_frontend_get_replay_buffer_output()
  52. if replay_buffer ~= nil then
  53. -- Call the procedure of the replay buffer named "get_last_replay" to
  54. -- get the last replay created by the replay buffer
  55. local ph = obs.obs_output_get_proc_handler(replay_buffer)
  56. obs.proc_handler_call(ph, "save", nil)
  57. -- Set a 1-second timer to attempt playback every 1 second
  58. -- until the replay is available
  59. if obs.obs_output_active(replay_buffer) then
  60. attempts = 0
  61. obs.timer_add(try_play, 1000)
  62. else
  63. obs.script_log(obs.LOG_WARNING, "Tried to save an instant replay, but the replay buffer is not active!")
  64. end
  65. obs.obs_output_release(replay_buffer)
  66. else
  67. obs.script_log(obs.LOG_WARNING, "Tried to save an instant replay, but found no active replay buffer!")
  68. end
  69. end
  70. ----------------------------------------------------------
  71. -- A function named script_update will be called when settings are changed
  72. function script_update(settings)
  73. source_name = obs.obs_data_get_string(settings, "source")
  74. end
  75. -- A function named script_description returns the description shown to
  76. -- the user
  77. function script_description()
  78. return "When the \"Instant Replay\" hotkey is triggered, saves a replay with the replay buffer, and then plays it in a media source as soon as the replay is ready. Requires an active replay buffer.\n\nMade by Jim"
  79. end
  80. -- A function named script_properties defines the properties that the user
  81. -- can change for the entire script module itself
  82. function script_properties()
  83. props = obs.obs_properties_create()
  84. local p = obs.obs_properties_add_list(props, "source", "Media Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
  85. local sources = obs.obs_enum_sources()
  86. if sources ~= nil then
  87. for _, source in ipairs(sources) do
  88. source_id = obs.obs_source_get_id(source)
  89. if source_id == "ffmpeg_source" then
  90. local name = obs.obs_source_get_name(source)
  91. obs.obs_property_list_add_string(p, name, name)
  92. end
  93. end
  94. end
  95. obs.source_list_release(sources)
  96. return props
  97. end
  98. -- A function named script_load will be called on startup
  99. function script_load(settings)
  100. hotkey_id = obs.obs_hotkey_register_frontend("instant_replay.trigger", "Instant Replay", instant_replay)
  101. local hotkey_save_array = obs.obs_data_get_array(settings, "instant_replay.trigger")
  102. obs.obs_hotkey_load(hotkey_id, hotkey_save_array)
  103. obs.obs_data_array_release(hotkey_save_array)
  104. end
  105. -- A function named script_save will be called when the script is saved
  106. --
  107. -- NOTE: This function is usually used for saving extra data (such as in this
  108. -- case, a hotkey's save data). Settings set via the properties are saved
  109. -- automatically.
  110. function script_save(settings)
  111. local hotkey_save_array = obs.obs_hotkey_save(hotkey_id)
  112. obs.obs_data_set_array(settings, "instant_replay.trigger", hotkey_save_array)
  113. obs.obs_data_array_release(hotkey_save_array)
  114. end