File size: 4,050 Bytes
13b72f5 |
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 |
# -----------------------------------------------------------------------------
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
# -----------------------------------------------------------------------------
import argparse
import ast
import csv
import json
import os
import shutil
import subprocess
from tqdm import tqdm
def get_vid_mapping(input_video_dir):
"""
Create mapping from video ID to local file path.
Args:
input_video_dir: Directory containing downloaded videos organized in folders
Returns:
vid_mapping: Dictionary mapping video IDs to local file paths
"""
print("mapping video id to local path")
vid_mapping = {}
folders = [x for x in os.listdir(input_video_dir) if os.path.isdir(f"{input_video_dir}/{x}")]
for folder in sorted(folders):
vids = [x for x in os.listdir(f"{input_video_dir}/{folder}") if x[-4:] == ".mp4"]
for vid in tqdm(sorted(vids)):
vid_path = f"{input_video_dir}/{folder}/{vid}"
vid_json = f"{input_video_dir}/{folder}/{vid[:-4]}.json"
# Extract video ID from URL in JSON metadata
vid_name = json.load(open(vid_json))["url"].split("?v=")[-1]
vid_mapping[vid_name] = vid_path
return vid_mapping
def extract(url_list, input_video_dir, output_frame_parent, uid_mapping):
"""
Extract frames from videos at 12 fps.
Args:
url_list: CSV file containing video URLs and timestamps
input_video_dir: Directory containing downloaded videos
output_frame_parent: Output directory for extracted frames
uid_mapping: CSV file mapping timestamps to unique IDs
"""
vid_mapping = get_vid_mapping(input_video_dir)
with open(uid_mapping, "r") as file2:
with open(url_list, "r") as file:
csv_reader = csv.reader(file)
csv_reader2 = csv.reader(file2)
for i, (row, row2) in enumerate(tqdm(zip(csv_reader, csv_reader2))):
# Skip header row and empty rows
if i == 0 or len(row) == 0:
continue
try:
full_vid_path = vid_mapping[row[0]]
except KeyError:
if i % 10 == 0:
print("video not found, skipping", row[0])
continue
print("extracting", row[0])
for j, timestamps in enumerate(ast.literal_eval(row[2])):
uid = row2[j]
# Copy video with unique ID name
vid_parent = os.path.dirname(os.path.dirname(full_vid_path))
vid_uid_path = shutil.copyfile(full_vid_path, f"{vid_parent}/{uid}.mp4")
os.makedirs(f"{output_frame_parent}/{uid}", exist_ok=True)
# Extract frames using ffmpeg
cmd = f"nice -n 19 ffmpeg -i {vid_uid_path} -q:v 2 -vf fps=12 {output_frame_parent}/{uid}/%05d.jpg"
subprocess.run(cmd, shell=True, capture_output=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--url_list", type=str, help="path for url list")
parser.add_argument("--input_video_dir", type=str, help="video directory")
parser.add_argument("--output_frame_parent", type=str, help="parent for output frames")
parser.add_argument("--uid_mapping", type=str, help="path for uid mapping")
args = parser.parse_args()
print("args", args)
extract(args.url_list, args.input_video_dir, args.output_frame_parent, args.uid_mapping)
|