File size: 4,348 Bytes
fca4fc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

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")