diff --git a/src/lerobot/datasets/lerobot_dataset.py b/src/lerobot/datasets/lerobot_dataset.py index 2a47d2ac3..ccbee263a 100644 --- a/src/lerobot/datasets/lerobot_dataset.py +++ b/src/lerobot/datasets/lerobot_dataset.py @@ -1317,17 +1317,8 @@ class LeRobotDataset(torch.utils.data.Dataset): Starts recording audio data provided by the microphone and directly writes it in a .wav file. """ - audio_dir = self._get_raw_audio_file_path( - self.num_episodes, "observation.audio." + microphone_key - ).parent - if not audio_dir.is_dir(): - audio_dir.mkdir(parents=True, exist_ok=True) - - microphone.start_recording( - output_file=self._get_raw_audio_file_path( - self.num_episodes, "observation.audio." + microphone_key - ) - ) + audio_file = self._get_raw_audio_file_path(self.num_episodes, "observation.audio." + microphone_key) + microphone.start_recording(output_file=audio_file) def save_episode( self, diff --git a/src/lerobot/microphones/microphone.py b/src/lerobot/microphones/microphone.py index ca445a369..992b93c1a 100644 --- a/src/lerobot/microphones/microphone.py +++ b/src/lerobot/microphones/microphone.py @@ -283,7 +283,12 @@ class Microphone: return audio_readings - def start_recording(self, output_file: str | None = None, multiprocessing: bool | None = False) -> None: + def start_recording( + self, + output_file: str | None = None, + multiprocessing: bool | None = False, + overwrite: bool | None = True, + ) -> None: """ Starts the recording of the microphone. If output_file is provided, the audio will be written to this file. """ @@ -302,8 +307,15 @@ class Microphone: # Write recordings into a file if output_file is provided if output_file is not None: output_file = Path(output_file) + output_file.parent.mkdir(parents=True, exist_ok=True) + if output_file.exists(): - output_file.unlink() + if overwrite: + output_file.unlink() + else: + raise FileExistsError( + f"Output file {output_file} already exists. Set overwrite to True to overwrite it." + ) if multiprocessing: self.record_stop_event = process_Event()