import json import os import shutil import random from pathlib import Path # Load the datasets train = json.load(open('/home/jisoo/demo/VidChain/VTimeLLM/data/activitynet/mdpo-train.json')) val = json.load(open('/home/jisoo/demo/VidChain/VTimeLLM/data/activitynet/val_2.json')) # Source directory where videos are already stored source_dir = Path('/hub_data2/jisoo/data/ActivityNet/videos') # Create directories if they don't exist train_dir = Path('/home/jisoo/demo/VidChain/VTimeLLM/data/activitynet/videos/train') val_dir = Path('/home/jisoo/demo/VidChain/VTimeLLM/data/activitynet/videos/val') train_dir.mkdir(exist_ok=True) val_dir.mkdir(exist_ok=True) def copy_video(video_id, output_dir): """Copy a video from the source directory to the target directory""" # Look for mp4 files first, then other extensions possible_extensions = ['.mp4', '.mkv', '.avi', '.mov'] for ext in possible_extensions: source_path = source_dir / f"{video_id}{ext}" if source_path.exists(): output_path = output_dir / f"{video_id}.mp4" # Skip if file already exists if output_path.exists(): print(f"Video {video_id} already exists in {output_dir}, skipping...") return True try: print(f"Copying {video_id} to {output_dir}...") shutil.copy2(source_path, output_path) print(f"Successfully copied {video_id}") return True except Exception as e: print(f"Error copying {video_id}: {e}") return False print(f"Video {video_id} not found in source directory") return False def copy_sample_videos_from_list(video_ids, output_dir, num_samples=25): """Copy a sample of videos from a list of video IDs""" # Randomly sample videos if len(video_ids) > num_samples: sampled_ids = random.sample(video_ids, num_samples) else: sampled_ids = video_ids print(f"Copying {len(sampled_ids)} videos to {output_dir}...") successful_copies = 0 for video_id in sampled_ids: if copy_video(video_id, output_dir): successful_copies += 1 print(f"Successfully copied {successful_copies}/{len(sampled_ids)} videos") return successful_copies # Set random seed for reproducibility random.seed(42) # Check if source directory exists if not source_dir.exists(): print(f"Error: Source directory {source_dir} does not exist!") exit(1) print(f"Source directory: {source_dir}") print(f"Train directory: {train_dir}") print(f"Validation directory: {val_dir}") # Get video IDs from train dataset (it has a 'vid' field with a list of video IDs) train_video_ids = train['vid'] if 'vid' in train else [] print(f"Found {len(train_video_ids)} video IDs in train dataset") # Get video IDs from validation dataset (it has video IDs as keys) val_video_ids = list(val.keys()) print(f"Found {len(val_video_ids)} video IDs in validation dataset") # Copy sample videos # print("\n=== Copying Train Videos ===") # train_success = copy_sample_videos_from_list(train_video_ids, train_dir, num_samples=100) print("\n=== Copying Validation Videos ===") val_success = copy_sample_videos_from_list(val_video_ids, val_dir, num_samples=50) print(f"\n=== Summary ===") # print(f"Train videos copied: {train_success}/100") print(f"Validation videos copied: {val_success}/25") # print(f"Total videos copied: {train_success + val_success}/125") # List the copied files # print(f"\n=== Copied Train Videos ===") # train_files = list(train_dir.glob("*.mp4")) # for file in train_files: # print(f" {file.name}") print(f"\n=== Copied Validation Videos ===") val_files = list(val_dir.glob("*.mp4")) for file in val_files: print(f" {file.name}") # Show some statistics print(f"\n=== Statistics ===") # print(f"Total train files: {len(train_files)}") print(f"Total validation files: {len(val_files)}") # # Check file sizes # if train_files: # total_train_size = sum(f.stat().st_size for f in train_files) # print(f"Total train directory size: {total_train_size / (1024*1024):.2f} MB") if val_files: total_val_size = sum(f.stat().st_size for f in val_files) print(f"Total validation directory size: {total_val_size / (1024*1024):.2f} MB")