-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcli.py
More file actions
392 lines (326 loc) · 13 KB
/
cli.py
File metadata and controls
392 lines (326 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import argparse
import sys
from datetime import datetime
from pathlib import Path
from typing import Dict, List
def validate_args_and_show_help():
parser = argparse.ArgumentParser(
description="Batch process videos to remove AI watermarks (Sora, Runway, Pika, Kling, etc.)",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Process all .mp4 files in input folder
demark-world -i /path/to/input -o /path/to/output
# Process all .mov files
demark-world -i /path/to/input -o /path/to/output --pattern "*.mov"
# Use E2FGVI_HQ model (time-consistent, slower)
demark-world -i /path/to/input -o /path/to/output --model e2fgvi_hq
# Use bf16 + torch compile for faster inference (CUDA only)
demark-world -i /path/to/input -o /path/to/output --model e2fgvi_hq --bf16 --torch-compile
# Suppress internal progress bars
demark-world -i /path/to/input -o /path/to/output --quiet
""",
)
parser.add_argument(
"-i",
"--input",
type=str,
required=True,
help="Input folder containing video files",
)
parser.add_argument(
"-o",
"--output",
type=str,
required=True,
help="Output folder for cleaned videos",
)
parser.add_argument(
"-p",
"--pattern",
type=str,
default="*.mp4",
help="File pattern to match (default: *.mp4)",
)
parser.add_argument(
"--quiet",
action="store_true",
default=False,
help="Run in quiet mode (suppress tqdm and most logs).",
)
parser.add_argument(
"-m",
"--model",
type=str,
default="lama",
choices=["lama", "e2fgvi_hq"],
help="Model to use for watermark removal (default: lama). Options: lama (fast, may flicker), e2fgvi_hq (time consistent, slower)",
)
parser.add_argument(
"--batch-size",
type=int,
default=4,
help="Batch size for YOLO watermark detection (default: 4)",
)
parser.add_argument(
"--torch-compile",
action="store_true",
default=False,
help="Enable torch.compile for E2FGVI_HQ model (CUDA only, faster after first run)",
)
parser.add_argument(
"--bf16",
action="store_true",
default=False,
help="Enable BF16 inference for E2FGVI_HQ model (CUDA only, ~2x faster)",
)
args = parser.parse_args()
input_folder = Path(args.input).expanduser().resolve()
output_folder = Path(args.output).expanduser().resolve()
if not input_folder.exists():
print(f"Error: Input folder does not exist: {input_folder}", file=sys.stderr)
sys.exit(1)
if not input_folder.is_dir():
print(f"Error: Input path is not a directory: {input_folder}", file=sys.stderr)
sys.exit(1)
return input_folder, output_folder, args
def main():
input_folder, output_folder, args = validate_args_and_show_help()
pattern = args.pattern
from rich import box
from rich.console import Console
from rich.panel import Panel
from rich.progress import (
BarColumn,
MofNCompleteColumn,
Progress,
ProgressColumn,
SpinnerColumn,
TaskProgressColumn,
TextColumn,
TimeElapsedColumn,
TimeRemainingColumn,
)
from rich.table import Table
from rich.text import Text
from rich.text import Text as RichText
from src.demark_world.core import DeMarkWorld
from src.demark_world.schemas import CleanerType
console = Console()
class SpeedColumnImpl(ProgressColumn):
"""Custom column to display processing speed in it/s format"""
def render(self, task):
if "Overall Progress" in task.description:
return RichText("", style="")
speed = task.finished_speed or task.speed
if speed is None:
return RichText("-- it/s", style="progress.data.speed")
return RichText(f"{speed:.2f} it/s", style="cyan")
class BatchProcessorImpl:
"""Batch video processor with rich progress tracking"""
def __init__(
self,
input_folder: Path,
output_folder: Path,
pattern: str = "*.mp4",
cleaner_type: CleanerType = CleanerType.LAMA,
detect_batch_size: int = 4,
enable_torch_compile: bool = False,
use_bf16: bool = False,
):
self.input_folder = input_folder
self.output_folder = output_folder
self.pattern = pattern
self.demark_world = DeMarkWorld(
cleaner_type=cleaner_type,
enable_torch_compile=enable_torch_compile,
detect_batch_size=detect_batch_size,
use_bf16=use_bf16,
)
self.console = console
self.successful: List[str] = []
self.failed: Dict[str, str] = {}
def show_banner(self):
banner_text = Text()
banner_text.append("DeMark-World", style="bold cyan")
banner_text.append(" - AI Watermark Remover", style="bold magenta")
panel = Panel(
banner_text,
box=box.DOUBLE,
border_style="bright_blue",
padding=(1, 2),
)
console.print(panel)
console.print()
def find_videos(self) -> List[Path]:
video_files = list(self.input_folder.glob(self.pattern))
return sorted(video_files)
def process_batch(self):
self.show_banner()
video_files = self.find_videos()
if not video_files:
console.print(
f"[bold red]No files matching '{self.pattern}' found in {self.input_folder}[/bold red]"
)
return
config_table = Table(show_header=False, box=box.SIMPLE, padding=(0, 1))
config_table.add_row("Input folder:", f"[cyan]{self.input_folder}[/cyan]")
config_table.add_row("Output folder:", f"[green]{self.output_folder}[/green]")
config_table.add_row("Pattern:", f"[yellow]{self.pattern}[/yellow]")
config_table.add_row(
"Videos found:", f"[bold magenta]{len(video_files)}[/bold magenta]"
)
console.print(config_table)
console.print()
self.output_folder.mkdir(parents=True, exist_ok=True)
start_time = datetime.now()
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(bar_width=40),
TaskProgressColumn(),
MofNCompleteColumn(),
SpeedColumnImpl(),
TimeElapsedColumn(),
TimeRemainingColumn(),
console=console,
) as progress:
batch_task = progress.add_task(
"[cyan]Overall Progress", total=len(video_files)
)
for idx, input_path in enumerate(video_files, 1):
output_path = self.output_folder / f"cleaned_{input_path.name}"
progress.update(
batch_task,
description=f"[cyan]Overall Progress ({idx}/{len(video_files)})",
)
console.print(
f"\n[bold blue][{idx}/{len(video_files)}][/bold blue] "
f"[yellow]{input_path.name}[/yellow]"
)
try:
video_task = progress.add_task(
" [green]Processing video", total=100
)
last_progress = [0]
def progress_callback(prog: int):
if prog > last_progress[0]:
progress.update(
video_task, advance=prog - last_progress[0]
)
last_progress[0] = prog
self.demark_world.run(
input_path, output_path, progress_callback, quiet=args.quiet
)
if last_progress[0] < 100:
progress.update(video_task, advance=100 - last_progress[0])
progress.remove_task(video_task)
self.successful.append(input_path.name)
console.print(
f" [bold green]Completed:[/bold green] {output_path.name}"
)
except Exception as e:
progress.remove_task(video_task)
self.failed[input_path.name] = str(e)
console.print(f" [bold red]Error:[/bold red] {e}")
progress.update(batch_task, advance=1)
self._print_summary(start_time)
def _print_summary(self, start_time: datetime):
end_time = datetime.now()
duration = end_time - start_time
console.print()
summary_table = Table(
show_header=False, box=box.ROUNDED, border_style="cyan"
)
summary_table.add_column("Metric", style="bold")
summary_table.add_column("Value")
summary_table.add_row("Total Time", f"[yellow]{duration}[/yellow]")
summary_table.add_row(
"Successful", f"[bold green]{len(self.successful)}[/bold green]"
)
summary_table.add_row(
"Failed", f"[bold red]{len(self.failed)}[/bold red]"
)
summary_table.add_row(
"Total",
f"[bold magenta]{len(self.successful) + len(self.failed)}[/bold magenta]",
)
total = len(self.successful) + len(self.failed)
success_rate = (len(self.successful) / total * 100) if total > 0 else 0
summary_table.add_row(
"Success Rate", f"[bold cyan]{success_rate:.1f}%[/bold cyan]"
)
summary_panel = Panel(
summary_table,
title="[bold white]BATCH PROCESSING SUMMARY[/bold white]",
border_style="bright_cyan",
box=box.DOUBLE,
)
console.print(summary_panel)
if self.successful:
console.print()
success_table = Table(
title="[bold green]Successfully Processed[/bold green]",
box=box.SIMPLE,
show_header=True,
header_style="bold green",
)
success_table.add_column("#", style="dim", width=4)
success_table.add_column("Filename", style="green")
for idx, filename in enumerate(self.successful, 1):
success_table.add_row(str(idx), filename)
console.print(success_table)
if self.failed:
console.print()
failed_table = Table(
title="[bold red]Failed to Process[/bold red]",
box=box.SIMPLE,
show_header=True,
header_style="bold red",
)
failed_table.add_column("#", style="dim", width=4)
failed_table.add_column("Filename", style="red")
failed_table.add_column("Error", style="dim")
for idx, (filename, error) in enumerate(self.failed.items(), 1):
error_msg = error if len(error) < 60 else error[:57] + "..."
failed_table.add_row(str(idx), filename, error_msg)
console.print(failed_table)
console.print()
if len(self.failed) == 0:
console.print(
"[bold green]All videos processed successfully![/bold green]",
justify="center",
)
else:
console.print(
"[bold yellow]Some videos failed to process. Check errors above.[/bold yellow]",
justify="center",
)
console.print()
try:
cleaner_type = (
CleanerType.LAMA if args.model == "lama" else CleanerType.E2FGVI_HQ
)
processor = BatchProcessorImpl(
input_folder,
output_folder,
pattern,
cleaner_type,
detect_batch_size=args.batch_size,
enable_torch_compile=args.torch_compile,
use_bf16=args.bf16,
)
processor.process_batch()
except KeyboardInterrupt:
console.print()
console.print(
"[bold yellow]Processing interrupted by user[/bold yellow]",
justify="center",
)
sys.exit(130)
except Exception as e:
console.print()
console.print(f"[bold red]Fatal error:[/bold red] {e}")
sys.exit(1)
if __name__ == "__main__":
main()