This repository has been archived by the owner on Jul 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrosbag2video.py
executable file
·188 lines (173 loc) · 7.35 KB
/
rosbag2video.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
"""
rosbag2video.py
rosbag to video file conversion tool
by Maximilian Laiacker 2016
"""
import roslib
roslib.load_manifest('rosbag')
import rosbag
import sys, getopt
import os
from sensor_msgs.msg import CompressedImage
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2
import numpy as np
import shlex, subprocess
opt_fps =25.0
opt_out_file=""
opt_fourcc = "XVID"
opt_files = []
opt_display_images = False;
def print_help():
print
print 'rosbag2video.py [--fps 25] [-o outputfile] [-s (show video)] bagfile1 [bagfile2] ...'
print
print 'converts image sequence(s) in ros bag file(s) to video file(s) with fixed frame rate using avconv'
print 'avconv needs to be installed!'
print 'if no output file (-o) is given the filename \'<topic>.mp4\' is used and default output codec is h264'
print 'multiple image topics are supportet only when -o option is _not_ used'
print 'avconv will guess the format according to given extension'
print 'compressed and raw image messages are supportet with mono8 and bgr8/rgb8'
print 'Maximilian Laiacker 2016'
if len(sys.argv) < 2:
print 'Please specify ros bag file(s)'
print 'For example:'
print_help()
exit(1)
else :
try:
opts, opt_files = getopt.getopt(sys.argv[1:],"hsr:o:c:",["fps=","ofile=","codec="])
except getopt.GetoptError:
print_help()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print_help()
sys.exit()
elif opt == '-s':
opt_display_images = True
elif opt in ("-r", "--fps"):
opt_fps = float(arg)
elif opt in ("-o", "--ofile"):
opt_out_file = arg
elif opt in ("-c", "--codec"):
opt_fourcc = arg
else:
print "opz:", opt,'arg:', arg
def filter_image_msgs(topic, datatype, md5sum, msg_def, header):
if(datatype=="sensor_msgs/CompressedImage"):
print topic,' with datatype:', str(datatype)
print "############# USING ######################"
return True;
if(datatype=="theora_image_transport/Packet"):
print topic,' with datatype:', str(datatype)
# print "############# USING ######################"
print '!!! theora not supportet, sorry !!!'
return False;
if(datatype=="sensor_msgs/Image"):
print topic,' with datatype:', str(datatype)
print "############# USING ######################"
return True;
return False;
t_first={};
t_file={};
t_video={}
cv_image = []
np_arr = []
if (opt_fps<=0):
opt_fps = 1
print "using ",opt_fps," FPS"
p_avconv = {}
bridge = CvBridge()
for files in range(0,len(opt_files)):
#First arg is the bag to look at
bagfile = opt_files[files]
#Go through the bag file
bag = rosbag.Bag(bagfile)
for topic, msg, t in bag.read_messages(connection_filter=filter_image_msgs):
print topic, 'at', str(t)#,'msg=', str(msg)
print msg.format
try:
if msg.format.find("jpg")!=-1 :
print "jpeg"
if True:#msg.format.find("8")!=-1 and (msg.format.find("rgb")!=-1 or msg.format.find("jpg")!=-1):
if opt_display_images:
np_arr = np.fromstring(msg.data, np.uint8)
cv_image = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR)
elif msg.format.find("mono8")!=-1 :
if opt_display_images:
np_arr = np.fromstring(msg.data, np.uint8)
cv_image = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR)
else:
print 'unsuportet format:', msg.format
exit(1)
if len(msg.data)>0:
if not topic in t_first :
t_first[topic] = t;
t_video[topic] = 0;
t_file[topic] = 0
t_file[topic] = (t-t_first[topic]).to_sec()
while t_video[topic]<t_file[topic]:
if not topic in p_avconv:
if opt_out_file=="":
out_file = str(topic).replace("/", "")+".mp4"
else:
out_file = opt_out_file
p_avconv[topic] = subprocess.Popen(['avconv','-r',str(opt_fps),'-an','-c','mjpeg','-f','mjpeg','-i','-',out_file],stdin=subprocess.PIPE)
p_avconv[topic].stdin.write(msg.data)
t_video[topic] += 1.0/opt_fps
if opt_display_images:
cv2.imshow(topic, cv_image)
key=cv2.waitKey(1)
if key==1048603:
exit(1);
except AttributeError:
try:
pix_fmt=""
if msg.encoding.find("mono8")!=-1 :
pix_fmt = "gray"
#np_arr = np.fromstring(msg.data, np.uint8)
if opt_display_images:
cv_image = bridge.imgmsg_to_cv2(msg, "bgr8")
elif msg.encoding.find("bgr8")!=-1 :
pix_fmt = "bgr24"
#np_arr = np.fromstring(msg.data, np.uint8)
if opt_display_images:
cv_image = bridge.imgmsg_to_cv2(msg, "bgr8")
elif msg.encoding.find("rgb8")!=-1 :
pix_fmt = "rgb24"
#np_arr = np.fromstring(msg.data, np.uint8)
if opt_display_images:
cv_image = bridge.imgmsg_to_cv2(msg, "bgr8")
else:
print 'unsuportet encoding:', msg.encoding
exit(1)
if len(msg.data)>0:
if not topic in t_first :
t_first[topic] = t;
t_video[topic] = 0;
t_file[topic] = 0
t_file[topic] = (t-t_first[topic]).to_sec()
while t_video[topic]<t_file[topic]:
if not topic in p_avconv:
if opt_out_file=="":
out_file = str(topic).replace("/", "")+".mp4"
else:
out_file = opt_out_file
size = str(msg.width)+"x"+str(msg.height)
p_avconv[topic] = subprocess.Popen(['avconv','-r',str(opt_fps),'-an','-f','rawvideo','-s',size,'-pix_fmt', pix_fmt,'-i','-',out_file],stdin=subprocess.PIPE)
p_avconv[topic].stdin.write(msg.data)
t_video[topic] += 1.0/opt_fps
if opt_display_images:
cv2.imshow(topic, cv_image)
key=cv2.waitKey(1)
if key==1048603:
exit(1);
except AttributeError:
# maybe theora packet
# theora not supportet
pass
bag.close();