File size: 4,426 Bytes
9308c70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Reconstruct DAVIS PointMotionBench videos from DAVIS 2017 Trainval frames.

Download Trainval 2017 - Images (480p) and Annotations from
https://davischallenge.org/davis2017/code.html, then run this script.

The DAVIS root should have this layout after extraction:
    <davis-root>/
    └── JPEGImages/
        └── 480p/
            └── <sequence>/
                ├── 00000.jpg
                ├── 00001.jpg
                └── ...

Example:
    python davis/reconstruct_davis.py \
        --davis-root /path/to/DAVIS \
        --output-dir davis/videos/input_480p \
        --captions   davis/davis_captions.json
"""

import argparse
import json
import os
import shutil
import subprocess
import sys
from pathlib import Path

TARGET_FPS = 24


def ffmpeg_exe():
    if shutil.which("ffmpeg"):
        return "ffmpeg"
    try:
        import imageio_ffmpeg
        return imageio_ffmpeg.get_ffmpeg_exe()
    except Exception:
        raise RuntimeError(
            "ffmpeg not found; install a system ffmpeg or `pip install imageio-ffmpeg`."
        )


def encode_sequence(ffmpeg, frames_dir, dst, fps):
    frames = sorted(frames_dir.glob("*.jpg"))
    if not frames:
        raise RuntimeError(f"no .jpg frames found in {frames_dir}")

    dst.parent.mkdir(parents=True, exist_ok=True)
    tmp = dst.with_suffix(".tmp.mp4")
    cmd = [
        ffmpeg, "-y", "-loglevel", "error",
        "-framerate", str(fps),
        "-i", str(frames_dir / "%05d.jpg"),
        "-vf", "crop=trunc(iw/2)*2:trunc(ih/2)*2",
        "-c:v", "libx264", "-crf", "18", "-preset", "fast",
        "-pix_fmt", "yuv420p", "-an",
        str(tmp),
    ]
    res = subprocess.run(cmd, capture_output=True)
    if res.returncode != 0:
        if tmp.exists():
            tmp.unlink()
        raise RuntimeError(res.stderr.decode(errors="replace").strip() or "ffmpeg failed")
    os.replace(tmp, dst)


def main():
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument(
        "--davis-root", required=True, type=Path,
        help="DAVIS 2017 root dir containing JPEGImages/480p/"
    )
    parser.add_argument(
        "--output-dir", type=Path,
        default=Path(__file__).resolve().parent / "videos" / "input_480p",
        help="Output directory for mp4 files (default: davis/videos/input_480p/)",
    )
    parser.add_argument(
        "--captions", type=Path,
        default=Path(__file__).resolve().parent / "davis_captions.json",
        help="davis_captions.json (default: alongside this script)",
    )
    parser.add_argument("--fps", type=int, default=TARGET_FPS)
    parser.add_argument("--overwrite", action="store_true", help="Re-encode even if output exists")
    args = parser.parse_args()

    frames_root = args.davis_root / "JPEGImages" / "480p"
    if not frames_root.exists():
        print(f"ERROR: {frames_root} does not exist — check --davis-root points to the DAVIS 2017 root.")
        sys.exit(1)

    captions = json.load(open(args.captions))
    sequences = sorted(captions.keys())
    args.output_dir.mkdir(parents=True, exist_ok=True)
    ffmpeg = ffmpeg_exe()

    print(f"[reconstruct] {len(sequences)} sequences | davis-root={args.davis_root} | out={args.output_dir}")

    done = skipped = failed = 0
    missing = []

    for i, seq in enumerate(sequences, 1):
        dst = args.output_dir / f"{seq}.mp4"
        if dst.exists() and not args.overwrite:
            skipped += 1
            continue

        frames_dir = frames_root / seq
        if not frames_dir.exists():
            missing.append(seq)
            print(f"[{i}/{len(sequences)}] MISSING  {seq}")
            failed += 1
            continue

        try:
            encode_sequence(ffmpeg, frames_dir, dst, args.fps)
            print(f"[{i}/{len(sequences)}] OK  {seq}")
            done += 1
        except Exception as e:
            print(f"[{i}/{len(sequences)}] ERROR  {seq}: {e}")
            failed += 1

    print(f"\n===== summary =====")
    print(f"encoded ok       : {done}")
    print(f"skipped (exists) : {skipped}")
    print(f"missing upstream : {len(missing)}")
    print(f"failed           : {failed}")
    for seq in missing[:20]:
        print(f"  [MISSING] {seq}")
    if missing or failed:
        sys.exit(1)


if __name__ == "__main__":
    main()