- 利用一个图片库,每次从中抽取一定数量的图片,裁剪成同一尺寸后,利用moviepy这个库的ImageSequenceClip批量生成简易视频,可加入音频。
- 每次运行程序都会将用于储存的文件夹刷新清空
- 加入了背景音乐,还有水印图片的镶嵌,因为利用moviepy的直接图片嵌入失败,也没有找到合适的解决方案,所以利用PIL先将图片拼接到每张图片上,在用moviepy合成视频,最后加上背景音乐。
- 更新!增加了多线程机制,提高生成速度
import moviepy.editor
from PIL import Image,ImageFile
import os.path , random, shutil
import glob
from pydub import AudioSegment
ImageFile.LOAD_TRUNCATED_IMAGES = True
fileDir = "图库"
cut_Dir = "image"
tarDir =cut_Dir+"/"
ima_url = "image/images"
v_url = "vedio"
vedio_url = v_url+"/new"
logo_url = "logo/二维码图.png"
music_url = "music"
photo_num = 25
vedio_num = 1
def moveFile(fileDir):
pathDir = os.listdir(fileDir)
picknumber=int(photo_num)
sample = random.sample(pathDir, picknumber)
Dir = fileDir + '/'
for name in sample:
fileDir = Dir + name
shutil.copy(fileDir, tarDir+name)
shutil.copy(logo_url,tarDir+"logo.jpg")
return
def musicFile(fileDir):
pathDir = os.listdir(fileDir)
sample = random.sample(pathDir, 1)
print(fileDir+'/'+sample[0])
return fileDir+'/'+sample[0]
def convertjpg(jpgfile,outdir,width=1280,height=720):
img=Image.open(jpgfile)
img=img.convert('RGB')
try:
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
except Exception as e:
print(e)
try:
os.mkdir("vedio")
except:
shutil.rmtree("vedio")
os.mkdir("vedio")
for i in range(vedio_num):
try:
os.mkdir(tarDir)
except:
shutil.rmtree(tarDir)
os.mkdir(tarDir)
os.mkdir(ima_url)
moveFile(fileDir)
rurl = cut_Dir +r"/*.jpg"
for jpgfile in glob.glob(rurl):
convertjpg(jpgfile,ima_url)
try:
image_clip = ImageSequenceClip(ima_url, fps=0.5)
except:
print("图片有错误,放弃制作")
continue
vurl = vedio_url+"%d" %(i)
vurl = vurl+".mp4"
audio_background = AudioFileClip(musicFile(music_url))
image_clip = image_clip.set_audio(audio_background)
image_clip.write_videofile(vurl)
改进了程序,增加了多线程运行,大幅提高速度
from moviepy.editor import *
import moviepy.editor
from PIL import Image,ImageFile
import os.path , random, shutil
import glob
from pydub import AudioSegment
import time
from multiprocessing.pool import ThreadPool
import psutil
ImageFile.LOAD_TRUNCATED_IMAGES = True
fileDir = "图库"
cut_Dir = "image"
tarDir =cut_Dir+"/"
ima_url = "image/images"
v_url = "vedio"
vedio_url = v_url+"/new"
logo_url = "logo/二维码图.png"
music_url = "music"
photo_num = 25
vedio_num = 10
def moveFile(fileDir,writeDir):
pathDir = os.listdir(fileDir)
picknumber=int(photo_num)
sample = random.sample(pathDir, picknumber)
Dir = fileDir + '/'
for name in sample:
fileDir = Dir + name
shutil.copy(fileDir, writeDir+name)
shutil.copy(logo_url,writeDir+"logo.jpg")
return
def musicFile(fileDir):
pathDir = os.listdir(fileDir)
sample = random.sample(pathDir, 1)
print(fileDir+'/'+sample[0])
return fileDir+'/'+sample[0]
def convertjpg(jpgfile,outdir,width=1280,height=720):
img=Image.open(jpgfile)
img=img.convert('RGB')
try:
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
except Exception as e:
print(e)
def make(num):
tarDir = cut_Dir+str(num)+"/"
ima_url = tarDir+"images"
try:
os.mkdir(tarDir)
os.mkdir(ima_url)
except:
shutil.rmtree(tarDir)
os.mkdir(tarDir)
os.mkdir(ima_url)
moveFile(fileDir,tarDir)
rurl = cut_Dir +"/*.jpg"
for jpgfile in glob.glob(rurl):
convertjpg(jpgfile,ima_url)
image_clip = ImageSequenceClip(ima_url, fps=0.5)
vurl = vedio_url+"%d" %(num)
vurl = vurl+".mp4"
audio_background = AudioFileClip(musicFile(music_url))
image_clip = image_clip.set_audio(audio_background)
image_clip.write_videofile(vurl)
shutil.rmtree(tarDir)
def main():
starttime =time.time()
try:
os.mkdir("vedio")
except:
shutil.rmtree("vedio")
os.mkdir("vedio")
pool = ThreadPool(psutil.cpu_count())
returnValues = pool.map(make, range(vedio_num))
endtime=time.time()
print(endtime-starttime)
main()