Advertisement
This is comment for paste
Untitled
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import obspython as obs
- from shutil import move
- import os
- import time
- import logging
- from typing import Dict, Optional
- import re
- class VODMover:
- def __init__(self):
- # Output paths and search strings with default values
- self.output_paths: Dict[str, str] = {
- "anythingelse": "D:/vods/anything-else",
- "bridges": "D:/vods/bridges",
- "destinystudio": "D:/vods/destiny-stream",
- "saturdaymorning": "D:/vods/saturday-morning"
- }
- # Initialize logging
- self.setup_logging()
- def setup_logging(self) -> None:
- """Set up logging configuration"""
- log_file = "obs_recording_move.log"
- logging.basicConfig(
- level=logging.INFO,
- format="%(asctime)s - %(levelname)s - %(message)s",
- handlers=[
- logging.FileHandler(log_file),
- logging.StreamHandler() # Also log to console
- ]
- )
- self.logger = logging.getLogger(__name__)
- def move_recording(self, recording_path: str) -> None:
- """
- Move recording to appropriate folder based on filename patterns
- """
- if not recording_path or not os.path.exists(recording_path):
- self.logger.error(f"Invalid recording path: {recording_path}")
- return
- try:
- filename = os.path.basename(recording_path).lower()
- destination = self._get_destination_folder(filename)
- if destination:
- self._perform_move(recording_path, destination)
- else:
- self.logger.warning(f"No matching folder found for: {filename}")
- # Move to anything-else folder as fallback
- self._perform_move(recording_path, self.output_paths["anythingelse"])
- except Exception as e:
- self.logger.error(f"Error processing recording {recording_path}: {str(e)}")
- def _get_destination_folder(self, filename: str) -> Optional[str]:
- """
- Determine destination folder based on filename patterns
- """
- patterns = {
- "bridges": r"bridge|bridges",
- "destinystudio": r"destiny|studio",
- "saturdaymorning": r"saturday|morning|weekend"
- }
- for key, pattern in patterns.items():
- if re.search(pattern, filename, re.IGNORECASE):
- return self.output_paths[key]
- return None
- def _perform_move(self, source: str, destination: str) -> None:
- """
- Perform the actual file move operation
- """
- if not os.path.exists(destination):
- os.makedirs(destination, exist_ok=True)
- self.logger.info(f"Created destination directory: {destination}")
- try:
- # Wait for file to be fully written
- time.sleep(5)
- # Get destination filepath
- dest_path = os.path.join(destination, os.path.basename(source))
- # Handle existing files
- if os.path.exists(dest_path):
- base, ext = os.path.splitext(dest_path)
- counter = 1
- while os.path.exists(f"{base}_{counter}{ext}"):
- counter += 1
- dest_path = f"{base}_{counter}{ext}"
- move(source, dest_path)
- self.logger.info(f"Successfully moved {source} to {dest_path}")
- except Exception as e:
- self.logger.error(f"Failed to move file {source}: {str(e)}")
- # Global instance of VODMover
- vod_mover = VODMover()
- def script_description():
- """Return the script description for OBS"""
- return "Automatically moves VOD recordings to categorized folders based on filename patterns"
- def script_load(settings):
- """Hook stop recording signal on script load"""
- signal_handler = obs.obs_output_get_signal_handler(
- obs.obs_frontend_get_recording_output()
- )
- obs.signal_handler_connect(signal_handler, "stop", on_recording_stop)
- def script_update(settings):
- """Update paths when settings are changed"""
- global vod_mover
- # Update paths from OBS settings
- vod_mover.output_paths = {
- "anythingelse": obs.obs_data_get_string(settings, "anything_else_path"),
- "bridges": obs.obs_data_get_string(settings, "bridges_path"),
- "destinystudio": obs.obs_data_get_string(settings, "destiny_path"),
- "saturdaymorning": obs.obs_data_get_string(settings, "saturday_path")
- }
- def script_properties():
- """Define properties for OBS interface"""
- props = obs.obs_properties_create()
- # Add path properties for each category
- obs.obs_properties_add_path(
- props, "anything_else_path",
- "Anything Else Path",
- obs.OBS_PATH_DIRECTORY,
- "", "D:/vods/anything-else"
- )
- obs.obs_properties_add_path(
- props, "bridges_path",
- "Bridges Path",
- obs.OBS_PATH_DIRECTORY,
- "", "D:/vods/bridges"
- )
- obs.obs_properties_add_path(
- props, "destiny_path",
- "Destiny Studio Path",
- obs.OBS_PATH_DIRECTORY,
- "", "D:/vods/destiny-stream"
- )
- obs.obs_properties_add_path(
- props, "saturday_path",
- "Saturday Morning Path",
- obs.OBS_PATH_DIRECTORY,
- "", "D:/vods/saturday-morning"
- )
- return props
- def on_recording_stop(calldata):
- """Handle recording stop event"""
- try:
- recording_path = obs.obs_frontend_get_last_recording()
- vod_mover.move_recording(recording_path)
- except Exception as e:
- logging.error(f"Error in recording stop handler: {str(e)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement