-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubs-2-video.py
80 lines (60 loc) · 2.6 KB
/
subs-2-video.py
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
76
77
78
79
import srt, unidecode, webvtt, os
from datetime import datetime
from unidecode import unidecode
from google_images_download import google_images_download
from cv2 import VideoWriter, VideoWriter_fourcc, imread, resize
def transliterate(string):
"""Transliterates string into his closest representation.
Ex: 1. àé => ae,
2. สวัสดีครับ => swasdiikhrab.
:param string: string
:return: closest string.
"""
if not isinstance(string, bytes):
string = u''.join(string)
return unidecode(string)
def format_text(string):
for txt in transliterate(string).split("\n")[::-1]:
if txt.strip() != "":
return txt
return ""
def make_video(subs, outimg=None, fps=30, size=None,
is_color=True, format="XVID"):
fourcc = VideoWriter_fourcc(*format)
vid = None
total_frames=0
subslist = [{"content":s.text, "start":to_deltatime(s.start), "end": to_deltatime(s.end)} for s in subs if len(s.text.strip()) >0]
for (i, sub) in enumerate(subslist):
image = paths[0][format_text(sub["content"])][0]
if not os.path.exists(image):
raise FileNotFoundError(image)
img = imread(image)
if vid is None:
if size is None:
size = img.shape[1], img.shape[0]
vid = VideoWriter('video.mp4', fourcc, float(fps), size, is_color)
if size[0] != img.shape[1] and size[1] != img.shape[0]:
img = resize(img, size)
print(sub)
if len(subslist) == i + 1:
frames = int(sub["end"].total_seconds()*fps - total_frames)
else:
frames = int(subslist[i+1]["start"].total_seconds()*fps - total_frames)
total_frames = frames + total_frames
for i in range(frames):
vid.write(img)
vid.release()
return vid
def to_deltatime(timestp):
return datetime.strptime(timestp, "%H:%M:%S.%f") - datetime.strptime("00:00:00.000", "%H:%M:%S.%f")
def get_keywords_from_subs(subs):
return ",".join([format_text(s.text) for s in subs if len(s.text.strip()) >0])
# with open("captions.srt") as f:
# subs = srt.parse(f.read())
subs = webvtt.read('captions.vtt')
response = google_images_download.googleimagesdownload()
arguments = {"keywords":get_keywords_from_subs(subs), "limit":1,"print_urls":True} #creating list of arguments
paths = response.download(arguments) #passing the arguments to the function
print(paths) #printing absolute paths of the downloaded images
subs = webvtt.read('captions.vtt')
make_video(subs, outimg=None, fps=2, size=None,is_color=True, format="XVID")