code-202604_v2 / sync_research /select_random_videos.py
Rakancorle11's picture
Snapshot 20260424-2142
0a4deb9 verified
#!/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()