-
Notifications
You must be signed in to change notification settings - Fork 32
/
img2video.py
118 lines (105 loc) · 4.53 KB
/
img2video.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
import cv2
import re
import numpy as np
import random
from PIL import Image, ImageDraw, ImageFont
from moviepy.video import fx
import os
import datetime
from utils.img_utils import *
from utils.video_utils import *
from utils.file_utils import *
from utils.info_utils import *
from emotion.emotion_analsys import *
from utils.effect_utils import *
def random_select_file(path='./source/',typ='mp3'):
file_list=[]
for home, dirs, files in os.walk(path):
for filename in files:
if filename.endswith(typ):
print(filename)
fullname = os.path.join(home, filename)
file_list.append(fullname)
print(random.choice(file_list))
return random.choice(file_list)
def find_new_file(path='./source/'):
'''查找目录下最新的文件'''
file_lists = os.listdir(path)
file_lists.sort(key=lambda fn: os.path.getmtime(path + "\\" + fn) if not os.path.isdir(path + "\\" + fn) else 0)
print('最新文件: ' + file_lists[-1])
file = os.path.join(path, file_lists[-1])
return file
def video_with_audio(audio,video_with_text_clip):
"""
视频合成音频,并删除临时文件
:return:
"""
# 设置视频音频,并写入到文件中去
#now = datetime.datetime.now().strftime('%Y%m%d%H%M')
video_with_text_clip.set_audio(audio).write_videofile("output.mp4",
codec='libx264',
audio_codec='aac',
temp_audiofile='temp-audio.m4a',
remove_temp=True
)
#删除所有的临时文件
del_temp_file("./source/")
def addword2colorimg(words='我要发表心灵,而不公开隐私',W=1280,H=720):
# Load image, define rectangle bounds
image = cv2.imread(random_select_file('../image_source/','jpg'))
pattern = r',|\.|/|;|\'|`|\[|\]|<|>|\?|:|"|\{|\}|\~|!|@|#|\$|%|\^|&|\(|\)|=|\_|\+|,|。|、|;|‘|’|【|】|·|!| |…|(|)'
dy=30
y=150
#image = np.zeros([H,W], dtype=np.uint8)
H=image.shape[0]
W=image.shape[1]
pil_img = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
txt_list = re.split(pattern, words)
pilimg = Image.fromarray(pil_img)
draw = ImageDraw.Draw(pilimg)
font = ImageFont.truetype("simhei.ttf", int(W/30), encoding="utf-8")
for i,txt in enumerate(txt_list):
text_width = font.getsize(txt)
y = y+text_width[1]*1.5
(x,y) = (int((W-text_width[0])/2), y)
draw.text((x,y), txt, (255,255,255), font=font)
#cv2img = cv2.cvtColor(np.array(pilimg),cv2.COLOR_RGB2BGR)
now = datetime.datetime.now().strftime('%Y%m%d%H%M')
pilimg.save('./source/temp.jpg')
image_video_clip=one_pic_to_video('./source/temp.jpg', './source/temp_img_video.mp4', 25, 60)
new_audio_name=random_select_audio('../music_source/')
audio= AudioFileClip(new_audio_name).subclip(4, image_video_clip.duration +4)
video_with_audio(audio,image_video_clip)
special_effects("output.mp4",now+"_output.mp4")
def addword2binimg(words='我要发表心灵,而不公开隐私',W=1280,H=720):
# Load image, define rectangle bounds
#image = cv2.imread('1.jpg')
pattern = r',|\.|/|;|\'|`|\[|\]|<|>|\?|:|"|\{|\}|\~|!|@|#|\$|%|\^|&|\(|\)|=|\_|\+|,|。|、|;|‘|’|【|】|·|!| |…|(|)'
dy=30
y=150
image = np.zeros([H,W], dtype=np.uint8)
txt_list = re.split(pattern, words)
pilimg = Image.fromarray(image)
draw = ImageDraw.Draw(pilimg)
font = ImageFont.truetype("simhei.ttf", 45, encoding="utf-8")
for i,txt in enumerate(txt_list):
text_width = font.getsize(txt)
y = y+text_width[1]*2
(x,y) = (int((W-text_width[0])/2), y)
draw.text((x,y), txt, 255, font=font)
now = datetime.datetime.now().strftime('%Y%m%d%H%M')
pilimg.save('./source/temp.jpg')
image_video_clip=one_pic_to_video('./source/temp.jpg', './source/temp_img_video.mp4', 25, 60)
new_audio_name=random_select_file('../music_source/')
audio= AudioFileClip(new_audio_name).subclip(4, image_video_clip.duration - 0)
video_with_audio(audio,image_video_clip)
special_effects("output.mp4",now+"_output.mp4")
if __name__ == '__main__':
#文案api
text_info = get_info()
#古诗词api
text_info=get_poetry()
#情绪api
emotion=analsys(text_info)
#addword2binimg(text_info)
addword2colorimg(text_info)