feat(main.py): add file overwrite handling for video editor

This commit is contained in:
2025-09-08 00:42:00 +02:00
parent 56d6e04b48
commit 3a8f8d26d3

View File

@@ -1677,6 +1677,9 @@ class VideoEditor:
self.update_progress_bar(progress, text, fps)
elif update_type == "complete":
self.update_progress_bar(progress, text, fps)
# Handle file overwrite if this was an overwrite operation
if hasattr(self, 'overwrite_temp_path') and self.overwrite_temp_path:
self._handle_overwrite_completion()
elif update_type == "error":
self.update_progress_bar(progress, text, fps)
elif update_type == "cancelled":
@@ -1687,6 +1690,44 @@ class VideoEditor:
# No more updates in queue
pass
def _handle_overwrite_completion(self):
"""Handle file replacement after successful render"""
try:
print("Replacing original file...")
# Release current video capture before replacing the file
if hasattr(self, 'cap') and self.cap:
self.cap.release()
# Replace the original file with the temporary file
import shutil
print(f"DEBUG: Moving {self.overwrite_temp_path} to {self.overwrite_target_path}")
try:
shutil.move(self.overwrite_temp_path, self.overwrite_target_path)
print("DEBUG: File move successful")
except Exception as e:
print(f"DEBUG: File move failed: {e}")
# Try to clean up temp file
if os.path.exists(self.overwrite_temp_path):
os.remove(self.overwrite_temp_path)
raise
# Small delay to ensure file system operations are complete
time.sleep(0.1)
try:
self._load_video(self.video_path)
self.load_current_frame()
print("File reloaded successfully")
except Exception as e:
print(f"Warning: Could not reload file after overwrite: {e}")
print("The file was saved successfully, but you may need to restart the editor to continue editing it.")
except Exception as e:
print(f"Error during file overwrite: {e}")
finally:
# Clean up overwrite state
self.overwrite_temp_path = None
self.overwrite_target_path = None
def cancel_render(self):
"""Cancel the current render operation"""
if self.render_thread and self.render_thread.is_alive():
@@ -2032,50 +2073,11 @@ class VideoEditor:
print(f"DEBUG: Created temp file: {temp_path}")
print("Rendering to temporary file first...")
# Use existing render method
success = self.render_video(temp_path)
# Wait for render to complete using existing logic
while self.render_thread and self.render_thread.is_alive():
self.update_render_progress()
self.display_current_frame() # Show the progress bar
cv2.waitKey(1) # Update the display
time.sleep(0.1)
if success:
print("Replacing original file...")
# Release current video capture before replacing the file
if hasattr(self, 'cap') and self.cap:
self.cap.release()
# Replace the original file with the temporary file
import shutil
print(f"DEBUG: Moving {temp_path} to {self.video_path}")
try:
shutil.move(temp_path, str(self.video_path))
print("DEBUG: File move successful")
except Exception as e:
print(f"DEBUG: File move failed: {e}")
# Try to clean up temp file
if os.path.exists(temp_path):
os.remove(temp_path)
raise
# Small delay to ensure file system operations are complete
time.sleep(0.1)
try:
self._load_video(self.video_path)
self.load_current_frame()
print("File reloaded successfully")
except Exception as e:
print(f"Warning: Could not reload file after overwrite: {e}")
print("The file was saved successfully, but you may need to restart the editor to continue editing it.")
else:
print("DEBUG: Rendering failed")
# Clean up temp file if rendering failed
if os.path.exists(temp_path):
os.remove(temp_path)
# Store the temp path so we can replace the file when render completes
self.overwrite_temp_path = temp_path
self.overwrite_target_path = str(self.video_path)
else:
print(f"DEBUG: File '{self.video_path.stem}' does not contain '_edited_'")
print("Enter key only overwrites files with '_edited_' in the name. Use 'n' to create new files.")