-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake_video.py
executable file
·183 lines (153 loc) · 6.26 KB
/
make_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
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
176
177
178
179
180
181
182
183
"""
Script to create the movie
"""
from subprocess import call
import random
import os
import shutil
import sys
#import datetime
from PIL import Image
from gtts import gTTS
from mutagen.mp3 import MP3
#import urllib.request
from moviepy.editor import *
import textwrap
from moviepy.video.tools.drawing import color_gradient
#import numpy as np
from skimage import transform as tf
from newspaper import Article
def create_movie(lang_choice, url, directory):
path_directory = 'videos/' + directory
if not os.path.exists(path_directory):
os.makedirs(path_directory)
# RESOLUTION
W = 1280
H = 720
moviesize = W, H
article = Article(url)
article.download()
article.parse()
authors = article.authors
title = article.title
top_image = article.top_image
images = article.images
try:
article.nlp()
except:
print('Error NLP')
pass
keywords = article.keywords
list_keywords = ', '.join(keywords)
txt_nowrap = article.text
description = txt_nowrap[:500].replace('\n\n', '\n')
print('Text >> ' + description + "...\n")
txt = '\n'.join(textwrap.wrap(txt_nowrap, 60)).replace(" ", " ")
# Add blanks
txt = 10*"\n" + txt + 10*"\n"
print("Title >> " + title)
if os.path.exists('mp3/' + title + '.mp3') is False:
if txt_nowrap != '':
speed_slow = False
#if lang_choice[:2] == "en":
# speed_slow = True
tts = gTTS(text=txt_nowrap, lang=lang_choice, slow=speed_slow)
tts.save('mp3/' + title + ".mp3")
try:
audio = MP3('mp3/' +title + ".mp3")
audio_length = int(audio.info.length)
except:
pass
# CREATE THE TEXT IMAGE
clip_txt = TextClip(txt,color='white', bg_color='black' ,align='Center', fontsize=20, font='Droid Sans', method='label')
title_txt = TextClip(title,color='white', bg_color='red', align='Center', fontsize=40, font='Droid Sans', method='label').margin(top=5, opacity=0)
# SCROLL THE TEXT IMAGE BY CROPPING A MOVING AREA
txt_speed = 4
fl = lambda gf,t : gf(t)[int(txt_speed*t):int(txt_speed*t)+int(H*90/100),:]
moving_txt = clip_txt.fl(fl, apply_to=['mask'])
# BACKGROUND IMAGE, DARKENED AT 60%
background_image = ImageClip('background.jpg')
background_darkened = background_image.fl_image(lambda pic: (0.6*pic).astype('int16'))
#print('Number of images >> ' + str(len(list_images)))
list_images_new = []
files_folder_background = os.listdir('background')
for i in range(15):
random_number = random.randint(0, len(files_folder_background))
list_images_new.append('background/' + files_folder_background[random_number-1])
list_images_new.append('background.jpg')
timing_duration = int(audio_length/len(list_images_new))
print('Timing Duration >> ' + str(timing_duration))
print('New number of images >> ' + str(len(list_images_new)))
logo = ImageClip('logo_' + lang_choice + '.png').set_position(('left','center'))
clips = []
for j in range (len(list_images_new)):
print(list_images_new[j])
slide = ImageClip(list_images_new[j]).set_duration(timing_duration).set_start(timing_duration * j).set_position('center').crossfadein(.5)
clips.append(slide)
clips.append(moving_txt.set_position(('center','bottom')).margin(bottom=45, opacity=0))
clips.append(title_txt.set_position(('center','top')))
clips.append(logo)
videoclip = CompositeVideoClip(clips, moviesize)
video_file_save = path_directory + '/' + title + ".avi"
videoclip.set_duration(audio_length).write_videofile(video_file_save, fps=5, codec='libx264',
audio='mp3/' + title + '.mp3', audio_codec='aac', temp_audiofile='mp3/' + title +'.mp3', remove_temp=True)
print('Title >> ' + title)
print('Keywords >> ' + list_keywords)
description_to_save = "URL = " + url + '\n\n' + '*' * 70 + "\n\nTITLE = " + title + '\n\n' + '*' * 70 + '\n\nDESCRIPTION = ' + \
description + '\n\n' + '*' * 70 + '\n\nKEYWORDS = ' + list_keywords + '\n\n' + '*' * 70 + '\n\nTIMING for ADS = ' + \
"0:10, 0:20, 0:30, 0:40, 0:50, 1:00"
# SAVE Url, Title, Description, Keywords to file TXT
file = open(path_directory + '/' + title + ".txt","w")
file.write(description_to_save)
file.close()
# CREATE SUBTITLES .SRT
# call(["autosub", "-D", lang_choice[:2], video_file_save])
#UPLOAD
#os.system('python filename.py')
# DELETE TEMPORY FILES IMAGES
folder = 'tmp'
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path): shutil.rmtree(file_path)
except Exception as e:
print(e)
if __name__ == "__main__":
# lang_choice = 'vi' en-us
# python make_video vi ALL <link for all> == download all links in URL
# python make_video vi LIST <filename.txt> == download all links in file list_download.txt
# python make_video vi LINK <link>== download ONLY 1 link in argv3
# python make_video.py en-us LIST ENG-Greek_Mythology
if len(sys.argv) > 1:
lang_choice = sys.argv[1]
print('Language >> ' + lang_choice)
arg_type = sys.argv[2]
try:
directory = sys.argv[3]
except:
pass
if arg_type == 'ALL':
import newspaper
paper = newspaper.build(url, memoize_articles=False)
directory = 'ALL'
for article in paper.articles:
print(article.url)
#try:
create_movie(lang_choice, article.url.strip(), directory)
#except:
# continue
elif arg_type == 'LIST':
file = open(directory + '.txt', 'r')
list_download = file.readlines()
file.close()
for i in range(len(list_download)):
print(list_download[i])
#try:
create_movie(lang_choice, list_download[i].strip(), directory)
#except:
# continue
elif arg_type == 'LINK':
url = directory
create_movie(lang_choice, url.strip(), directory)