|
import os |
|
from io import StringIO, BytesIO |
|
from typing import Iterator, TextIO, List, Dict, Any, Optional, Sequence, Union |
|
import base64 |
|
import glob |
|
from tqdm import tqdm |
|
from pytubefix import YouTube, Stream |
|
import cv2 |
|
import json |
|
import textwrap |
|
|
|
|
|
|
|
def format_timestamp(seconds: float, always_include_hours: bool = False, fractionalSeperator: str = '.'): |
|
assert seconds >= 0, "non-negative timestamp expected" |
|
milliseconds = round(seconds * 1000.0) |
|
|
|
hours = milliseconds // 3_600_000 |
|
milliseconds -= hours * 3_600_000 |
|
|
|
minutes = milliseconds // 60_000 |
|
milliseconds -= minutes * 60_000 |
|
|
|
seconds = milliseconds // 1_000 |
|
milliseconds -= seconds * 1_000 |
|
|
|
hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else "" |
|
return f"{hours_marker}{minutes:02d}:{seconds:02d}{fractionalSeperator}{milliseconds:03d}" |
|
|
|
|
|
def _processText(text: str, maxLineWidth=None): |
|
if (maxLineWidth is None or maxLineWidth < 0): |
|
return text |
|
|
|
lines = textwrap.wrap(text, width=maxLineWidth, tabsize=4) |
|
return '\n'.join(lines) |
|
|
|
|
|
def write_vtt(transcript: Iterator[dict], file: TextIO, maxLineWidth=None): |
|
print("WEBVTT\n", file=file) |
|
for segment in transcript: |
|
text = _processText(segment['text'], maxLineWidth).replace('-->', '->') |
|
|
|
print( |
|
f"{format_timestamp(segment['start'])} --> {format_timestamp(segment['end'])}\n" |
|
f"{text}\n", |
|
file=file, |
|
flush=True, |
|
) |
|
|
|
|
|
def getSubs(segments: Iterator[dict], format: str, maxLineWidth: int=-1) -> str: |
|
segmentStream = StringIO() |
|
|
|
if format == 'vtt': |
|
write_vtt(segments, file=segmentStream, maxLineWidth=maxLineWidth) |
|
else: |
|
raise Exception("Unknown format " + format) |
|
|
|
segmentStream.seek(0) |
|
return segmentStream.read() |
|
|
|
def download_video(video_url, path='/tmp/'): |
|
print(f'Getting video information for {video_url}') |
|
if not video_url.startswith('http'): |
|
return os.path.join(path, video_url) |
|
|
|
filepath = glob.glob(os.path.join(path, '*.mp4')) |
|
if len(filepath) > 0: |
|
return filepath[0] |
|
|
|
def progress_callback(stream: Stream, data_chunk: bytes, bytes_remaining: int) -> None: |
|
pbar.update(len(data_chunk)) |
|
|
|
yt = YouTube(video_url, on_progress_callback=progress_callback) |
|
stream = yt.streams.filter(progressive=True, file_extension='mp4', res='720p').desc().first() |
|
if stream is None: |
|
stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first() |
|
if not os.path.exists(path): |
|
os.makedirs(path) |
|
|
|
filepath = os.path.join(path, stream.default_filename) |
|
if not os.path.exists(filepath): |
|
print('Downloading video from YouTube...') |
|
pbar = tqdm(desc='Downloading video from YouTube', total=stream.filesize, unit="bytes") |
|
stream.download(path) |
|
pbar.close() |
|
return filepath |
|
|
|
|
|
def str2time(strtime): |
|
|
|
strtime = strtime.strip('"') |
|
|
|
hrs, mins, seconds = [float(c) for c in strtime.split(':')] |
|
|
|
total_seconds = hrs * 60**2 + mins * 60 + seconds |
|
total_miliseconds = total_seconds * 1000 |
|
return total_miliseconds |
|
|
|
|
|
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA): |
|
|
|
dim = None |
|
(h, w) = image.shape[:2] |
|
|
|
|
|
if width is None and height is None: |
|
return image |
|
|
|
|
|
if width is None: |
|
|
|
r = height / float(h) |
|
dim = (int(w * r), height) |
|
|
|
else: |
|
|
|
r = width / float(w) |
|
dim = (width, int(h * r)) |
|
|
|
|
|
return cv2.resize(image, dim, interpolation=inter) |
|
|
|
def load_json_file(file_path): |
|
|
|
with open(file_path, 'r') as file: |
|
data = json.load(file) |
|
return data |