-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpre-process.py
More file actions
129 lines (102 loc) · 6.11 KB
/
Copy pathpre-process.py
File metadata and controls
129 lines (102 loc) · 6.11 KB
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
import shutil
from run_pipeline import *
import argparse
parser = argparse.ArgumentParser(description="PreProcess");
parser.add_argument('--out_dir', type=str, default='', help='Output direcotry');
parser.add_argument('--dont_crop_face', action='store_true', help='dont crop face (for fakeavceleb)')
opt = parser.parse_args();
setattr(opt, 'data_dir', os.path.join(opt.out_dir))
setattr(opt, 'fake_dir', os.path.join(opt.data_dir, 'fake'))
setattr(opt, 'real_dir', os.path.join(opt.data_dir, 'real'))
if not os.path.exists(os.path.join(opt.data_dir, 'pycrop')):
os.makedirs(os.path.join(opt.data_dir, 'pycrop'))
if not os.path.exists(os.path.join(opt.data_dir, 'pytmp')):
os.makedirs(os.path.join(opt.data_dir, 'pytmp'))
setattr(opt, 'tmp_dir', os.path.join(opt.data_dir, 'pytmp'))
setattr(opt, 'crop_dir', os.path.join(opt.data_dir, 'pycrop'))
for video in os.listdir(opt.real_dir):
if not os.path.exists(os.path.join(opt.data_dir, 'pycrop', 'real', os.path.basename(video)[0:-4])):
print(video)
run_pipeline(opt.data_dir, os.path.join(opt.real_dir, video), os.path.basename(video)[0:-4], 'real', crop_face=not opt.dont_crop_face)
for video in os.listdir(opt.fake_dir):
if not os.path.exists(os.path.join(opt.data_dir, 'pycrop', 'fake', os.path.basename(video)[0:-4])):
print(video)
run_pipeline(opt.data_dir, os.path.join(opt.fake_dir, video), os.path.basename(video)[0:-4], 'fake', crop_face=not opt.dont_crop_face)
for directory in os.listdir(os.path.join(opt.crop_dir, 'real')):
if os.path.isdir(os.path.join(opt.crop_dir, 'real', directory)):
if not os.path.exists(os.path.join(opt.tmp_dir, 'real', directory)):
if len(os.listdir(os.path.join(opt.crop_dir, 'real', directory))) == 0:
continue
videoName = os.listdir(os.path.join(opt.crop_dir, 'real', directory))[0]
videopath = os.path.join(opt.crop_dir, 'real', directory, videoName)
framedir = os.path.join(opt.crop_dir, 'real', directory, 'frames')
os.makedirs(framedir)
audiodir = os.path.join(opt.crop_dir, 'real', directory)
command = ("ffmpeg -y -i %s -qscale:v 2 -threads 1 -f image2 %s" % (
videopath, os.path.join(framedir, '%06d.jpg')))
output = subprocess.call(command, shell=True, stdout=None)
command = ("ffmpeg -y -i %s -ac 1 -vn -acodec pcm_s16le -ar 48000 %s" % (
videopath, os.path.join(audiodir, 'audio.wav')))
output = subprocess.call(command, shell=True, stdout=None)
os.makedirs(os.path.join(opt.tmp_dir, 'real', directory))
total_frames = len(os.listdir(framedir))
for frameNum in range(0, total_frames, 30):
if (frameNum + 30 > total_frames):
continue
if (frameNum == 0):
start_time = 0
else:
start_time = 30.0 / frameNum
videonum = '%05d' % (frameNum / 30)
os.makedirs(os.path.join(opt.tmp_dir, 'real', directory, videonum))
dst = os.path.join(opt.tmp_dir, 'real', directory, videonum)
for i in range(frameNum + 1, frameNum + 31):
i_str = '%06d' % i
i_jpg = i_str + '.jpg'
shutil.copy(os.path.join(framedir, i_jpg), dst)
output_audio = videonum + '.wav'
audiotmp = os.path.join(opt.tmp_dir, 'real', directory, output_audio)
audiostart = frameNum / 30
audioend = (frameNum + 30) / 30
command = ("ffmpeg -y -i %s -ss %.3f -to %.3f %s" % (
os.path.join(audiodir, 'audio.wav'), audiostart, audioend, audiotmp))
output = subprocess.call(command, shell=True, stdout=None)
for directory in os.listdir(os.path.join(opt.crop_dir, 'fake')):
if os.path.isdir(os.path.join(opt.crop_dir, 'fake', directory)):
if not os.path.exists(os.path.join(opt.tmp_dir, 'fake', directory)):
if len(os.listdir(os.path.join(opt.crop_dir, 'fake', directory))) == 0:
continue
videoName = os.listdir(os.path.join(opt.crop_dir, 'fake', directory))[0]
videopath = os.path.join(opt.crop_dir, 'fake', directory, videoName)
framedir = os.path.join(opt.crop_dir, 'fake', directory, 'frames')
os.makedirs(framedir)
audiodir = os.path.join(opt.crop_dir, 'fake', directory)
command = ("ffmpeg -y -i %s -qscale:v 2 -threads 1 -f image2 %s" % (
videopath, os.path.join(framedir, '%06d.jpg')))
output = subprocess.call(command, shell=True, stdout=None)
command = ("ffmpeg -y -i %s -ac 1 -vn -acodec pcm_s16le -ar 48000 %s" % (
videopath, os.path.join(audiodir, 'audio.wav')))
output = subprocess.call(command, shell=True, stdout=None)
os.makedirs(os.path.join(opt.tmp_dir, 'fake', directory))
total_frames = len(os.listdir(framedir))
for frameNum in range(0, total_frames, 30):
if (frameNum + 30 > total_frames):
continue
if (frameNum == 0):
start_time = 0
else:
start_time = 30.0 / frameNum
videonum = '%05d' % (frameNum / 30)
os.makedirs(os.path.join(opt.tmp_dir, 'fake', directory, videonum))
dst = os.path.join(opt.tmp_dir, 'fake', directory, videonum)
for i in range(frameNum + 1, frameNum + 31):
i_str = '%06d' % i
i_jpg = i_str + '.jpg'
shutil.copy(os.path.join(framedir, i_jpg), dst)
output_audio = videonum + '.wav'
audiotmp = os.path.join(opt.tmp_dir, 'fake', directory, output_audio)
audiostart = frameNum / 30
audioend = (frameNum + 30) / 30
command = ("ffmpeg -y -i %s -ss %.3f -to %.3f %s" % (
os.path.join(audiodir, 'audio.wav'), audiostart, audioend, audiotmp))
output = subprocess.call(command, shell=True, stdout=None)