|
1 | | -#!/usr/bin/env python3 |
2 | | - |
3 | | -import os |
| 1 | +import re |
4 | 2 | import sys |
5 | | -import argparse |
6 | | - |
7 | | -def is_non_fixed(filename): |
8 | | - """ |
9 | | - Determine if the .srt file is 'non-fixed'. |
10 | | - Non-fixed files end with '.en.srt' but do not end with '.en-fixed.srt'. |
11 | | - """ |
12 | | - return filename.lower().endswith('.en.srt') and not filename.lower().endswith('.en-fixed.srt') |
13 | | - |
14 | | -def find_non_fixed_srt_files(directory): |
15 | | - """ |
16 | | - Find all .srt files in the directory that are not 'fixed'. |
17 | | - """ |
18 | | - try: |
19 | | - all_files = os.listdir(directory) |
20 | | - except Exception as e: |
21 | | - print(f"Error accessing directory '{directory}': {e}") |
22 | | - sys.exit(1) |
23 | | - |
24 | | - non_fixed_files = [f for f in all_files if is_non_fixed(f)] |
25 | | - return non_fixed_files |
26 | | - |
27 | | -def delete_files(files, directory, dry_run=False): |
28 | | - """ |
29 | | - Delete the specified files from the directory. |
30 | | - If dry_run is True, just print the files that would be deleted. |
31 | | - """ |
32 | | - if dry_run: |
33 | | - print("\nDry Run: The following files would be deleted:") |
34 | | - for f in files: |
35 | | - print(f" - {f}") |
36 | | - return |
37 | | - |
38 | | - for f in files: |
39 | | - file_path = os.path.join(directory, f) |
40 | | - try: |
41 | | - os.remove(file_path) |
42 | | - print(f"Deleted: {f}") |
43 | | - except Exception as e: |
44 | | - print(f"Error deleting '{f}': {e}") |
| 3 | +from pathlib import Path |
| 4 | +import shutil |
| 5 | + |
| 6 | +def remove_timestamps_and_line_numbers(input_file, output_file): |
| 7 | + # Regex pattern to match timestamp lines |
| 8 | + timestamp_pattern = re.compile(r'^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}$') |
| 9 | + # Regex pattern to match line numbers |
| 10 | + line_number_pattern = re.compile(r'^\d+$') |
| 11 | + |
| 12 | + with open(input_file, 'r', encoding='utf-8') as infile, open(output_file, 'w', encoding='utf-8') as outfile: |
| 13 | + for line in infile: |
| 14 | + line = line.strip() |
| 15 | + # Skip timestamp lines and line numbers, write all others |
| 16 | + if not timestamp_pattern.match(line) and not line_number_pattern.match(line) and line != '': |
| 17 | + outfile.write(line + '\n') |
45 | 18 |
|
46 | 19 | def main(): |
47 | | - parser = argparse.ArgumentParser( |
48 | | - description="Delete non-fixed .srt files in a directory.\n\n" |
49 | | - "Fixed files end with '.en-fixed.srt' and will be preserved.\n" |
50 | | - "Non-fixed files end with '.en.srt' and will be deleted.", |
51 | | - formatter_class=argparse.RawDescriptionHelpFormatter |
52 | | - ) |
53 | | - parser.add_argument( |
54 | | - 'directory', |
55 | | - nargs='?', |
56 | | - default='.', |
57 | | - help='Target directory (default: current directory)' |
58 | | - ) |
59 | | - parser.add_argument( |
60 | | - '--force', |
61 | | - '-f', |
62 | | - action='store_true', |
63 | | - help='Force deletion without confirmation' |
64 | | - ) |
65 | | - parser.add_argument( |
66 | | - '--dry-run', |
67 | | - '-d', |
68 | | - action='store_true', |
69 | | - help='Show files to be deleted without deleting them' |
70 | | - ) |
71 | | - |
72 | | - args = parser.parse_args() |
73 | | - target_dir = os.path.abspath(args.directory) |
74 | | - |
75 | | - if not os.path.isdir(target_dir): |
76 | | - print(f"Error: The directory '{target_dir}' does not exist or is not a directory.") |
| 20 | + if len(sys.argv) < 2: |
| 21 | + print("Usage: drag and drop SRT files onto this script.") |
77 | 22 | sys.exit(1) |
78 | 23 |
|
79 | | - non_fixed_files = find_non_fixed_srt_files(target_dir) |
80 | | - |
81 | | - if not non_fixed_files: |
82 | | - print("No non-fixed .srt files found to delete.") |
83 | | - sys.exit(0) |
| 24 | + for input_path in sys.argv[1:]: |
| 25 | + input_file = Path(input_path) |
| 26 | + if not input_file.exists() or input_file.suffix.lower() != '.srt': |
| 27 | + print(f"Skipping invalid file: {input_path}") |
| 28 | + continue |
84 | 29 |
|
85 | | - print(f"\nFound {len(non_fixed_files)} non-fixed .srt file(s) to delete in '{target_dir}':") |
86 | | - for f in non_fixed_files: |
87 | | - print(f" - {f}") |
| 30 | + # Create output folder in the same directory as the input file |
| 31 | + output_folder = input_file.parent / "no_timestamps" |
| 32 | + output_folder.mkdir(exist_ok=True) |
88 | 33 |
|
89 | | - if args.dry_run: |
90 | | - delete_files(non_fixed_files, target_dir, dry_run=True) |
91 | | - sys.exit(0) |
| 34 | + output_file = input_file.with_name(f"{input_file.stem}_no_timestamps{input_file.suffix}") |
| 35 | + print(f"Processing: {input_file} -> {output_file}") |
92 | 36 |
|
93 | | - if not args.force: |
94 | | - confirm = input("\nAre you sure you want to delete these files? (y/N): ").strip().lower() |
95 | | - if confirm != 'y': |
96 | | - print("Deletion canceled.") |
97 | | - sys.exit(0) |
98 | | - |
99 | | - delete_files(non_fixed_files, target_dir) |
| 37 | + try: |
| 38 | + remove_timestamps_and_line_numbers(input_file, output_file) |
| 39 | + # Move the processed file to the output folder |
| 40 | + shutil.move(output_file, output_folder / output_file.name) |
| 41 | + print(f"Processed and moved to: {output_folder / output_file.name}") |
| 42 | + except Exception as e: |
| 43 | + print(f"Error processing {input_file}: {e}") |
100 | 44 |
|
101 | 45 | if __name__ == "__main__": |
102 | 46 | main() |
0 commit comments