Skip to content

Commit

Permalink
Merge pull request #413 from amspsingh04/patch-1
Browse files Browse the repository at this point in the history
Update captions.py
  • Loading branch information
JuanBindez authored Jan 13, 2025
2 parents 1e0b0d2 + 5b409b0 commit 1e3863a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pytubefix/captions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import time
import json
import re
import xml.etree.ElementTree as ElementTree
from html import unescape
from typing import Dict, Optional
Expand Down Expand Up @@ -62,6 +63,22 @@ def generate_srt_captions(self) -> str:
recompiles them into the "SubRip Subtitle" format.
"""
return self.xml_caption_to_srt(self.xml_captions)

def generate_txt_captions(self) -> str:
"""Generate Text captions.
Takes the "SubRip Subtitle" format captions and converts them into text
"""
srt_captions = self.generate_srt_captions()
lines = srt_captions.splitlines()
text = ''
for line in lines:
if re.search('^[0-9]+$', line) is None and \
re.search('^[0-9]{2}:[0-9]{2}:[0-9]{2}', line) is None and \
re.search('^$', line) is None:
text += ' ' + line.strip()
text = text.lstrip()
return text.strip()

def save_captions(self, filename: str):
"""Generate and save "SubRip Subtitle" captions to a text file.
Expand Down

0 comments on commit 1e3863a

Please sign in to comment.