|
|
import json |
|
|
import os |
|
|
import glob |
|
|
|
|
|
def get_valid_episode_indexes(): |
|
|
"""Read the episodes.jsonl file and extract valid episode indexes.""" |
|
|
valid_indexes = set() |
|
|
|
|
|
try: |
|
|
with open('meta/episodes.jsonl', 'r', encoding='utf-8') as f: |
|
|
for line_num, line in enumerate(f, 1): |
|
|
line = line.strip() |
|
|
if not line: |
|
|
continue |
|
|
try: |
|
|
episode_data = json.loads(line) |
|
|
if 'episode_index' in episode_data: |
|
|
valid_indexes.add(episode_data['episode_index']) |
|
|
else: |
|
|
print(f"Warning: No 'episode_index' found on line {line_num}") |
|
|
except json.JSONDecodeError as e: |
|
|
print(f"Warning: Could not parse JSON on line {line_num}: {e}") |
|
|
print(f"Line content: '{line[:100]}...' (truncated)") |
|
|
continue |
|
|
except FileNotFoundError: |
|
|
print("Error: meta/episodes.jsonl file not found!") |
|
|
return set() |
|
|
except Exception as e: |
|
|
print(f"Error reading episodes.jsonl: {e}") |
|
|
return set() |
|
|
|
|
|
return valid_indexes |
|
|
|
|
|
def cleanup_video_files(): |
|
|
"""Remove video files that don't correspond to valid episode indexes.""" |
|
|
valid_indexes = get_valid_episode_indexes() |
|
|
|
|
|
if not valid_indexes: |
|
|
print("No valid episode indexes found. Exiting.") |
|
|
return |
|
|
|
|
|
print(f"Found {len(valid_indexes)} valid episode indexes: {sorted(list(valid_indexes)[:10])}{'...' if len(valid_indexes) > 10 else ''}") |
|
|
|
|
|
|
|
|
video_dirs = glob.glob('videos/chunk-*/observation.images.base/') |
|
|
|
|
|
if not video_dirs: |
|
|
print("No video directories found matching pattern 'videos/chunk-*/observation.images.base/'") |
|
|
return |
|
|
|
|
|
print(f"Found {len(video_dirs)} video directories") |
|
|
|
|
|
removed_files = [] |
|
|
kept_files = [] |
|
|
|
|
|
for video_dir in video_dirs: |
|
|
print(f"\nProcessing directory: {video_dir}") |
|
|
|
|
|
video_files = glob.glob(os.path.join(video_dir, 'episode_*.mp4')) |
|
|
|
|
|
for video_file in video_files: |
|
|
|
|
|
filename = os.path.basename(video_file) |
|
|
|
|
|
episode_num_str = filename.replace('episode_', '').replace('.mp4', '') |
|
|
|
|
|
try: |
|
|
|
|
|
episode_num = int(episode_num_str.lstrip('0') or '0') |
|
|
|
|
|
if episode_num not in valid_indexes: |
|
|
print(f" Removing: {filename} (episode {episode_num} not in valid list)") |
|
|
os.remove(video_file) |
|
|
removed_files.append(video_file) |
|
|
else: |
|
|
print(f" Keeping: {filename} (episode {episode_num} is valid)") |
|
|
kept_files.append(video_file) |
|
|
|
|
|
except ValueError: |
|
|
print(f" Warning: Could not parse episode number from {filename}") |
|
|
|
|
|
print(f"\n" + "="*50) |
|
|
print(f"CLEANUP SUMMARY:") |
|
|
print(f"Files removed: {len(removed_files)}") |
|
|
print(f"Files kept: {len(kept_files)}") |
|
|
|
|
|
if removed_files: |
|
|
print(f"\nRemoved files:") |
|
|
for file in removed_files: |
|
|
print(f" - {file}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
cleanup_video_files() |