File size: 2,503 Bytes
0a4deb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import argparse
import random
import shutil
from pathlib import Path


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Randomly select videos from source folder to destination folder."
    )
    parser.add_argument(
        "--src",
        type=Path,
        default=Path("/home/ubuntu/video/uag_oops"),
        help="Source folder containing videos.",
    )
    parser.add_argument(
        "--dst",
        type=Path,
        default=Path("/home/ubuntu/video/sample"),
        help="Destination folder to place selected videos.",
    )
    parser.add_argument(
        "--count",
        type=int,
        default=100,
        help="Number of videos to select.",
    )
    parser.add_argument(
        "--seed",
        type=int,
        default=None,
        help="Random seed for reproducible sampling.",
    )
    parser.add_argument(
        "--move",
        action="store_true",
        help="Move files instead of copying.",
    )
    return parser.parse_args()


def unique_target_path(dst_dir: Path, src_name: str) -> Path:
    target = dst_dir / src_name
    if not target.exists():
        return target

    stem = target.stem
    suffix = target.suffix
    index = 1
    while True:
        candidate = dst_dir / f"{stem}_{index}{suffix}"
        if not candidate.exists():
            return candidate
        index += 1


def main() -> None:
    args = parse_args()

    if not args.src.exists():
        raise FileNotFoundError(f"Source folder not found: {args.src}")
    if args.count <= 0:
        raise ValueError("--count must be > 0")

    args.dst.mkdir(parents=True, exist_ok=True)

    videos = sorted(args.src.glob("*.mp4"))
    total = len(videos)
    if total == 0:
        print(f"No .mp4 files found in: {args.src}")
        return

    n = min(args.count, total)
    rng = random.Random(args.seed)
    selected = rng.sample(videos, n)

    op_name = "Moved" if args.move else "Copied"
    print(f"Found {total} videos in {args.src}")
    print(f"Selecting {n} videos -> {args.dst}")
    if args.seed is not None:
        print(f"Using seed: {args.seed}")

    for i, src in enumerate(selected, start=1):
        dst = unique_target_path(args.dst, src.name)
        if args.move:
            shutil.move(str(src), str(dst))
        else:
            shutil.copy2(src, dst)
        print(f"[{i}/{n}] {op_name}: {src.name} -> {dst.name}")

    print("Done.")


if __name__ == "__main__":
    main()