Skip to content

Instantly share code, notes, and snippets.

@Wollw
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wollw/10570541 to your computer and use it in GitHub Desktop.
Save Wollw/10570541 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
"""
SYNOPSIS
video-rotate [-h,--help] [-v,--verbose] [--version]
DESCRIPTION
Rotate a video around the x-axis. Frames are output as PNG files
but can be reconstructed into a video with ffmpeg or another
similar tool.
EXAMPLES
video-rotate input-video-file
EXIT STATUS
TODO: List exit codes
AUTHOR
David E. Shere <david.e.shere@gmail.com>
LICENSE
Copyright (c) 2014 David E. Shere <david.e.shere@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
VERSION
0.1
"""
import sys, os, traceback, optparse
import time
import re
import cv
import cv2
import numpy
def main ():
global options, args
if len(args) != 1:
sys.exit('Usage: %s input-video-file' % sys.argv[0])
if not os.path.exists(args[0]):
sys.exit('ERROR: File %s was not found!' % args[0])
vc_src = cv2.VideoCapture(args[0])
width = int(vc_src.get(cv.CV_CAP_PROP_FRAME_WIDTH))
src_height = int(vc_src.get(cv.CV_CAP_PROP_FRAME_HEIGHT))
dst_height = int(vc_src.get(cv.CV_CAP_PROP_FRAME_COUNT))
frame_dst = numpy.array([], dtype=numpy.uint8)
frame_dst.resize((dst_height, width, 3))
j = 0
rval = True
while rval and j < src_height:
print "Frame %d"%j
for i in range(0, dst_height):
rval, frame_src = vc_src.read()
frame_dst[i] = frame_src[j]
vc_src.set(cv.CV_CAP_PROP_POS_FRAMES, 0)
cv2.imwrite("frame_%0.5d.png"%j, frame_dst)
j = j + 1
if __name__ == '__main__':
try:
start_time = time.time()
parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter(),
usage=globals()['__doc__'], version='0.1')
parser.add_option ('-v', '--verbose', action='store_true', default=False, help='verbose output')
(options, args) = parser.parse_args()
#if len(args) < 1:
# parser.error ('missing argument')
if options.verbose: print time.asctime()
main()
if options.verbose: print time.asctime()
if options.verbose: print 'TOTAL TIME IN MINUTES:',
if options.verbose: print (time.time() - start_time) / 60.0
sys.exit(0)
except KeyboardInterrupt, e: # Ctrl-C
raise e
except SystemExit, e: # sys.exit()
raise e
except Exception, e:
print 'ERROR, UNEXPECTED EXCEPTION'
print str(e)
traceback.print_exc()
os._exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment