|
|
from time_r1.utils.qwen_vl_utils_prepare import floor_by_factor, FRAME_FACTOR, smart_resize, ceil_by_factor |
|
|
import decord |
|
|
import torch |
|
|
import os |
|
|
import tqdm |
|
|
import glob |
|
|
import multiprocessing |
|
|
from torchvision import io, transforms |
|
|
from torchvision.transforms import InterpolationMode |
|
|
from functools import partial |
|
|
from time_r1.utils.io import load_jsonl |
|
|
import debugpy |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FPS_MIN_FRAMES = 4 |
|
|
FPS_MAX_FRAMES = 120 |
|
|
FRAME_FACTOR = 2 |
|
|
def get_video_tensor(video_path, target_fps=2, image_factor = 28, min_pixels = 28 * 28 * 4, max_pixels = 28 * 28 * 128): |
|
|
""" |
|
|
将视频以固定帧率提前抽帧、解码保存为tensor,用于后续训练 |
|
|
""" |
|
|
vr = decord.VideoReader(video_path) |
|
|
total_frames, video_fps = len(vr), vr.get_avg_fps() |
|
|
|
|
|
|
|
|
min_nframes = ceil_by_factor(FPS_MIN_FRAMES, FRAME_FACTOR) |
|
|
max_frames = floor_by_factor(min(FPS_MAX_FRAMES, total_frames), FRAME_FACTOR) |
|
|
nframes = total_frames / video_fps * target_fps |
|
|
if nframes > total_frames: |
|
|
print(f"smart_nframes: nframes[{nframes}] > total_frames[{total_frames}]") |
|
|
|
|
|
nframes = min(min(max(nframes, min_nframes), max_frames), total_frames) |
|
|
nframes = floor_by_factor(nframes, FRAME_FACTOR) |
|
|
|
|
|
frame_idx = torch.linspace(0, total_frames - 1, nframes).round().long().tolist() |
|
|
frame_tensor = vr.get_batch(frame_idx).asnumpy() |
|
|
frame_tensor = torch.tensor(frame_tensor).permute(0, 3, 1, 2) |
|
|
sample_fps = nframes / max(total_frames, 1e-6) * video_fps |
|
|
height, width = frame_tensor.shape[2], frame_tensor.shape[3] |
|
|
resized_height, resized_width = smart_resize( |
|
|
height, |
|
|
width, |
|
|
factor=image_factor, |
|
|
min_pixels=min_pixels, |
|
|
max_pixels=max_pixels, |
|
|
) |
|
|
frame_tensor = transforms.functional.resize( |
|
|
frame_tensor, |
|
|
[resized_height, resized_width], |
|
|
interpolation=InterpolationMode.BICUBIC, |
|
|
antialias=True, |
|
|
) |
|
|
frame_cache = { |
|
|
"frame_tensor": frame_tensor, |
|
|
"fps": sample_fps, |
|
|
} |
|
|
return frame_cache |
|
|
|
|
|
|
|
|
def process_single_video(video_path, target_fps=1, image_factor = 28, min_pixels = 28 * 28 * 128, max_pixels = 28 * 28 * 256): |
|
|
save_dir = '/data/shuimu.chen/LongVideoBench/video_cache_300p_120' |
|
|
os.makedirs(save_dir, exist_ok=True) |
|
|
video_filename = os.path.basename(video_path) |
|
|
save_path = os.path.join(save_dir, video_filename + '.frame_cache') |
|
|
"""Helper function to process and save frame cache for a single video.""" |
|
|
print(f"Processing {video_path}...") |
|
|
try: |
|
|
frame_cache = get_video_tensor(video_path, target_fps, image_factor, min_pixels, max_pixels) |
|
|
torch.save(frame_cache, save_path) |
|
|
print(f"Successfully saved frame cache for {video_path}") |
|
|
except Exception as e: |
|
|
print(f"Error processing {video_path}: {e}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_frame_cache(video_root, dataset_path=None, num_workers=8, target_fps=1, overwrite=False, image_factor = 28, min_pixels = 28 * 28 * 128, max_pixels = 28 * 28 * 256): |
|
|
if dataset_path is not None: |
|
|
|
|
|
import json |
|
|
with open(dataset_path, 'r', encoding='utf-8') as f: |
|
|
video_data = json.load(f) |
|
|
|
|
|
|
|
|
video_paths = set() |
|
|
for v in video_data: |
|
|
if "video_path" in v: |
|
|
video_paths.add(v["video_path"]) |
|
|
elif "video" in v: |
|
|
|
|
|
video_path = os.path.join(video_root, v["video"]) |
|
|
video_paths.add(video_path) |
|
|
|
|
|
video_list = list(video_paths) |
|
|
else: |
|
|
video_list = glob.glob(os.path.join(video_root, "*.mp4")) |
|
|
if not video_list: |
|
|
print(f"No MP4 videos found in {video_root}") |
|
|
return |
|
|
|
|
|
if not overwrite: |
|
|
print("skipping videos that already have frame cache") |
|
|
num_total = len(video_list) |
|
|
video_list = [v for v in video_list if not os.path.exists(v + ".frame_cache")] |
|
|
num_skipped = num_total - len(video_list) |
|
|
print(f"skipped {num_skipped} videos") |
|
|
|
|
|
if num_workers is None: |
|
|
num_workers = multiprocessing.cpu_count() |
|
|
|
|
|
print(f"Found {len(video_list)} videos. Starting processing with {num_workers} workers...") |
|
|
|
|
|
|
|
|
with multiprocessing.Pool(processes=num_workers) as pool: |
|
|
|
|
|
|
|
|
|
|
|
func = partial(process_single_video, target_fps=target_fps, image_factor = image_factor, min_pixels = min_pixels, max_pixels = max_pixels) |
|
|
list(tqdm.tqdm(pool.imap_unordered(func, video_list), total=len(video_list))) |
|
|
|
|
|
print("All videos processed.") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
import fire |
|
|
fire.Fire(prepare_frame_cache) |
|
|
|