import LSF if len(sys.argv) == 3: (vid,cropsdictfile) = sys.argv[1:3] dur = 0 offset = 0 else: (vid,offset,dur,cropsdictfile) = sys.argv[1:5] cropsdict = eval(open(cropsdictfile).read()) offset = int(offset) dur = int(dur) if dur == 0: dur = vidtools.vid_duration(vid) - offset cmds = [] rerun = True while rerun: for clab,crops in cropsdict.items(): outbase,outext = os.path.splitext(vid) outvid = '%s_%s_%s-%s%s' % (outbase,clab,offset,dur,outext) if os.path.exists(outvid) and ( vidtools.vid_duration(outvid) == dur ): print >> sys.stderr, '%s present and expected size, skip' % outvid else: cropstr = '-vf crop=in_w-%s:in_h-%s:%s:%s' % (crops[0]+crops[2],crops[1]+crops[3],crops[0],crops[1]) cmd = 'ffmpeg -ss %s -t %s -i %s -y %s -b 20000k %s' % (offset,dur,vid,cropstr,outvid) cmds.append(cmd)
startsecs = opts['startframe']/float(opts['fps']) durframes = opts['endframe'] - opts['startframe'] dursecs = durframes/float(opts['fps']) vidname = opts.get( 'vidname', os.path.join(imagedir,'%s-%s_%sfps_%sbit' % \ (opts['startframe'],opts['endframe'],opts['fps'],opts['bitrate']) )) print >> sys.stderr, 'start,end of %s, %s invoked. %s frames @ %s fps; start %0.2f, %0.2f seconds total' % \ (opts['startframe'], opts['endframe'], durframes, opts['fps'], startsecs, dursecs) cmd = 'ffmpeg -r %s -i %s/%%0%dd.%s -ss %0.2f -t %0.2f -b %s %s -y %s.%s' % \ (opts['fps'],imagedir,digits,opts['imageformat'],startsecs,dursecs,opts['bitrate'],res_str,vidname,opts['vidformat']) else: vidname = opts.get( 'vidname', os.path.join(imagedir,'all_%sfps_%sbit' % (opts['fps'],opts['bitrate']) )) cmd = 'ffmpeg -r %s -i %s/%%0%dd.%s -b %s %s -y %s.%s' % \ (opts['fps'],imagedir,digits,opts['imageformat'],opts['bitrate'],res_str,vidname,opts['vidformat']) print >> sys.stderr, 'running: %s' % cmd os.system(cmd) if opts.get('cleanup',None): numframes = len(glob(imagedir+'/*.%s' % opts['imageformat'])) exp_vidlen = numframes / float(opts['fps']) print >> sys.stderr, 'vidname %s.%s from %s frames expect %s sec;' % (vidname,opts['vidformat'],numframes,exp_vidlen) obs_vidlen = vidtools.vid_duration('%s.%s' % (vidname,opts['vidformat'])) if obs_vidlen < exp_vidlen*0.95: print >> sys.stderr, 'observed length (%s) less than expected (%s), skipping cleanup' % (obs_vidlen,exp_vidlen) else: print >> sys.stderr, 'observed length (%s). cleanup...' % (obs_vidlen) os.system('rm -rf %s' % imagedir.rstrip('/'))
vidname = opts.get( 'vidname', os.path.join(imagedir,'%s-%s_%sfps_%sbit' % \ (opts['startframe'],opts['endframe'],opts['fps'],opts['bitrate']) )) print >> sys.stderr, 'start,end of %s, %s invoked. %s frames @ %s fps; start %0.2f, %0.2f seconds total' % \ (opts['startframe'], opts['endframe'], durframes, opts['fps'], startsecs, dursecs) cmd = 'ffmpeg -r %s -i %s/%%0%dd.%s -ss %0.2f -t %0.2f -b %s %s -y %s.%s' % \ (opts['fps'],imagedir,digits,opts['imageformat'],startsecs,dursecs,opts['bitrate'],res_str,vidname,opts['vidformat']) else: vidname = opts.get( 'vidname', os.path.join(imagedir, 'all_%sfps_%sbit' % (opts['fps'], opts['bitrate']))) cmd = 'ffmpeg -r %s -i %s/%%0%dd.%s -b %s %s -y %s.%s' % \ (opts['fps'],imagedir,digits,opts['imageformat'],opts['bitrate'],res_str,vidname,opts['vidformat']) print >> sys.stderr, 'running: %s' % cmd os.system(cmd) if opts.get('cleanup', None): numframes = len(glob(imagedir + '/*.%s' % opts['imageformat'])) exp_vidlen = numframes / float(opts['fps']) print >> sys.stderr, 'vidname %s.%s from %s frames expect %s sec;' % ( vidname, opts['vidformat'], numframes, exp_vidlen) obs_vidlen = vidtools.vid_duration('%s.%s' % (vidname, opts['vidformat'])) if obs_vidlen < exp_vidlen * 0.95: print >> sys.stderr, 'observed length (%s) less than expected (%s), skipping cleanup' % ( obs_vidlen, exp_vidlen) else: print >> sys.stderr, 'observed length (%s). cleanup...' % (obs_vidlen) os.system('rm -rf %s' % imagedir.rstrip('/'))
#!/usr/bin/env python import os from video_analysis import vidtools cmds = [] for d in os.listdir('.'): if os.path.isdir(d): try: t = vidtools.vid_duration(d+'/merge6mbit_720.mp4') print d,t except: print d,'FAIL' cmds.append('cat %s/M2U*.MPG > %s/merge.MPG; ffmpeg -i %s/merge.MPG -r 29.97 -b 6000k -an -y -s 480x320 %s/merge6mbit_320.mp4' % (d,d,d,d)) cmd = ';'.join(cmds) shname = os.path.join(os.getcwd(),'run_mpg_ffmpeg.sh') open(shname,'w').write('#!/usr/bin/env sh\n'+cmd+'\n') os.system('chmod +x %s' % shname) os.system('bsub -q hoekstra -o cat-mpg-trans-log "%s"' % shname) #print cmd
# SUBMIT RUNS import os,sys,re from subprocess import Popen, PIPE from glob import glob seglen = 1800 q = 'unrestricted_serial' for cfg in sorted(glob('*/*-config.dict')): currjobs = Popen('bjobs -w',shell=True,stdout=PIPE).stdout.read() print >> sys.stderr, cfg,'\t', vid = cfg.split('-config')[0]+'.mp4' if vidtools.vid_duration(vid) < 8*60*60: #only analyze videos longer than 8hrs print >> sys.stderr, 'too short; skip' continue donebase = '%s-l%snp60nf300ns4' % (vid[:-4],seglen) if os.path.exists(donebase+'.done'): print >> sys.stderr, 'done' elif donebase in currjobs: print >> sys.stderr, 'running' else: cmd = 'summarize_segment_opencv.py -l %s -s 60 -nf 300 -np 60 -ns 4 -gi 0.03 -oe shapely -ac %s -vs np60nf300ns4shapely %s' % (seglen,cfg,vid) logfile = donebase+'.lsflog' ss = run_safe.safe_script(cmd,donebase,force_write=True) subcmd = 'bsub -q %s -o %s %s' % (q,logfile,ss) ret = os.system(subcmd)
#!/usr/bin/env python import os from video_analysis import vidtools cmds = [] for d in os.listdir('.'): if os.path.isdir(d): try: t = vidtools.vid_duration(d + '/merge6mbit_720.mp4') print d, t except: print d, 'FAIL' cmds.append( 'cat %s/M2U*.MPG > %s/merge.MPG; ffmpeg -i %s/merge.MPG -r 29.97 -b 6000k -an -y -s 480x320 %s/merge6mbit_320.mp4' % (d, d, d, d)) cmd = ';'.join(cmds) shname = os.path.join(os.getcwd(), 'run_mpg_ffmpeg.sh') open(shname, 'w').write('#!/usr/bin/env sh\n' + cmd + '\n') os.system('chmod +x %s' % shname) os.system('bsub -q hoekstra -o cat-mpg-trans-log "%s"' % shname) #print cmd
FORCE_PAR = True if len(sys.argv) == 3: (vid,cropsdictfile) = sys.argv[1:3] dur = 0 offset = 0 else: (vid,offset,dur,cropsdictfile) = sys.argv[1:5] cropsdict = eval(open(cropsdictfile).read()) offset = int(offset) dur = int(dur) if dur == 0: dur = vidtools.vid_duration(vid) - offset to_run_dict = {} for clab,crops in cropsdict.items(): outbase,outext = os.path.splitext(vid) outvid = '%s_%s_%s-%s%s' % (outbase,clab,offset,dur,outext) if os.path.exists(outvid) and not os.path.getsize(outvid) == 0 and ( vidtools.vid_duration(outvid) == dur ): print >> sys.stderr, '%s present and expected size, skip' % outvid else: if FORCE_PAR: h,w = vidtools.extract_keyframe(vid).shape th = h - (crops[1]+crops[3]) tw = w - (crops[0]+crops[2]) pixw = 255 pixh = int((float(th)/tw)*pixw) parstr = '-aspect %s:%s' % (pixw,pixh)
from glob import glob from video_analysis import vidtools from PIL import Image (vid,tdir,offset,dur,fps,cropstr) = sys.argv[1:7] if cropstr == '.': cropstr = '' try: foffset = int(sys.argv[7]) + int(int(offset)*float(fps)) except IndexError: foffset = int(int(offset)*float(fps)) if dur == 0: dur = vidtools.vid_duration(vid) - offset #### # experimental: switch to node scratch #outroot = os.path.join(tdir,str(foffset)) outroot = os.path.join('/scratch/brantp/vid2png/',str(foffset)) # #### try: os.system('rm -rf %s' % outroot) except: print >> sys.stderr, "couldn't remove %s" % outroot try:
import LSF if len(sys.argv) == 3: (vid, cropsdictfile) = sys.argv[1:3] dur = 0 offset = 0 else: (vid, offset, dur, cropsdictfile) = sys.argv[1:5] cropsdict = eval(open(cropsdictfile).read()) offset = int(offset) dur = int(dur) if dur == 0: dur = vidtools.vid_duration(vid) - offset cmds = [] rerun = True while rerun: for clab, crops in cropsdict.items(): outbase, outext = os.path.splitext(vid) outvid = '%s_%s_%s-%s%s' % (outbase, clab, offset, dur, outext) if os.path.exists(outvid) and (vidtools.vid_duration(outvid) == dur): print >> sys.stderr, '%s present and expected size, skip' % outvid else: cropstr = '-vf crop=in_w-%s:in_h-%s:%s:%s' % ( crops[0] + crops[2], crops[1] + crops[3], crops[0], crops[1]) cmd = 'ffmpeg -ss %s -t %s -i %s -y %s -b 20000k %s' % ( offset, dur, vid, cropstr, outvid) cmds.append(cmd)