Rework naming to include %03d

This commit is contained in:
2025-09-04 16:25:29 +02:00
parent ad4130906b
commit f8780a2d43

View File

@@ -6,6 +6,7 @@ import numpy as np
from pathlib import Path from pathlib import Path
from typing import Optional, Tuple, List from typing import Optional, Tuple, List
import time import time
import re
class VideoEditor: class VideoEditor:
@@ -112,6 +113,29 @@ class VideoEditor:
self.progress_bar_text = "" self.progress_bar_text = ""
self.progress_bar_fps = 0.0 # Current rendering FPS 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]: def _get_video_files_from_directory(self, directory: Path) -> List[Path]:
"""Get all video files from a directory, sorted by name""" """Get all video files from a directory, sorted by name"""
video_files = set() video_files = set()
@@ -122,20 +146,25 @@ class VideoEditor:
): ):
video_files.add(file_path) 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: for file_path in video_files:
if "_edited" in file_path.stem: match = edited_pattern.match(file_path.stem)
edited_videos.add(file_path) if match:
edited_base_names.add(match.group(1))
non_edited_videos = set() non_edited_videos = set()
for file_path in video_files: 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 continue
edited_equivalent = file_path.with_name(
f"{file_path.stem}_edited{file_path.suffix}" # Skip if there's already an edited version of this video
) if file_path.stem in edited_base_names:
if edited_equivalent in edited_videos:
continue continue
non_edited_videos.add(file_path) non_edited_videos.add(file_path)
return sorted(non_edited_videos) return sorted(non_edited_videos)
@@ -1248,7 +1277,7 @@ class VideoEditor:
if len(self.video_files) > 1: if len(self.video_files) > 1:
self.next_video() self.next_video()
elif key == 13: # Enter 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)) self.render_video(str(self.video_path.parent / output_name))
# Auto advance frame when playing # Auto advance frame when playing