File size: 2,401 Bytes
847968c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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:
            # print( print(f"[Error] Unexpected error for {video_id}: {e}. Skipping."))
            break
    prev = video_id
    # if candidate:
    #     res.append(candidate)

print(len(res))

with open("InternVid_1.json", "w") as f:
    json.dump(res, f)