00001
00002 """
00003 Copyright (c) 2012,
00004 Systems, Robotics and Vision Group
00005 University of the Balearican Islands
00006 All rights reserved.
00007
00008 Redistribution and use in source and binary forms, with or without
00009 modification, are permitted provided that the following conditions are met:
00010 * Redistributions of source code must retain the above copyright
00011 notice, this list of conditions and the following disclaimer.
00012 * Redistributions in binary form must reproduce the above copyright
00013 notice, this list of conditions and the following disclaimer in the
00014 documentation and/or other materials provided with the distribution.
00015 * Neither the name of Systems, Robotics and Vision Group, University of
00016 the Balearican Islands nor the names of its contributors may be used to
00017 endorse or promote products derived from this software without specific
00018 prior written permission.
00019
00020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00024 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00027 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 """
00031
00032
00033 PKG = 'bag_tools'
00034
00035 import roslib; roslib.load_manifest(PKG)
00036
00037 import tempfile
00038 import subprocess
00039 import glob
00040 import shutil
00041
00042 def create_video(tmp_dir, args):
00043 print "Using {} as working directory.".format(tmp_dir)
00044 print "Extracting images..."
00045
00046 cmd = ["rosrun", "bag_tools", "extract_images" , tmp_dir, "jpg", args.topic] + args.inbag
00047 print " {}".format(' '.join(cmd))
00048 subprocess.call(cmd)
00049
00050 print "Renaming..."
00051 images = glob.glob(tmp_dir + '/*.jpg')
00052 images.sort()
00053 i = 1
00054 for image in images:
00055 shutil.move(image, tmp_dir + '/img-' + str(i) + '.jpg')
00056 i = i + 1
00057
00058 print "Creating video..."
00059 cmd = ["ffmpeg", "-f", "image2", "-r", str(args.fps), "-i", tmp_dir + "/img-%d.jpg", "-sameq", args.output]
00060 print " {}".format(' '.join(cmd))
00061 subprocess.call(cmd)
00062
00063
00064 if __name__ == "__main__":
00065 import argparse
00066 parser = argparse.ArgumentParser(
00067 description=
00068 'Creates a video from sensor_msgs/Image messages from a bagfile. '
00069 'This script uses the extract_images binary to extract color images '
00070 'from bagfiles and calls ffmpeg afterwards to combine them together '
00071 'to form a video. Note that ffmpeg must be installed on your system.')
00072 parser.add_argument('topic', help='topic of the images to use')
00073 parser.add_argument('--output', help='name of the output video. Note that the file ending defines the codec to use.', default='video.mp4')
00074 parser.add_argument('--fps', help='frames per second in the output video, as long as codec supports this', type=int, default=20)
00075 parser.add_argument('inbag', help='input bagfile(s)', nargs='+')
00076 args = parser.parse_args()
00077 tmp_dir = tempfile.mkdtemp()
00078 try:
00079 create_video(tmp_dir, args)
00080 except Exception, e:
00081 import traceback
00082 traceback.print_exc()
00083 print "Cleaning up temp files..."
00084 shutil.rmtree(tmp_dir)