-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcompress_video.py
executable file
·136 lines (117 loc) · 4.97 KB
/
compress_video.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
"""
Used to compress video in: https://github.com/ArrowLuo/CLIP4Clip
Author: ArrowLuo
"""
import os
import argparse
import ffmpeg
import subprocess
import time
import multiprocessing
from multiprocessing import Pool
import shutil
import json
try:
from psutil import cpu_count
except:
from multiprocessing import cpu_count
# multiprocessing.freeze_support()
def compress(paras):
input_video_path, output_video_path = paras
try:
command = ['ffmpeg',
'-y', # (optional) overwrite output file if it exists
'-i', input_video_path,
'-filter:v',
'scale=\'if(gt(a,1),trunc(oh*a/2)*2,512)\':\'if(gt(a,1),512,trunc(ow*a/2)*2)\'', # scale to 256
'-map', '0:v',
#'-r', '3', # frames per second
output_video_path,
]
ffmpeg = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = ffmpeg.communicate()
retcode = ffmpeg.poll()
# print something above for debug
except Exception as e:
raise e
def prepare_input_output_pairs(input_root, output_root):
input_video_path_list = []
output_video_path_list = []
for root, dirs, files in os.walk(input_root):
for file_name in files:
input_video_path = os.path.join(root, file_name)
output_video_path = os.path.join(output_root, file_name)
output_video_path = os.path.splitext(output_video_path)[0] + ".mp4"
if os.path.exists(output_video_path) and os.path.getsize(output_video_path) > 0:
pass
else:
input_video_path_list.append(input_video_path)
output_video_path_list.append(output_video_path)
return input_video_path_list, output_video_path_list
def msvd():
captions = pickle.load(open('raw-captions.pkl','rb'))
outdir = "/data/datasets/msvd/videos_mp4"
for key in captions:
outpath = os.path.join(outdir, key+".txt")
with open(outpath, 'w') as f:
for line in captions[key]:
f.write(" ".join(line)+"\n")
def webvid():
df = pd.read_csv('/webvid/results_2M_train_1/0.csv')
df['rel_fn'] = df.apply(lambda x: os.path.join(str(x['page_dir']), str(x['videoid'])), axis=1)
df['rel_fn'] = df['rel_fn'] + '.mp4'
# remove nan
df.dropna(subset=['page_dir'], inplace=True)
playlists_to_dl = np.sort(df['page_dir'].unique())
vjson = []
video_dir = '/webvid/webvid/data/videos'
for page_dir in playlists_to_dl:
vid_dir_t = os.path.join(video_dir, page_dir)
pdf = df[df['page_dir'] == page_dir]
if len(pdf) > 0:
for idx, row in pdf.iterrows():
video_fp = os.path.join(vid_dir_t, str(row['videoid']) + '.mp4')
if os.path.isfile(video_fp):
caption = row['name']
video_path = os.path.join(page_dir, str(row['videoid'])+'.mp4')
vjson.append({'caption':caption,'video':video_path})
with open('/webvid/webvid/data/2M.json', 'w') as f:
json.dump(vjson, f)
def webvid20k():
j = json.load(open('/webvid/webvid/data/2M.json'))
idir = '/webvid/webvid/data/videos'
v2c = []
for item in j:
caption = item['caption']
video = item['video']
if os.path.exists(os.path.join(idir, video)):
v2c.append(item)
print("video numbers", len(v2c))
with open('/webvid/webvid/data/40K.json', 'w') as f:
json.dump(v2c, f)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Compress video for speed-up')
parser.add_argument('--input_root', type=str, help='input root')
parser.add_argument('--output_root', type=str, help='output root')
args = parser.parse_args()
input_root = args.input_root
output_root = args.output_root
assert input_root != output_root
if not os.path.exists(output_root):
os.makedirs(output_root, exist_ok=True)
input_video_path_list, output_video_path_list = prepare_input_output_pairs(input_root, output_root)
print("Total video need to process: {}".format(len(input_video_path_list)))
num_works = cpu_count()
print("Begin with {}-core logical processor.".format(num_works))
pool = Pool(num_works)
data_dict_list = pool.map(compress,
[(input_video_path, output_video_path) for
input_video_path, output_video_path in
zip(input_video_path_list, output_video_path_list)])
pool.close()
pool.join()
print("Compress finished, wait for checking files...")
for input_video_path, output_video_path in zip(input_video_path_list, output_video_path_list):
if os.path.exists(input_video_path):
if os.path.exists(output_video_path) is False or os.path.getsize(output_video_path) < 1.:
print("convert fail: {}".format(output_video_path))