Prettier TQDM for reviewtranscode

This commit is contained in:
2025-06-26 05:51:44 +02:00
parent 97974fc27f
commit 9a691d90e6

View File

@@ -5,26 +5,42 @@ import argparse
from tqdm import tqdm
import ctypes
def is_windows():
return os.name == 'nt'
def hide_windows_console():
if is_windows():
try:
ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)
ctypes.windll.user32.ShowWindow(
ctypes.windll.kernel32.GetConsoleWindow(), 0)
except Exception:
pass
def process_files(csv_path, dry_run=False):
# Constants
MAX_DISPLAY_LEN = 90
RED = "\033[91m"
RESET = "\033[0m"
# Read all rows
with open(csv_path, newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
rows = list(reader)
fieldnames = reader.fieldnames
# Compute max length for padding (after truncation if needed)
def safe_name(name):
return name if len(name) <= MAX_DISPLAY_LEN else name[:MAX_DISPLAY_LEN - 3] + "..."
all_filenames = [safe_name(os.path.basename(row['original_file'])) for row in rows if 'original_file' in row]
max_filename_len = max(len(name) for name in all_filenames)
updated = False
for row in tqdm(rows, desc="Processing files", unit="file"):
for row in (pbar := tqdm(rows, desc="Processing files", unit="file")):
status = row.get('accepted', '').strip()
orig = row['original_file']
trans = row['transcoded_file']
@@ -32,61 +48,60 @@ def process_files(csv_path, dry_run=False):
base, _ = os.path.splitext(os.path.basename(orig))
target = os.path.join(orig_dir, base + '.mkv')
if status == 'o':
print(f"\n[ACCEPTED:o] Handling:\n Original: {orig}\n Transcoded: {trans}\n Target: {target}")
# Format filename for display
filename = os.path.basename(orig)
if len(filename) > MAX_DISPLAY_LEN:
display_name = filename[:MAX_DISPLAY_LEN - 3] + "..."
display_name_colored = f"{RED}{display_name}{RESET}"
else:
display_name = filename
display_name_colored = display_name
padded_name = display_name_colored.ljust(max_filename_len + len(RED) + len(RESET)) # pad including ANSI
# Update progress bar
pbar.set_description(f"Processing: {padded_name}")
if status == 'o':
if not os.path.exists(trans):
print(f" [ERROR] Transcoded file not found: {trans}")
continue
if not dry_run:
try:
if os.path.exists(orig):
os.remove(orig)
print(f" Deleted original: {orig}")
shutil.move(trans, target)
print(f" Renamed transcoded to: {target}")
row['accepted'] = 'D'
updated = True
except Exception as e:
print(f" [ERROR] {e}")
else:
print(" [DRY RUN] Would delete original and move transcoded")
except Exception:
pass
elif status == 'x':
print(f"\n[REJECTED:x] Handling:\n Transcoded: {trans}")
if os.path.exists(trans):
if not dry_run:
try:
os.remove(trans)
print(f" Deleted rejected transcoded: {trans}")
updated = True
except Exception as e:
print(f" [ERROR] {e}")
else:
print(" [DRY RUN] Would delete rejected transcoded")
except Exception:
pass
if updated and not dry_run:
backup = csv_path + '.bak'
shutil.copy(csv_path, backup)
print(f"\nBackup of CSV saved to: {backup}")
with open(csv_path, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
print(f"Updated CSV written: {csv_path}")
elif not updated:
print("\nNo files processed. CSV unchanged.")
elif dry_run:
print("\nDry run complete. No changes made.")
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Replace original videos with AV1 transcodes based on CSV accepted column'
)
parser.add_argument('csvfile', help='Path to the CSV result file')
parser.add_argument('--dry-run', action='store_true', help='Show actions without making changes')
parser.add_argument('--hide-console', action='store_true', help='Hide Windows console window (GUI mode)')
parser.add_argument('--dry-run', action='store_true',
help='Show actions without making changes')
parser.add_argument('--hide-console', action='store_true',
help='Hide Windows console window (GUI mode)')
args = parser.parse_args()
if args.hide_console: