Mercurial > hg > config
comparison python/ffslice.py @ 754:f011ec45b8e8
add example load type interface
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Fri, 03 Jul 2015 21:07:03 -0700 |
| parents | 9681a0bd74d6 |
| children |
comparison
equal
deleted
inserted
replaced
| 753:05fef8e5b8a9 | 754:f011ec45b8e8 |
|---|---|
| 137 def format_seconds(self, seconds): | 137 def format_seconds(self, seconds): |
| 138 """format seconds to string""" | 138 """format seconds to string""" |
| 139 if self.options.hhmmss: | 139 if self.options.hhmmss: |
| 140 return format_seconds_to_hhmmss(seconds) | 140 return format_seconds_to_hhmmss(seconds) |
| 141 return '{:.2}'.format(seconds) | 141 return '{:.2}'.format(seconds) |
| 142 | |
| 143 def slice(self): | |
| 144 """ | |
| 145 iterates over all the specified clips and slices them as per input params. | |
| 146 The sliced clips are stored under the provided coommand line destinati | |
| 147 on directory or current working dir | |
| 148 """ | |
| 149 | |
| 150 for clip_path in self.options.clip_paths: | |
| 151 print "***** Processing {0}".format(clip_path) | |
| 152 | |
| 153 if not os.path.exists(clip_path): | |
| 154 print "File not found! skipping {0}".format(clip_path) | |
| 155 continue | |
| 156 | |
| 157 #making sure the slice time is within bounds of the clip duration | |
| 158 duration = self.duration(clip_path) | |
| 159 print "Duration: {0}".format(duration) | |
| 160 if self.options.slice_length_sec > duration: | |
| 161 print "Skipping {0}, slice_time {1} is GREATER than file duration {2} ".format(clip_path,self.options.slice_length_sec ,duration) | |
| 162 continue | |
| 163 | |
| 164 #calculating the number slices to create | |
| 165 num_slices = 0 | |
| 166 if self.options.num_slices: | |
| 167 num_slices = min (self.options.num_slices, int(duration/(self.options.slice_length_sec))) | |
| 168 else: #number of slice were not specified | |
| 169 num_slices = int(duration/(self.options.slice_length_sec)) | |
| 170 print "Slicing in {0} parts, {1} seconds each".format(num_slices,self.options.slice_length_sec) | |
| 171 | |
| 172 hh = 0 | |
| 173 mm = 0 | |
| 174 ss = 0 | |
| 175 start_time_secs = 0 | |
| 176 [out_filename,out_file_ext] = clip_path.split("/")[-1].split(".") #assuming the file path to be something like /df/dsf/dsf/dsf.mp4 | |
| 177 ensure_dir(self.options.out_directory) | |
| 178 #creating slices | |
| 179 for i in range(num_slices): | |
| 180 [hh,mm,ss] = self.format_seconds_to_hhmmss(start_time_secs) | |
| 181 out_file_path = "{0}/{1}_{2}.{3}".format(self.options.out_directory,out_filename,i,out_file_ext) | |
| 182 command = "ffmpeg -ss {0}:{1}:{2} -t {3} -i {4} -q {5} -strict -2 {6}".format(hh,mm,ss,self.options.slice_length_sec, | |
| 183 clip_path,1, out_file_path) | |
| 184 | |
| 185 try: | |
| 186 output = subprocess.call(command, shell=True) | |
| 187 except subprocess.CalledProcessError as e: | |
| 188 print (e.output) | |
| 189 raise | |
| 190 start_time_secs += self.options.slice_length_sec | |
| 142 | 191 |
| 143 | 192 |
| 144 def main(args=sys.argv[1:]): | 193 def main(args=sys.argv[1:]): |
| 145 """CLI""" | 194 """CLI""" |
| 146 | 195 |
| 161 else: | 210 else: |
| 162 print ('{} : {}'.format(clip, | 211 print ('{} : {}'.format(clip, |
| 163 parser.format_seconds(_duration))) | 212 parser.format_seconds(_duration))) |
| 164 sys.exit(returncode) | 213 sys.exit(returncode) |
| 165 | 214 |
| 215 parser.slice() | |
| 216 | |
| 166 if __name__ == '__main__': | 217 if __name__ == '__main__': |
| 167 main() | 218 main() |
| 168 | 219 |
