refactor(main.py): simplify filename generation logic and update user instructions
This commit is contained in:
@@ -266,33 +266,6 @@ class VideoEditor:
|
|||||||
"""Check if file is a supported media format (video or image)"""
|
"""Check if file is a supported media format (video or image)"""
|
||||||
return self._is_video_file(file_path) or self._is_image_file(file_path)
|
return self._is_video_file(file_path) or self._is_image_file(file_path)
|
||||||
|
|
||||||
def _get_next_edited_filename(self, video_path: Path) -> str:
|
|
||||||
"""Generate the next available _edited_%03d filename, or overwrite if already edited"""
|
|
||||||
directory = video_path.parent
|
|
||||||
base_name = video_path.stem
|
|
||||||
extension = video_path.suffix
|
|
||||||
|
|
||||||
# Check if the current video already contains "_edited_" in its name
|
|
||||||
if "_edited_" in base_name:
|
|
||||||
# If it's already an edited video, overwrite it instead of creating _edited_edited
|
|
||||||
return video_path.name
|
|
||||||
|
|
||||||
# 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_next_screenshot_filename(self, video_path: Path) -> str:
|
def _get_next_screenshot_filename(self, video_path: Path) -> str:
|
||||||
"""Generate the next available screenshot filename: video_frame_00001.jpg, video_frame_00002.jpg, etc."""
|
"""Generate the next available screenshot filename: video_frame_00001.jpg, video_frame_00002.jpg, etc."""
|
||||||
@@ -1832,7 +1805,7 @@ class VideoEditor:
|
|||||||
print(" N: Next file")
|
print(" N: Next file")
|
||||||
print(" n: Previous file")
|
print(" n: Previous file")
|
||||||
print(" Enter: Save image (overwrites if '_edited_' in name)")
|
print(" Enter: Save image (overwrites if '_edited_' in name)")
|
||||||
print(" Shift+N: Save image as _edited_edited")
|
print(" n: Save image as _edited_edited")
|
||||||
print(" Q/ESC: Quit")
|
print(" Q/ESC: Quit")
|
||||||
print()
|
print()
|
||||||
else:
|
else:
|
||||||
@@ -1864,7 +1837,7 @@ class VideoEditor:
|
|||||||
print(" N: Next video")
|
print(" N: Next video")
|
||||||
print(" n: Previous video")
|
print(" n: Previous video")
|
||||||
print(" Enter: Render video (overwrites if '_edited_' in name)")
|
print(" Enter: Render video (overwrites if '_edited_' in name)")
|
||||||
print(" Shift+N: Render video as _edited_edited")
|
print(" n: Render video as _edited_edited")
|
||||||
print(" X: Cancel render")
|
print(" X: Cancel render")
|
||||||
print(" Q/ESC: Quit")
|
print(" Q/ESC: Quit")
|
||||||
print()
|
print()
|
||||||
@@ -2005,8 +1978,11 @@ class VideoEditor:
|
|||||||
elif key == ord("N"):
|
elif key == ord("N"):
|
||||||
if len(self.video_files) > 1:
|
if len(self.video_files) > 1:
|
||||||
self.previous_video()
|
self.previous_video()
|
||||||
|
elif key == ord("n"):
|
||||||
|
if len(self.video_files) > 1:
|
||||||
|
self.next_video()
|
||||||
else:
|
else:
|
||||||
# Shift+N - Create _edited_edited file
|
# n - Create _edited_edited file
|
||||||
directory = self.video_path.parent
|
directory = self.video_path.parent
|
||||||
base_name = self.video_path.stem
|
base_name = self.video_path.stem
|
||||||
extension = self.video_path.suffix
|
extension = self.video_path.suffix
|
||||||
@@ -2021,9 +1997,6 @@ class VideoEditor:
|
|||||||
|
|
||||||
output_path = str(directory / new_name)
|
output_path = str(directory / new_name)
|
||||||
success = self.render_video(output_path)
|
success = self.render_video(output_path)
|
||||||
elif key == ord("n"):
|
|
||||||
if len(self.video_files) > 1:
|
|
||||||
self.next_video()
|
|
||||||
elif key == 13: # Enter
|
elif key == 13: # Enter
|
||||||
# Only overwrite if file already contains "_edited_" in name
|
# Only overwrite if file already contains "_edited_" in name
|
||||||
if "_edited_" in self.video_path.stem:
|
if "_edited_" in self.video_path.stem:
|
||||||
@@ -2063,7 +2036,7 @@ class VideoEditor:
|
|||||||
if os.path.exists(temp_path):
|
if os.path.exists(temp_path):
|
||||||
os.remove(temp_path)
|
os.remove(temp_path)
|
||||||
else:
|
else:
|
||||||
print("Enter key only overwrites files with '_edited_' in the name. Use Shift+N to create new files.")
|
print("Enter key only overwrites files with '_edited_' in the name. Use 'n' to create new files.")
|
||||||
elif key == ord("t"):
|
elif key == ord("t"):
|
||||||
# Marker looping only for videos
|
# Marker looping only for videos
|
||||||
if not self.is_image_mode:
|
if not self.is_image_mode:
|
||||||
|
|||||||
Reference in New Issue
Block a user