tvaLib
preprocessseq.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # tvaLib Copyright (c) 2012-2016 Paul G. St-Aubin
3 # Ecole Polytechnique de Montreal, McGill University
4 # Python 2.7; (dt) Spyder Windows 10 64-bit; ipython Ubuntu 15.04 64-bit
5 version = 'R2.3.0 u. 2017-03-22'
6 
8 import os, subprocess, platform, argparse, datetime
9 
10 
12 
13 # Command line
14 parser = argparse.ArgumentParser()
15 parser.add_argument('-f', '--fps', type=str, dest='fps', default='11097/500', help='[str/int] FPS at which to encode video.')
16 parser.add_argument('-t', '--starttime', type=str, dest='starttime', default='00:00:00', help='[str] This is the timestamp of the first sequence (NOT the timestamp of the first video file).')
17 parser.add_argument('-o', '--video-start-offset', type=str, dest='video_start_offset', default='00:00:00', help='[str] This is the offset from the beginning of the first video file to the begining of the first sequences.')
18 parser.add_argument('-s', '--sequences', type=int, dest='sequences', default=1, help='[int] If set to true, and running multiple threads, a single child process will be spawned but this child will consume all normally available threads.')
19 parser.add_argument('-i', '--sequence-dur_first', type=str, dest='sequence_duration_first', default='01:00:00', help='[str] Duration of first sequence.')
20 parser.add_argument('-l', '--sequence-duration', type=str, dest='sequence_duration', default='01:00:00', help='[str] Duration of each subsequent sequence.')
21 parser.add_argument('-v', '--force-fourcc', type=str, dest='force_fourcc', default='MP4V', help='[str] FOURCC (codec) settings.')
22 parser.add_argument('-p', '--path', type=str, dest='path', default='*', help='[str] Video path.')
23 parser.add_argument('-x', '--source-ext', type=str, dest='source_ext', default='.mp4', help='[str] Source video file extensions.')
24 parser.add_argument( '--output-ext', type=str, dest='output_ext', default='.avi', help='[str] Source video file extensions.')
25 parser.add_argument('-m', '--path-to-mencoder', type=str, dest='path_to_mencoder', default=r'C:\User\Portable Apps (Windows)\mplayer', help='[str] Path to mencoder (only if executable is not bound to system paths).')
26 commands = parser.parse_args()
27 
28 # Regulation
29 if(commands.force_fourcc): fourcc_cmd = ' -ffourcc '+commands.force_fourcc
30 else: fourcc_cmd = ''
31 if(platform.system() == 'Windows'): path_to_mencoder = os.path.join(commands.path_to_mencoder,'mencoder')
32 else: path_to_mencoder = 'mencoder'
33 
34 
36 if(commands.source_ext != commands.output_ext):
37  for filename in os.listdir(commands.path):
38  if(os.path.isfile(os.path.join(commands.path,filename)) and os.path.splitext(os.path.join(commands.path,filename))[1].lower()==commands.source_ext.lower()):
39  print '"'+path_to_mencoder+'" '+os.path.join(commands.path,filename)+' -ovc copy -nosound -fps '+str(commands.fps)+' -ofps '+str(commands.fps)+' -o '+os.path.join(commands.path,os.path.splitext(os.path.join(commands.path,filename))[0]+'-tmp'+commands.output_ext)
40  subprocess.call('"'+path_to_mencoder+'" '+os.path.join(commands.path,filename)+' -ovc copy -nosound -fps '+str(commands.fps)+' -ofps '+str(commands.fps)+' -o '+os.path.join(commands.path,filename+'-tmp'+commands.output_ext), shell=True)
41 
42 
43 print '"'+path_to_mencoder+'" '+os.path.join(commands.path,'*-tmp'+commands.output_ext)+' -forceidx -ovc copy -nosound -o '+os.path.join(commands.path,'tmp'+commands.output_ext)
44 subprocess.call('"'+path_to_mencoder+'" '+os.path.join(commands.path,'*-tmp'+commands.output_ext)+' -forceidx -ovc copy -nosound -o '+os.path.join(commands.path,'tmp'+commands.output_ext), shell=True)
45 
46 
47 if(commands.source_ext != commands.output_ext):
48  for filename in os.listdir(commands.path):
49  if(os.path.isfile(os.path.join(commands.path,os.path.splitext(os.path.join(commands.path,filename))[0]+'-tmp'+commands.output_ext))):
50  print 'Delete file '+os.path.join(commands.path,os.path.splitext(os.path.join(commands.path,filename))[0]+'-tmp'+commands.output_ext)
51  os.remove(os.path.join(commands.path,os.path.splitext(os.path.join(commands.path,filename))[0]+'-tmp'+commands.output_ext))
52 
53 
55 starttime = datetime.datetime.strptime(commands.starttime, '%H:%M:%S')
56 offset = datetime.datetime.strptime(commands.video_start_offset, '%H:%M:%S')
57 for seq in range(commands.sequences):
58  if(seq==0): endpos=datetime.datetime.strptime(commands.sequence_duration_first, '%H:%M:%S')
59  else: endpos=datetime.datetime.strptime(commands.sequence_duration, '%H:%M:%S')
60  delta = datetime.timedelta(hours=endpos.hour, minutes=endpos.minute, seconds=endpos.second)
61  print '"'+path_to_mencoder+'" -ss '+offset.strftime('%H:%M:%S')+' -endpos '+endpos.strftime('%H:%M:%S')+' -ovc copy -nosound '+os.path.join(commands.path,'tmp'+commands.output_ext)+' -o '+os.path.join(commands.path,starttime.strftime('%H-%M-%S')+commands.output_ext)
62  subprocess.call('"'+path_to_mencoder+'" -ss '+offset.strftime('%H:%M:%S')+' -endpos '+endpos.strftime('%H:%M:%S')+' -ovc copy -nosound '+os.path.join(commands.path,'tmp'+commands.output_ext)+' -o '+os.path.join(commands.path,starttime.strftime('%H-%M-%S')+commands.output_ext), shell=True)
63  offset += delta
64  starttime += delta
65 
66 
68 print 'Delete file '+os.path.join(commands.path,'tmp'+commands.output_ext)
69 os.remove(os.path.join(commands.path,'tmp'+commands.output_ext))