Última atividade 1749445286

Load this obspython script into OBS to register a hotkey that deletes last recording file and starts a new recording

Revisão e79d2c82d7d1a21e5208fd9fa5a0925d88990801

stop-record-delete-recording-start-new.py Bruto
1# Copyright (C) 2025 Onyx and Iris
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <https://www.gnu.org/licenses/>.
15#
16# Special Thanks to OBS Discord user Penwywern for helping to improve this snippet.
17
18"""
19stop-record-delete-recording-start-new.py
20This OBS Python script allows users to register a hotkey that, when pressed, will:
211. Stop the current recording (if active).
222. Delete the most recent recording file.
233. Start a new recording after a short delay.
24Features:
25- Registers a customizable hotkey in OBS under 'Hotkeys'.
26- Ensures the hotkey only works when a recording is active.
27- Handles OBS frontend events to safely delete the last recording after stopping.
28- Provides logging for each major action (stop, delete, start).
29Functions:
30- script_description(): Returns a description of the script for OBS.
31- script_load(settings): Registers the hotkey and loads its settings.
32- callback(pressed): Hotkey callback to initiate the stop/delete/start sequence.
33- on_event(event): Handles OBS events, deletes the last recording, and schedules the new recording.
34- on_timer_tick(): Starts a new recording after a delay.
35Requirements:
36- OBS Studio with Python scripting support.
37- Proper hotkey configuration in OBS settings.
38License:
39- GPL-3.0-or-later
40Copyright (C) 2025 Onyx and Iris
41"""
42
43__version__ = "0.2.0"
44
45from pathlib import Path
46
47import obspython as obs
48
49HOTKEY_ID = obs.OBS_INVALID_HOTKEY_ID
50
51
52### SCRIPT SETUP ###
53def script_description() -> str:
54 """Registers a hotkey to delete the last recording and start a new one."""
55 return (
56 "This script registers a hotkey to delete the last recording and start a new one.\n"
57 "The hotkey can be set in the OBS settings under 'Hotkeys'.\n"
58 "Make sure to set the hotkey before using this script."
59 )
60
61
62def script_load(settings):
63 """Register a hotkey to trigger the script."""
64 HOTKEY_ID = obs.obs_hotkey_register_frontend(
65 "record_delete_start", "Delete Last Recording and Start New", callback
66 )
67 hotkey_save = obs.obs_data_get_array(settings, "record_delete_start")
68 obs.obs_hotkey_load(HOTKEY_ID, hotkey_save)
69 obs.obs_data_array_release(hotkey_save)
70
71 return HOTKEY_ID
72
73
74### CALLBACKS ###
75def callback(pressed):
76 """Callback function for the hotkey."""
77 if pressed:
78 if not obs.obs_frontend_recording_active():
79 obs.script_log(obs.LOG_WARNING, "Recording is not active.")
80 return
81
82 obs.obs_frontend_add_event_callback(on_event)
83 obs.obs_frontend_recording_stop()
84
85
86def on_event(event):
87 """Event handler for the hotkey."""
88 if event == obs.OBS_FRONTEND_EVENT_RECORDING_STOPPED:
89 obs.obs_frontend_remove_event_callback(on_event)
90
91 obs.script_log(obs.LOG_INFO, "Recording stopped.")
92 resp = obs.obs_frontend_get_last_recording()
93 file_pn = Path(resp)
94 if file_pn.exists():
95 file_pn.unlink()
96 obs.script_log(obs.LOG_INFO, f"Deleted recording: {file_pn}")
97
98 obs.timer_add(on_timer_tick, 1000)
99
100
101def on_timer_tick():
102 """Timer callback to start a new recording."""
103 obs.remove_current_callback()
104
105 obs.obs_frontend_recording_start()
106 obs.script_log(obs.LOG_INFO, "Recording started.")
107