-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergevideo.py
154 lines (133 loc) · 5.29 KB
/
mergevideo.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
# coding=utf-8
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip
import subprocess,os,time,datetime
def mergeAudio(path,mp4filelist,ouput,timelist):
'''
this function is merge audio.\r\n
path : room path dir,type(str).\r\n
mp4filelist : file need to merge in this path,type(list -> str).\r\n
ouput : output file address,type(str).\r\n
timelist : delay time list,type(dic -> int).\r\n
return : True or False,type(bool).
'''
try:
for i in range(len(mp4filelist)):
try:
globals()['clip'+str(i)] = VideoFileClip(path+mp4filelist[i]).audio
except:
globals()['clip'+str(i)] = AudioFileClip(path+mp4filelist[i])
new_audioclip = CompositeAudioClip([eval('clip'+str(j)).set_start(timelist[mp4filelist[j].split('.')[0]]).volumex(0.5) for j in range(len(mp4filelist))])
new_audioclip.write_audiofile(ouput,44100)
except:
return False
return True
def webm2Mp4(file,result):
'''
this func is turn webm file to mp4 file used ffmpeg packege.\r\n
file : need to turn to mp4 file address,type(str).\r\n
result : output file address,type(str).\r\n
return : True or False,type(bool).
'''
cmd = 'ffmpeg -i {} -c:v copy {}'.format(file,result)
x = subprocess.call(cmd,shell=True)
if x == 0 :
return True
else:
os.remove(result)
cmd = 'ffmpeg -i {} {}'.format(file,result)
x = subprocess.call(cmd,shell=True)
if x == 0 :
return True
return False
def go2Log(log_path, e):
'''
write info to log.\r\n
log_path : log file address,type(str).\r\n
e : error info,type(unknow).
'''
time = datetime.datetime.now()
with open(log_path, 'a', newline='') as f:
f.write('{} :{}\n'.format(time.strftime("%Y-%m-%d %H:%M:%S"), str(e)))
def flipTime(file,date):
'''
check file create time,and mp3 are already in path ,if not filter date ,return False,else return True.\r\n
file : input file address,type(str).\r\n
date : filter date,type(str).\r\n
return : True or False,type(bool).
'''
filetime = time.strftime('%Y-%m-%d', time.localtime(os.stat(file).st_ctime))
# check mp3 already in path or creat room time not yesterday
alreadymerge = [i.split('.')[1] if i.split('.')[1]== 'mp3' else None for i in os.listdir(file)]
if date != filetime or 'mp3' in alreadymerge:
return False
return True
def readTime(path):
'''
read delay time and make dictionary.\r\n
path : need to read delay time path,type(list -> str).\r\n
return : {filename:delaytime},type(str:int)
'''
timelist = {}
inputfilelist = []
filenamelist = os.listdir(path)
for i in filenamelist:
if i.split('.')[1] == 'txt':
with open(path+i,'r',newline=None) as file:
r = file.readlines()
sttime =int(r[0].split('\n')[0])
timelist[i.split('.')[0]] = sttime
else :
inputfilelist.append(i)
minsec = timelist[min(timelist, key=timelist.get)]
for i in timelist:
timelist[i] -= minsec
if timelist[i] == 1:
timelist[i] = 0
return timelist,inputfilelist
def splitListDir(path,splitstring,cindex):
'''
split string in list filter split string.\r\n
path : need to listdir path address,type(str).\r\n
splitstring : filter to split string,type(str).\r\n
cindex : split with splitstring index,type(int).\r\n
return : result with split filter string,type(list).
'''
results = []
pathlist = os.listdir(path)
for i in pathlist:
if i.split('.')[cindex] == splitstring:
results.append(i)
return results
if __name__ == '__main__':
# recordpath = r"D:\\WebSite\\ITTS-EP-FRONTSITE\\wwwroot\\RecordUpload\\"
recordpath = "D:\\webrtc_multiple_conference_Flask_record\\record\\"
record_list = os.listdir(recordpath)
yesterday = str(datetime.date.today()-datetime.timedelta(days=0))
# for each all room
for i in record_list:
try:
# get time delay list and webm file list
timelist,inputfilelist = readTime(recordpath+i+'\\')
# check room if creat time is yesterday
if flipTime(recordpath+i,yesterday) == True:
filelist = []
# for each all webm in this room
for j in inputfilelist:
a = j.split('.')[1]
if a =='webm':
file = recordpath+i+'\\'+ j
ouput = file.split('webm')[0]+'mp4'
# turn webm to mp4
if webm2Mp4(file,ouput) != True :
go2Log("./mergelog.log","[webm2mp4ERR] {} ".format(j))
# get all mp4 file in this dir
mp4filelist = splitListDir(recordpath+i+'\\','mp4',1)
result = recordpath+i+'\\{}.mp3'.format(i)
# merge audio
if mergeAudio(recordpath+i+'\\',mp4filelist,result,timelist) != True :
go2Log("./mergelog.log","[merge failed] {} ".format(i))
else :
go2Log("./mergelog.log","[info] no file need to merge today")
except Exception as e :
print(e)
go2Log("./mergelog.log","merge mp3 Done")