|
|
from datasets import load_dataset |
|
|
import tqdm |
|
|
from xml.etree.ElementTree import ParseError |
|
|
from youtube_transcript_api._errors import TranscriptsDisabled |
|
|
import json |
|
|
from youtube_transcript_api import YouTubeTranscriptApi |
|
|
|
|
|
dataset = load_dataset("OpenGVLab/InternVid-Full") |
|
|
|
|
|
video_id = set() |
|
|
for idx, row in tqdm.tqdm(enumerate(dataset['train'])): |
|
|
video_id.add(row['YoutubeID']) |
|
|
|
|
|
if len(video_id) == 50000: |
|
|
break |
|
|
|
|
|
video_id_list = list(video_id) |
|
|
|
|
|
def create_transcript(video_id, transcripts): |
|
|
candidate = {} |
|
|
candidate["video_id"] = video_id |
|
|
candidate["transcripts"] = transcripts.to_raw_data() |
|
|
return candidate |
|
|
|
|
|
|
|
|
|
|
|
res = [] |
|
|
prev = "" |
|
|
|
|
|
for video_id in tqdm.tqdm(video_id_list): |
|
|
if video_id == prev: |
|
|
print("sth is wrong") |
|
|
retry = False |
|
|
while True: |
|
|
try: |
|
|
ytt_api = YouTubeTranscriptApi() |
|
|
transcript_list = ytt_api.list(video_id) |
|
|
ok = False |
|
|
for transcript in transcript_list: |
|
|
if transcript.language == "en": |
|
|
data = transcript.fetch() |
|
|
print(f"English is available for this video: {video_id}") |
|
|
res.append(create_transcript(video_id, data)) |
|
|
ok = True |
|
|
break |
|
|
if not ok: |
|
|
for transcript in transcript_list: |
|
|
if transcript.is_translatable: |
|
|
data = transcript.translate('en').fetch() |
|
|
print(f"[TRAN] Translated to English for {video_id}") |
|
|
res.append(create_transcript(video_id, data)) |
|
|
ok = True |
|
|
break |
|
|
if not ok: |
|
|
print(f"There is no English version for the video") |
|
|
break |
|
|
except ParseError as e: |
|
|
if not retry: |
|
|
print(f"[Retry] XML ParseError for {video_id}: {e}. Retrying...") |
|
|
retry = True |
|
|
continue |
|
|
except TranscriptsDisabled as e: |
|
|
print(f"[Skip ] TranscriptsDisabled for {video_id}. Skipping.") |
|
|
break |
|
|
except Exception as e: |
|
|
|
|
|
break |
|
|
prev = video_id |
|
|
|
|
|
|
|
|
|
|
|
print(len(res)) |
|
|
|
|
|
with open("InternVid_1.json", "w") as f: |
|
|
json.dump(res, f) |