-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_to_vedio.py
71 lines (56 loc) · 2.31 KB
/
data_to_vedio.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
# -*- coding: utf-8 -*-
"""
作者:张贵发
日期:2023年06月12日
描述:将生成的语音、图像合成视频
"""
import os
from moviepy.editor import ImageSequenceClip, AudioFileClip, concatenate_videoclips
import numpy as np
import random
from moviepy.video.io.VideoFileClip import VideoFileClip
speed =10
size =700
def fl(gf, t):
image = gf(t)
start_index = int(speed * t)
end_index = int(speed * t) + size
if end_index >768 :
end_index =768
start_index = 768 -700
cropped_image = image[start_index:end_index, :]
return cropped_image
def merge_vedio(image_dir_path,audio_dir_path):
# image_files = os.listdir(image_dir_path)
audio_files = os.listdir(audio_dir_path)
# clips = []
new_path = image_dir_path.replace("data_image", "data_vedio")
for i in range(len(audio_files)):
image_path = os.path.join(image_dir_path, str(i)+".png")
audio_path = os.path.join(audio_dir_path, str(i)+".wav")
audio_clip = AudioFileClip(audio_path)
img_clip = ImageSequenceClip([image_path], audio_clip.duration)
img_clip = img_clip.set_position(('center', 'center')).fl(fl,apply_to=['mask']).set_duration(audio_clip.duration)
clip = img_clip.set_audio(audio_clip)
clip.write_videofile(new_path + "/" + str(i) + ".mp4", fps=24, audio_codec="aac")
# clips.append(clip)
# final_clip = concatenate_videoclips(clips)
new_parent = image_dir_path.replace("data_image","data_vedio").split("story")[0]
if not os.path.exists(new_parent):
os.makedirs(new_parent)
items = os.listdir(new_parent)
lips = []
for i in range(len(items)):
clip = VideoFileClip(new_parent + "/" + str(i) + ".mp4")
lips.append(clip)
final_clip = concatenate_videoclips(lips)
final_clip.write_videofile(new_parent+".mp4", fps=24, audio_codec="aac")
for item in lips:
item.close()
for i in range(len(items)):
os.remove(new_parent + "/" + str(i) + ".mp4")
list_new_parents = ""
# new_path = image_dir_path.replace("data_image","data_vedio")
# final_clip.write_videofile(new_path+".mp4", fps=24,audio_codec="aac")
if __name__ == '__main__':
merge_vedio("data/data_image/少年歌行第一章/少年歌行第一章","data/data_audio/少年歌行第一章/少年歌行第一章")