# Pass 2: download FineVideo shards one by one, write mapped videos as media/finevideo/.mp4, delete shard. import json, os, shutil, collections from concurrent.futures import ThreadPoolExecutor from huggingface_hub import hf_hub_download import pyarrow.parquet as pq CAP = 14_500_000_000_000 OUT = '/root/datasets/media/finevideo' TMP = '/root/datasets/_downloads/finevideo_tmp' os.makedirs(OUT, exist_ok=True) m = json.load(open('/root/datasets/tools/finevideo_map.json'))['map'] by_shard = collections.defaultdict(lambda: collections.defaultdict(list)) for vid_id, (shard, fn, _, _) in m.items(): by_shard[shard][fn].append(vid_id) def work(shard): want = by_shard[shard] if all(os.path.exists(f'{OUT}/{i}.mp4') for ids in want.values() for i in ids): return shard, 'skip' if os.path.exists('/root/datasets/tools/STOP') or shutil.disk_usage('/').used + 5_000_000_000 > CAP: return shard, 'REFUSE' d = f'{TMP}/{shard}' p = hf_hub_download('HuggingFaceFV/finevideo', f'data/{shard}', repo_type='dataset', local_dir=d) t = pq.read_table(p, columns=['mp4', 'json']) for mp4, js in zip(t.column('mp4').to_pylist(), t.column('json').to_pylist()): for i in want.get(js['original_video_filename'], ()): with open(f'{OUT}/{i}.mp4.part', 'wb') as f: f.write(mp4) os.replace(f'{OUT}/{i}.mp4.part', f'{OUT}/{i}.mp4') shutil.rmtree(d) return shard, 'ok' with ThreadPoolExecutor(6) as ex: for n, (shard, st) in enumerate(ex.map(work, sorted(by_shard))): print(n, shard, st, flush=True) if st == 'REFUSE': break print('FINEVIDEO_DONE', len(os.listdir(OUT)), flush=True)