File size: 5,076 Bytes
77dbe7c |
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import csv
import os
def get_channel_ids(input_file: str):
channel_ids = []
if input_file.endswith(".csv"): # CSV file
with open(input_file, "r") as file:
csvreader = csv.DictReader(file)
for row in csvreader:
channel_ids.append(row["channel_id"])
else: # Default format
with open(input_file, "r") as file:
lines = file.readlines()
for line in lines:
channel_ids.append(line.strip())
return channel_ids
def get_video_ids(input_file: str):
video_ids = []
if input_file.endswith(".csv"): # CSV file
with open(input_file, "r") as file:
csvreader = csv.DictReader(file)
for row in csvreader:
video_ids.append(row["video_id"])
else: # Default format
with open(input_file, "r") as file:
lines = file.readlines()
for line in lines:
video_ids.append(line.strip())
return video_ids
def get_file_ids(input_file: str):
file_ids = []
if input_file.endswith(".csv"): # CSV file
with open(input_file, "r") as file:
csvreader = csv.reader(file)
# skip header
next(csvreader)
for row in csvreader:
file_ids.append(row[0])
else: # Default format
with open(input_file, "r") as file:
lines = file.readlines()
for line in lines:
file_ids.append(line.strip())
return file_ids
def get_video_ids_and_start_times(input_file: str):
video_ids = []
start_times = []
lines = []
if input_file.endswith(".csv"): # CSV file
with open(input_file, "r") as file:
csvreader = csv.DictReader(file)
for row in csvreader:
lines.append(row["file_id"])
else: # Default format
with open(input_file, "r") as file:
lines = file.readlines()
for line in lines:
parts = line.rsplit("_", 1)
if len(parts) != 2:
continue
video_ids.append(parts[0])
start_times.append(int(parts[1]))
return video_ids, start_times
def get_video_ids_and_start_times_list(input_file: str):
video_data = {} # Dictionary to store video_id and corresponding start_times_list
with open(input_file, "r") as file:
lines = file.readlines()
for line in lines:
parts = line.strip().split()
if len(parts) != 2:
continue
video_id = parts[0]
start_times = parts[1].split(",")
start_times_list = [int(t) for t in start_times if t.isdigit()]
# Merge start_times_list for same video_id
if video_id in video_data:
video_data[video_id].extend(start_times_list)
else:
video_data[video_id] = start_times_list
# Deduplicate and sort start_times_list for each video_id
for video_id in video_data:
video_data[video_id] = sorted(set(video_data[video_id]))
# Separate results into two lists
file_ids = list(video_data.keys())
start_times_list = list(video_data.values())
return file_ids, start_times_list
def get_video_ids_from_dir(input_dir: str, ext: str):
"""
Extract video IDs from filenames in the specified directory.
Args:
input_dir: Directory containing the files
ext: File extension to filter by (e.g., 'mp4')
Returns:
List of video IDs extracted from filenames
"""
if not ext.startswith("."):
ext = "." + ext
video_ids = []
for file in os.listdir(input_dir):
if file.endswith(ext):
video_ids.append(file.split(".")[0])
return video_ids
def get_video_ids_from_dir_and_start_times(input_dir: str, ext: str):
"""
Extract video IDs and start times from filenames in the specified directory.
Args:
input_dir: Directory containing the files
ext: File extension to filter by (e.g., 'mp4')
Returns:
Tuple of (video_ids, start_times) extracted from filenames
"""
if not ext.startswith("."):
ext = "." + ext
video_ids = []
start_times = []
for file in os.listdir(input_dir):
if file.endswith(ext):
parts = file.split(".")[0].rsplit("_", 1)
if len(parts) != 2:
continue
video_ids.append(parts[0])
start_times.append(int(parts[1]))
return video_ids, start_times
def get_file_ids_from_dir(input_dir: str, ext: str):
"""
Extract file IDs from filenames in the specified directory.
Args:
input_dir: Directory containing the files
ext: File extension to filter by (e.g., 'mp4')
Returns:
List of file IDs extracted from filenames
"""
if not ext.startswith("."):
ext = "." + ext
file_ids = []
for file in os.listdir(input_dir):
if file.endswith(ext):
file_ids.append(file.split(".")[0])
return file_ids |