Python извлекает автоматические титры с помощью youtube_dl и преобразует их в транскрипт - PullRequest
0 голосов
/ 06 декабря 2018

Автоматические надписи на английском языке, извлеченные из YouTube, содержат не информацию в читаемой форме, а дублированную текстовую информацию.

welcome<00:00:01.790><c> my</c><00:00:02.790><c> name</c><c.colorCCCCCC><00:00:02.820><c> is</c><00:00:03.210><c> Helga</c></c><c.colorE5E5E5><00:00:03.449><c> Vieira</c><00:00:03.929><c> and</c><00:00:04.080><c> this</c></c>

00:00:04.670 --> 00:00:04.680 align:start position:0%
welcome my name<c.colorCCCCCC> is Helga</c><c.colorE5E5E5> Vieira and this
 </c>

Мой код:

def captions_test02(url):
    ydl = youtube_dl.YoutubeDL({'writesubtitles': True, 'allsubtitles': True, 'writeautomaticsub': True})
    res = ydl.extract_info(url, download=False)
    if res['requested_subtitles'] and res['requested_subtitles']['en']:
        print('Grabbing vtt file from ' + res['requested_subtitles']['en']['url'])
        response = requests.get(res['requested_subtitles']['en']['url'], stream=True)
        f1 = open("testfile01.txt", "w")
        f1.write(response.text)
        f1.close()
        if len(res['subtitles']) > 0:
            print('manual captions')
        else:
            print('automatic_captions')
    else:
        print('Youtube Video does not have any english captions')

if __name__ == '__main__':
    captions_test02("https://www.youtube.com/watch?v=tCTqNZW0wIk&t=2s")

Любые предложения, чтобы получить правильныетранскрипт?Начальная точка: https://shkspr.mobi/blog/2018/09/convert-webvtt-to-a-transcript-using-python/

1 Ответ

0 голосов
/ 06 июня 2019

Чтобы исключить метки времени и получить лучшую расшифровку, вы можете использовать регулярное выражение:

    def captions_test02(url):
        ydl = youtube_dl.YoutubeDL({'writesubtitles': True, 'allsubtitles': True, 'writeautomaticsub': True})
        res = ydl.extract_info(url, download=False)
        if res['requested_subtitles'] and res['requested_subtitles']['en']:
            print('Grabbing vtt file from ' + res['requested_subtitles']['en']['url'])
            response = requests.get(res['requested_subtitles']['en']['url'], stream=True)
            f1 = open("testfile01.txt", "w")
            new = re.sub(r'\d{2}\W\d{2}\W\d{2}\W\d{3}\s\W{3}\s\d{2}\W\d{2}\W\d{2}\W\d{3}','',response.text)
            f1.write(new)
            f1.close()
            if len(res['subtitles']) > 0:
                print('manual captions')
            else:
                print('automatic_captions')
        else:
            print('Youtube Video does not have any english captions')

    if __name__ == '__main__':
        captions_test02("https://www.youtube.com/watch?v=d1CDP6sMuLA") 
...