diff --git a/croppa/main.py b/croppa/main.py index 29bf228..1b2eca6 100644 --- a/croppa/main.py +++ b/croppa/main.py @@ -6,6 +6,7 @@ import numpy as np from pathlib import Path from typing import Optional, Tuple, List import time +import re class VideoEditor: @@ -112,6 +113,29 @@ class VideoEditor: self.progress_bar_text = "" self.progress_bar_fps = 0.0 # Current rendering FPS + def _get_next_edited_filename(self, video_path: Path) -> str: + """Generate the next available _edited_%03d filename""" + directory = video_path.parent + base_name = video_path.stem + extension = video_path.suffix + + # Pattern to match existing edited files: basename_edited_001.ext, basename_edited_002.ext, etc. + pattern = re.compile(rf"^{re.escape(base_name)}_edited_(\d{{3}}){re.escape(extension)}$") + + existing_numbers = set() + for file_path in directory.iterdir(): + if file_path.is_file(): + match = pattern.match(file_path.name) + if match: + existing_numbers.add(int(match.group(1))) + + # Find the next available number starting from 1 + next_number = 1 + while next_number in existing_numbers: + next_number += 1 + + return f"{base_name}_edited_{next_number:03d}{extension}" + def _get_video_files_from_directory(self, directory: Path) -> List[Path]: """Get all video files from a directory, sorted by name""" video_files = set() @@ -122,20 +146,25 @@ class VideoEditor: ): video_files.add(file_path) - edited_videos = set() + # Pattern to match edited files: basename_edited_001.ext, basename_edited_002.ext, etc. + edited_pattern = re.compile(r"^(.+)_edited_\d{3}$") + + edited_base_names = set() for file_path in video_files: - if "_edited" in file_path.stem: - edited_videos.add(file_path) + match = edited_pattern.match(file_path.stem) + if match: + edited_base_names.add(match.group(1)) non_edited_videos = set() for file_path in video_files: - if "_edited" in file_path.stem: + # Skip if this is an edited video + if edited_pattern.match(file_path.stem): continue - edited_equivalent = file_path.with_name( - f"{file_path.stem}_edited{file_path.suffix}" - ) - if edited_equivalent in edited_videos: + + # Skip if there's already an edited version of this video + if file_path.stem in edited_base_names: continue + non_edited_videos.add(file_path) return sorted(non_edited_videos) @@ -1248,7 +1277,7 @@ class VideoEditor: if len(self.video_files) > 1: self.next_video() elif key == 13: # Enter - output_name = f"{self.video_path.stem}_edited.mp4" + output_name = self._get_next_edited_filename(self.video_path) self.render_video(str(self.video_path.parent / output_name)) # Auto advance frame when playing