Commit
·
847968c
0
Parent(s):
init commit
Browse files- InternVid.json +0 -0
- dataset.py +75 -0
InternVid.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
dataset.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
import tqdm
|
| 3 |
+
from xml.etree.ElementTree import ParseError
|
| 4 |
+
from youtube_transcript_api._errors import TranscriptsDisabled
|
| 5 |
+
import json
|
| 6 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 7 |
+
|
| 8 |
+
dataset = load_dataset("OpenGVLab/InternVid-Full")
|
| 9 |
+
|
| 10 |
+
video_id = set()
|
| 11 |
+
for idx, row in tqdm.tqdm(enumerate(dataset['train'])):
|
| 12 |
+
video_id.add(row['YoutubeID'])
|
| 13 |
+
|
| 14 |
+
if len(video_id) == 50000:
|
| 15 |
+
break
|
| 16 |
+
|
| 17 |
+
video_id_list = list(video_id)
|
| 18 |
+
|
| 19 |
+
def create_transcript(video_id, transcripts):
|
| 20 |
+
candidate = {}
|
| 21 |
+
candidate["video_id"] = video_id
|
| 22 |
+
candidate["transcripts"] = transcripts.to_raw_data()
|
| 23 |
+
return candidate
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
res = []
|
| 28 |
+
prev = ""
|
| 29 |
+
|
| 30 |
+
for video_id in tqdm.tqdm(video_id_list):
|
| 31 |
+
if video_id == prev:
|
| 32 |
+
print("sth is wrong")
|
| 33 |
+
retry = False
|
| 34 |
+
while True:
|
| 35 |
+
try:
|
| 36 |
+
ytt_api = YouTubeTranscriptApi()
|
| 37 |
+
transcript_list = ytt_api.list(video_id)
|
| 38 |
+
ok = False
|
| 39 |
+
for transcript in transcript_list:
|
| 40 |
+
if transcript.language == "en":
|
| 41 |
+
data = transcript.fetch()
|
| 42 |
+
print(f"English is available for this video: {video_id}")
|
| 43 |
+
res.append(create_transcript(video_id, data))
|
| 44 |
+
ok = True
|
| 45 |
+
break
|
| 46 |
+
if not ok:
|
| 47 |
+
for transcript in transcript_list:
|
| 48 |
+
if transcript.is_translatable:
|
| 49 |
+
data = transcript.translate('en').fetch()
|
| 50 |
+
print(f"[TRAN] Translated to English for {video_id}")
|
| 51 |
+
res.append(create_transcript(video_id, data))
|
| 52 |
+
ok = True
|
| 53 |
+
break
|
| 54 |
+
if not ok:
|
| 55 |
+
print(f"There is no English version for the video")
|
| 56 |
+
break
|
| 57 |
+
except ParseError as e:
|
| 58 |
+
if not retry:
|
| 59 |
+
print(f"[Retry] XML ParseError for {video_id}: {e}. Retrying...")
|
| 60 |
+
retry = True
|
| 61 |
+
continue
|
| 62 |
+
except TranscriptsDisabled as e:
|
| 63 |
+
print(f"[Skip ] TranscriptsDisabled for {video_id}. Skipping.")
|
| 64 |
+
break
|
| 65 |
+
except Exception as e:
|
| 66 |
+
# print( print(f"[Error] Unexpected error for {video_id}: {e}. Skipping."))
|
| 67 |
+
break
|
| 68 |
+
prev = video_id
|
| 69 |
+
# if candidate:
|
| 70 |
+
# res.append(candidate)
|
| 71 |
+
|
| 72 |
+
print(len(res))
|
| 73 |
+
|
| 74 |
+
with open("InternVid_1.json", "w") as f:
|
| 75 |
+
json.dump(res, f)
|