Advertisement
Thuggernaut58
Oct 29th, 2024
16
0
Never
This is comment for paste Untitled
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import obspython as obs
  2. from shutil import move
  3. import os
  4. import time
  5. import logging
  6.  
  7. # Change the log file location to a user-writable directory
  8. log_file = os.path.join(os.path.expanduser("~"), "obs_recording_move.log")
  9.  
  10. # Output paths and search strings (set dynamically)
  11. output_paths = {}
  12.  
  13. # Set up logging to a file
  14. logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
  15. logger = logging.getLogger(__name__)
  16.  
  17. # Ensure logging to a file as well
  18. file_handler = logging.FileHandler(log_file)
  19. file_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
  20. logger.addHandler(file_handler)
  21.  
  22. # Description of the script for OBS
  23. def script_description():
  24. """Return the plugin description."""
  25. return "This plugin moves the recorded file to a specified folder after recording stops, based on keywords in the filename."
  26.  
  27. # Hook to connect recording stop signal
  28. def script_load(settings):
  29. """Hook the recording stop signal when the plugin loads."""
  30. signal_handler = obs.obs_output_get_signal_handler(
  31. obs.obs_frontend_get_recording_output()
  32. )
  33. obs.signal_handler_connect(signal_handler, "stop", signal_handler_function)
  34.  
  35. # Update function to set output paths and search strings dynamically
  36. def script_update(settings):
  37. """Called when script settings are updated."""
  38. global output_paths
  39. output_paths.clear()
  40.  
  41. # Retrieve settings from the OBS properties panel
  42. for i in range(1, 5):
  43. keyword = obs.obs_data_get_string(settings, f"search_string{i}")
  44. output_path = obs.obs_data_get_string(settings, f"output_path{i}")
  45.  
  46. if keyword and output_path:
  47. output_paths[keyword] = output_path
  48.  
  49. # Property panel for OBS interface configuration
  50. def script_properties():
  51. """Define properties for OBS interface configuration."""
  52. props = obs.obs_properties_create()
  53.  
  54. # Add fields for up to 4 keyword-folder pairs
  55. for i in range(1, 5):
  56. obs.obs_properties_add_text(
  57. props, f"search_string{i}", f"Keyword for Output {i}", obs.OBS_TEXT_DEFAULT
  58. )
  59. obs.obs_properties_add_path(
  60. props, f"output_path{i}", f"Output Folder {i}", obs.OBS_PATH_DIRECTORY, "", ""
  61. )
  62.  
  63. return props
  64.  
  65. # Signal handler for recording stop
  66. def signal_handler_function(calldata):
  67. """Handle the recording stop signal and move the file accordingly."""
  68. try:
  69. last_recording = obs.obs_frontend_get_last_recording()
  70.  
  71. # Wait a bit to ensure the file is available
  72. time.sleep(5)
  73.  
  74. if not last_recording or not os.path.exists(last_recording):
  75. logger.error(f"Recording file not found: {last_recording}")
  76. return
  77.  
  78. # Move the file based on keyword matching
  79. for keyword, output_path in output_paths.items():
  80. if keyword.lower() in os.path.basename(last_recording).lower():
  81. # Ensure the output path exists
  82. os.makedirs(output_path, exist_ok=True)
  83.  
  84. destination = os.path.join(output_path, os.path.basename(last_recording))
  85. move(last_recording, destination)
  86.  
  87. logger.info(f"Moved file to: {destination}")
  88. return # Exit once the file is moved successfully
  89.  
  90. # If no matching keyword was found
  91. logger.warning(f"No keyword match found for recording: {last_recording}")
  92.  
  93. except Exception as e:
  94. logger.error(f"Error occurred during file move: {e}")
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement