chop videos in chunks
Browse files- yttemporal180m.py +55 -33
yttemporal180m.py
CHANGED
|
@@ -20,15 +20,16 @@ _URL_BASE = "https://rowanzellers.com/merlot/#data"
|
|
| 20 |
|
| 21 |
url_numbers = ["00" + str(i) if i < 10 else "0" + str(i) for i in range(100)]
|
| 22 |
_DL_URLS = [
|
| 23 |
-
f"https://storage.googleapis.com/merlot/yttemporal180m/yttemporal180m_{num}of100.jsonl.gz"
|
|
|
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
def json_serializer(o):
|
| 27 |
if isinstance(o, datetime):
|
| 28 |
return str(o)
|
| 29 |
|
| 30 |
-
raise TypeError(
|
| 31 |
-
f"Object of type {o.__class__.__name__} is not JSON serializable")
|
| 32 |
|
| 33 |
|
| 34 |
class yttemporal180mConfig(datasets.BuilderConfig):
|
|
@@ -36,7 +37,8 @@ class yttemporal180mConfig(datasets.BuilderConfig):
|
|
| 36 |
|
| 37 |
def __init__(self, **kwargs):
|
| 38 |
super(yttemporal180mConfig, self).__init__(
|
| 39 |
-
version=datasets.Version("2.1.0", ""), **kwargs
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
class yttemporal180m(datasets.GeneratorBasedBuilder):
|
|
@@ -44,7 +46,8 @@ class yttemporal180m(datasets.GeneratorBasedBuilder):
|
|
| 44 |
DEFAULT_CONFIG_NAME = "default"
|
| 45 |
BUILDER_CONFIGS = [
|
| 46 |
yttemporal180mConfig(
|
| 47 |
-
name="default", description="Default full yttemporal180m dataset"
|
|
|
|
| 48 |
]
|
| 49 |
|
| 50 |
def _info(self):
|
|
@@ -54,8 +57,9 @@ class yttemporal180m(datasets.GeneratorBasedBuilder):
|
|
| 54 |
{
|
| 55 |
"video_id": datasets.Value("string"),
|
| 56 |
"video_url": datasets.Value("string"),
|
| 57 |
-
"
|
| 58 |
-
"
|
|
|
|
| 59 |
"meta": datasets.Value("string"),
|
| 60 |
}
|
| 61 |
),
|
|
@@ -65,15 +69,12 @@ class yttemporal180m(datasets.GeneratorBasedBuilder):
|
|
| 65 |
)
|
| 66 |
|
| 67 |
def _split_generators(self, dl_manager):
|
| 68 |
-
archive_paths = [dl_manager.download_and_extract(
|
| 69 |
-
url) for url in _DL_URLS]
|
| 70 |
|
| 71 |
train_split = [
|
| 72 |
datasets.SplitGenerator(
|
| 73 |
name=datasets.Split.TRAIN,
|
| 74 |
-
gen_kwargs={
|
| 75 |
-
"jsonl_files": archive_paths
|
| 76 |
-
},
|
| 77 |
)
|
| 78 |
]
|
| 79 |
|
|
@@ -88,26 +89,47 @@ class yttemporal180m(datasets.GeneratorBasedBuilder):
|
|
| 88 |
for json_str in json_list:
|
| 89 |
infos = json.loads(json_str)
|
| 90 |
|
| 91 |
-
id = infos[
|
| 92 |
url = "https://www.youtube.com/watch?v=" + id
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
}
|
| 102 |
-
yield idx, {
|
| 103 |
-
"video_id": id,
|
| 104 |
-
"video_url": url,
|
| 105 |
-
"asr": asr,
|
| 106 |
-
"title": infos['info']['title'],
|
| 107 |
-
"meta": json.dumps(
|
| 108 |
-
metadata_dict,
|
| 109 |
-
default=json_serializer,
|
| 110 |
-
indent=2
|
| 111 |
)
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
url_numbers = ["00" + str(i) if i < 10 else "0" + str(i) for i in range(100)]
|
| 22 |
_DL_URLS = [
|
| 23 |
+
f"https://storage.googleapis.com/merlot/yttemporal180m/yttemporal180m_{num}of100.jsonl.gz"
|
| 24 |
+
for num in url_numbers
|
| 25 |
+
]
|
| 26 |
|
| 27 |
|
| 28 |
def json_serializer(o):
|
| 29 |
if isinstance(o, datetime):
|
| 30 |
return str(o)
|
| 31 |
|
| 32 |
+
raise TypeError(f"Object of type {o.__class__.__name__} is not JSON serializable")
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
class yttemporal180mConfig(datasets.BuilderConfig):
|
|
|
|
| 37 |
|
| 38 |
def __init__(self, **kwargs):
|
| 39 |
super(yttemporal180mConfig, self).__init__(
|
| 40 |
+
version=datasets.Version("2.1.0", ""), **kwargs
|
| 41 |
+
)
|
| 42 |
|
| 43 |
|
| 44 |
class yttemporal180m(datasets.GeneratorBasedBuilder):
|
|
|
|
| 46 |
DEFAULT_CONFIG_NAME = "default"
|
| 47 |
BUILDER_CONFIGS = [
|
| 48 |
yttemporal180mConfig(
|
| 49 |
+
name="default", description="Default full yttemporal180m dataset"
|
| 50 |
+
),
|
| 51 |
]
|
| 52 |
|
| 53 |
def _info(self):
|
|
|
|
| 57 |
{
|
| 58 |
"video_id": datasets.Value("string"),
|
| 59 |
"video_url": datasets.Value("string"),
|
| 60 |
+
"caption": datasets.Value("string"),
|
| 61 |
+
"timestamp_start": datasets.Value("float32"),
|
| 62 |
+
"timestamp_stop": datasets.Value("float32"),
|
| 63 |
"meta": datasets.Value("string"),
|
| 64 |
}
|
| 65 |
),
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
def _split_generators(self, dl_manager):
|
| 72 |
+
archive_paths = [dl_manager.download_and_extract(url) for url in _DL_URLS]
|
|
|
|
| 73 |
|
| 74 |
train_split = [
|
| 75 |
datasets.SplitGenerator(
|
| 76 |
name=datasets.Split.TRAIN,
|
| 77 |
+
gen_kwargs={"jsonl_files": archive_paths},
|
|
|
|
|
|
|
| 78 |
)
|
| 79 |
]
|
| 80 |
|
|
|
|
| 89 |
for json_str in json_list:
|
| 90 |
infos = json.loads(json_str)
|
| 91 |
|
| 92 |
+
id = infos["info"]["display_id"]
|
| 93 |
url = "https://www.youtube.com/watch?v=" + id
|
| 94 |
+
|
| 95 |
+
# Divide video by segments of 15 sec
|
| 96 |
+
max_sec_per_segment = 15
|
| 97 |
+
num_chunks = (
|
| 98 |
+
int(
|
| 99 |
+
divmod(infos["subtitles"][-1]["time"], max_sec_per_segment)[
|
| 100 |
+
0
|
| 101 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
)
|
| 103 |
+
+ 1
|
| 104 |
+
)
|
| 105 |
+
time_chunks = [
|
| 106 |
+
i * max_sec_per_segment for i in range(num_chunks + 1)
|
| 107 |
+
]
|
| 108 |
+
time_chunk_idx = 0
|
| 109 |
+
caption = ""
|
| 110 |
+
for el in infos["subtitles"]:
|
| 111 |
+
if el["time"] <= time_chunks[time_chunk_idx + 1]:
|
| 112 |
+
caption += el["word"] + " "
|
| 113 |
+
else:
|
| 114 |
+
timestamp_start = float(time_chunks[time_chunk_idx])
|
| 115 |
+
timestamp_stop = float(time_chunks[time_chunk_idx + 1])
|
| 116 |
+
time_chunk_idx += 1
|
| 117 |
+
|
| 118 |
+
metadata_dict = {
|
| 119 |
+
"asr_info": infos["denoised"],
|
| 120 |
+
"info": infos["info"],
|
| 121 |
+
"subtitles": infos["subtitles"],
|
| 122 |
+
"title": infos["info"]["title"],
|
| 123 |
+
}
|
| 124 |
+
yield idx, {
|
| 125 |
+
"video_id": id,
|
| 126 |
+
"video_url": url,
|
| 127 |
+
"caption": caption,
|
| 128 |
+
"timestamp_start": timestamp_start,
|
| 129 |
+
"timestamp_stop": timestamp_stop,
|
| 130 |
+
"meta": json.dumps(
|
| 131 |
+
metadata_dict, default=json_serializer, indent=2
|
| 132 |
+
),
|
| 133 |
+
}
|
| 134 |
+
idx += 1
|
| 135 |
+
caption = ""
|