-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsegment.py
138 lines (105 loc) · 3.79 KB
/
segment.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
#!/usr/bin/env python
# ---------------------------------------------------------------------
# OPRA Dataset
# Copyright 2018 University of Southern California, Stanford University
# Licensed under The MIT License [see LICENSE for details]
# Written by Kuan Fang, Te-Lin Wu
# ---------------------------------------------------------------------
"""Segment videos in the OPRA Dataset according to the annotations.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import os
def parse_args():
"""Parse arguments.
Returns:
args: The parsed arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
'--annotations',
dest='annotations',
help='Path of the annotations.',
type=str,
required=True)
parser.add_argument(
'--raw',
dest='raw_video_dir',
help='Directory of raw videos',
type=str,
default='data/playlists')
parser.add_argument(
'--output',
dest='output_dir',
help='The output directory.',
type=str,
default='data/clips')
args = parser.parse_args()
return args
def read_annotations(filename, num_points=10):
"""Read the annotations from file.
Args:
filename: Path to the annotations.
num_points: Number of annotated points.
Returns:
annotations: The dict of annotations.
"""
annotations = []
with open(filename, 'r') as fin:
for line in fin:
items = line.split(' ')
points = []
for i in xrange(num_points):
x = float(items[8 + 2*i])
y = float(items[8 + 2*i + 1])
points.append([x, y])
entry = {
'channel': items[0],
'playlist': items[1],
'video': items[2],
'start_time': items[3],
'duration': items[4],
'image': items[5],
'image_shape': (float(items[6]), float(items[7])),
'points': points,
}
annotations.append(entry)
return annotations
def segment_videos(annotations, raw_video_dir, output_dir):
"""Segment the videos.
Args:
annotations: The dict of the annotations.
raw_video_dir: Directory of raw videos.
Returns:
output_dir: Output directory.
"""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
num_annotations = len(annotations)
for i, entry in enumerate(annotations):
input_path = os.path.join(
raw_video_dir, entry['channel'], entry['playlist'],
'%s.mp4' % (entry['video']))
video_output_dir = os.path.join(output_dir, entry['channel'],
entry['playlist'], entry['video'])
output_path = os.path.join(
video_output_dir,
'%s_%s.mp4' % (entry['start_time'], entry['duration']))
if not os.path.exists(video_output_dir):
os.makedirs(video_output_dir)
command = ('ffmpeg -y -ss {} -i {} -t {} -c copy {}').format(
entry['start_time'], input_path, entry['duration'], output_path)
print('Segmenting video (%d / %d) by calling [%s] ...' %
(i, num_annotations, command))
os.system(command)
def main():
args = parse_args()
if args.output_dir is not None:
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
annotations = read_annotations(args.annotations)
segment_videos(annotations, args.raw_video_dir, args.output_dir)
if __name__ == '__main__':
main()