-
Notifications
You must be signed in to change notification settings - Fork 5
/
MakeVideo.py
82 lines (66 loc) · 2.88 KB
/
MakeVideo.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
import urllib.request
from gtts import gTTS
from mutagen.mp3 import MP3
from moviepy.editor import *
from PIL import Image
def getContextImages(article,c,imgpath):
'''Converts News API Image URL to a Image(.png)'''
urllib.request.urlretrieve(article['urlToImage'],imgpath+str(c)+".png") #URL to Image Conversion
image = Image.open(imgpath+str(c)+".png")
new_image = image.resize((1920, 1080)) #Resizing the Image
new_image.save(imgpath+str(c)+".png")
def generateAudio(article,c,audpath):
'''Converts Text NEWS to audio(mp3)'''
con = str(c+1)+article['title']+article["description"] #Getting Content that needs to be converted to audio
tts = gTTS(con,lang="en") #Converting to audio
tts.save(audpath+str(c)+'.mp3') #Saving the audio
audio = MP3(audpath+str(c)+".mp3")
return int(audio.info.length) #returning duration of audio
def generateVideo(duration,c,imgpath,audpath):
'''generating video using images and audio'''
clip = ImageSequenceClip([imgpath+str(c)+'.png'], durations=[duration])
audioclip = AudioFileClip(audpath+str(c)+".mp3")
videoclip = clip.set_audio(audioclip)
return videoclip
def makeVideo(articles,imgpath,audpath,intro,outro):
'''This Function Generates a Output Video'''
if not os.path.exists(imgpath): #Making Directories to save files
os.makedirs(imgpath)
if not os.path.exists(audpath):
os.makedirs(audpath)
c = 0
description = ''
clips = []
if intro != '': #Adding intro video
clips.append(VideoFileClip(intro))
for article in articles:
try:
if c > 10: #Number of Headlines(Change it according to your need)
break
getContextImages(article,c,imgpath)
duration = generateAudio(article,c,audpath)
clips.append(generateVideo(duration,c,imgpath,audpath))
desc = [['Source : ',article['source']['name']],
['Author : ',article['author']],
['Url : ',article['url']]]
for des in desc:
if des[1] is not None:
description += des[0]+des[1]+'\n'
description += '\n'
c += 1
print("Clip",c,": Successfully Created")
except Exception as e:
print("Skipped one, there is a Problem")
print("The Error Is",e,sep="\n")
if outro != '': #Adding outro video
clips.append(VideoFileClip(outro))
outputVideo = concatenate_videoclips(clips,method='compose')
outputVideo.to_videofile("output.mp4",threads=8, remove_temp=True)
return description
if __name__ == "__main__":
description = makeVideo(articles = '',
imgpath = '',
audpath = '',
intro = '',
outro = ''
)