-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoSummarizer.py
More file actions
177 lines (140 loc) · 5.09 KB
/
Copy pathVideoSummarizer.py
File metadata and controls
177 lines (140 loc) · 5.09 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from __future__ import unicode_literals
import os
import re
from itertools import starmap
import pysrt
import chardet
import nltk
nltk.download('punkt')
srt_filename = "deep.srt"
srt_file = pysrt.open(srt_filename)
video_file = "deep.mp4"
from moviepy.editor import VideoFileClip, concatenate_videoclips
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.nlp.stemmers import Stemmer
from sumy.utils import get_stop_words
from sumy.summarizers.kl import KLSummarizer
def summarize(srt_file, n_sentences, language="english"):
""" Generating Text summary after conversion of srt file to txt
Args:
srt_file(str) : The name of the SRT FILE
n_sentences(int): No of sentences
language(str) : Language of subtitles (default to English)
Returns:
list: Top Sentences from the subtitles along with the index and
time stamps
"""
parser = PlaintextParser.from_string(
srt_to_txt(srt_file), Tokenizer(language))
stemmer = Stemmer(language)
summarizer = KLSummarizer(stemmer)
summarizer.stop_words = get_stop_words(language)
segment = []
for sentence in summarizer(parser.document, n_sentences):
index = int(re.findall("\(([0-9]+)\)", str(sentence))[0])
item = srt_file[index]
segment.append(srt_segment_to_range(item))
return segment
def srt_to_txt(srt_file):
""" srt to text conversion
Args:
srt_file(str): The name of the SRT FILE
Returns:
str: extracted text from subtitles file
"""
text = ''
for index, item in enumerate(srt_file):
if item.text.startswith("["):
continue
text += "(%d) " % index
text += item.text.replace("\n", "").strip("...").replace(
".", "").replace("?", "").replace("!", "")
text += ". "
return text
def srt_segment_to_range(item):
""" Accessing time stamps (END and START stamps) for each sentence
selected during summarization (summarize function)
Args:
item():
Returns:
int: starting time stamp
int: ending time stamp
"""
start_segment = item.start.hours * 60 * 60 + item.start.minutes * \
60 + item.start.seconds + item.start.milliseconds / 1000.0
end_segment = item.end.hours * 60 * 60 + item.end.minutes * \
60 + item.end.seconds + item.end.milliseconds / 1000.0
return start_segment, end_segment
def time_regions(regions):
""" Calulating Duration for each time segment by subtracting star and
end time stamps
Args:
regions():
Returns:
float: duration of segments
"""
return sum(starmap(lambda start, end: end - start, regions))
def find_summary_regions(srt_filename, duration=30, language="english"):
""" Finding the summary regions selected in the txt version
in the srt file for video trimming
Args:
srt_filename(str): Name of the SRT FILE
duration(int): Time duration
language(str): Language of subtitles (default to English)
Returns:
list: segment of subtitles as "summary"
"""
srt_file = pysrt.open(srt_filename)
enc = chardet.detect(open(srt_filename, "rb").read())['encoding']
srt_file = pysrt.open(srt_filename, encoding=enc)
# generate average subtitle duration
subtitle_duration = time_regions(
map(srt_segment_to_range, srt_file)) / len(srt_file)
# compute number of sentences in the summary file
n_sentences = duration / subtitle_duration
summary = summarize(srt_file, n_sentences, language)
total_time = time_regions(summary)
too_short = total_time < duration
if too_short:
while total_time < duration:
n_sentences += 1
summary = summarize(srt_file, n_sentences, language)
total_time = time_regions(summary)
else:
while total_time > duration:
n_sentences -= 1
summary = summarize(srt_file, n_sentences, language)
total_time = time_regions(summary)
return summary
def create_summary(filename, regions):
""" Creating the video summary by cutting the segments
and then concatenating them
Args:
filename(str): filename
regions():
Returns:
VideoFileClip: joined subclips in segment
"""
subclips = []
input_video = VideoFileClip(filename)
last_end = 0
for (start, end) in regions:
subclip = input_video.subclip(start, end)
subclips.append(subclip)
last_end = end
return concatenate_videoclips(subclips)
def get_summary(filename=video_file, subtitles=srt_filename, duration = 300):
"""
Final function to put all of this together
"""
regions = find_summary_regions(subtitles, duration, "english")
summary = create_summary(filename, regions)
base, ext = os.path.splitext(filename)
output = "{0}_summary.mp4".format(base)
summary.to_videofile(
output,
codec="libx264",
temp_audiofile="temp.m4a", remove_temp=True, audio_codec="aac")
return True
get_summary()