annotate python/ffslice.py @ 924:fc88a12acf48 default tip

add lfs filter
author Jeff Hammel <k0scist@gmail.com>
date Wed, 04 Dec 2024 15:50:53 -0800
parents f011ec45b8e8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
741
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 slice a clip with `ffmpeg`:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 ffmpeg -ss <00:00:00> -t <sec> -i <input_file> -q 1 <output_file> #
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 --ss = start time ;
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 -t = length of video to create from the start time in seconds.
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 """
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 # imports
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 import argparse
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 import os
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 import subprocess
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 import sys
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 # module globals
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 __all__ = ['main', 'Parser']
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 here = os.path.dirname(os.path.realpath(__file__))
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 string = (str, unicode)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 def ensure_dir(directory):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 """ensure a directory exists"""
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 if os.path.exists(directory):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 assert os.path.isdir(directory)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 return directory
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 os.makedirs(directory)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 return directory
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 def convert_seconds_to_hhmmss(time_sec):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 """converts `time_sec` to (hh,mm,ss)"""
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 hh = int(time_sec / 3600)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 mm = int((time_sec % 3600) / 60)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 ss = int((time_sec % 3600) % 60)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 return (hh,mm,ss)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 def format_seconds_to_hhmmss(time_sec, separator=':'):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 """converts `time_sec` to string 'hh:mm:ss'"""
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 return separator.join(['{:0>2d}'.format(i)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 for i in convert_seconds_to_hhmmss(time_sec)])
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 def duration(clip, ffprobe='ffprobe'):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 """get the duration in seconds using `ffprobe` from ffmpeg"""
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 command = [ffprobe, clip]
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 assert os.path.exists(clip), "Missing clip, duration not available"
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 # probe the file
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 try:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 output = subprocess.check_output(command, stderr=subprocess.STDOUT)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 # (for some reason, ffprobe sends output to stderr)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 except subprocess.CalledProcessError as e:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 print (e.output)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 raise
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 duration = None
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 for line in output.splitlines():
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66 # Parse the output of `ffprobe`
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 # look for a line like:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 # Duration: 00:00:59.73, start: 0.000000, bitrate: 7783 kb/s
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 line = line.strip()
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 if line.startswith("Duration:"):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 if duration:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 raise AssertionError("Duplicate duration - already found: {}".format(duration))
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 line = line.split(',')[0]
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 duration = line.split(':', 1)[1].strip()
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 if duration:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 hh, mm, ss = [float(i) for i in duration.split(':')]
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 duration = 3600*hh + 60*mm + ss
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81 return duration
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 class FFSliceParser(argparse.ArgumentParser):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85 """fflice CLI option parser"""
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87 default_slice_time = 300.
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
89 def __init__(self):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
90 argparse.ArgumentParser.__init__(self, description=__doc__)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
91 self.add_argument('clips', metavar='clip', nargs='+', help="clips to slice")
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
92 self.add_argument('-d', '--directory', dest='directory',
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
93 default=os.getcwd(),
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
94 help="output directory [DEFAULT: %(default)s]")
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
95 self.add_argument('--durations', '--print-durations', dest='print_durations',
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
96 action='store_true', default=False,
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
97 help="print durations and exit")
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
98 self.add_argument('-n', dest='number', type=int,
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
99 help="number of slices")
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
100 self.add_argument('-t', '--time', dest='slice_time',
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
101 type=float,
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
102 help="time of each slice [DEFAULT: {}]".format(self.default_slice_time))
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
103 self.add_argument('--dry-run', dest='dry_run',
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
104 action='store_true', default=False,
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
105 help="print out what will be done")
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
106 self.add_argument('--hhmmss', dest='hhmmss',
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
107 action='store_true', default=False,
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
108 help="display times in 'hh:mm:ss' format; thedefault is in seconds")
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
109
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
110 self.add_argument('-v', '--verbose', dest='verbose',
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
111 action='store_true', default=False,
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
112 help="be verbose")
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
113 self.options = None
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
114
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
115 def parse_args(self, *args, **kw):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
116 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
117 self.validate(options)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
118 self.options = options
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
119 return options
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
120
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
121 def validate(self, options):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
122 """validate options"""
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
123
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
124 missing = [clip for clip in options.clips
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
125 if not os.path.exists(clip)]
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
126 if missing:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
127 self.error("Not found: {}".format(', '.join(missing)))
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
128
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
129 if options.slice_time and options.number:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
130 self.error("Cannot specify slice time and number of slices")
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
131 # TODO: allow specification of both
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
132 elif options.slice_time is None and options.number is None:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
133 options.slice_time = self.default_slice_time
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
134
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
135 ensure_dir(options.directory)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
136
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
137 def format_seconds(self, seconds):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
138 """format seconds to string"""
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
139 if self.options.hhmmss:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
140 return format_seconds_to_hhmmss(seconds)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
141 return '{:.2}'.format(seconds)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
142
754
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
143 def slice(self):
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
144 """
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
145 iterates over all the specified clips and slices them as per input params.
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
146 The sliced clips are stored under the provided coommand line destinati
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
147 on directory or current working dir
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
148 """
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
149
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
150 for clip_path in self.options.clip_paths:
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
151 print "***** Processing {0}".format(clip_path)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
152
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
153 if not os.path.exists(clip_path):
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
154 print "File not found! skipping {0}".format(clip_path)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
155 continue
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
156
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
157 #making sure the slice time is within bounds of the clip duration
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
158 duration = self.duration(clip_path)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
159 print "Duration: {0}".format(duration)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
160 if self.options.slice_length_sec > duration:
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
161 print "Skipping {0}, slice_time {1} is GREATER than file duration {2} ".format(clip_path,self.options.slice_length_sec ,duration)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
162 continue
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
163
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
164 #calculating the number slices to create
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
165 num_slices = 0
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
166 if self.options.num_slices:
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
167 num_slices = min (self.options.num_slices, int(duration/(self.options.slice_length_sec)))
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
168 else: #number of slice were not specified
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
169 num_slices = int(duration/(self.options.slice_length_sec))
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
170 print "Slicing in {0} parts, {1} seconds each".format(num_slices,self.options.slice_length_sec)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
171
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
172 hh = 0
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
173 mm = 0
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
174 ss = 0
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
175 start_time_secs = 0
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
176 [out_filename,out_file_ext] = clip_path.split("/")[-1].split(".") #assuming the file path to be something like /df/dsf/dsf/dsf.mp4
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
177 ensure_dir(self.options.out_directory)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
178 #creating slices
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
179 for i in range(num_slices):
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
180 [hh,mm,ss] = self.format_seconds_to_hhmmss(start_time_secs)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
181 out_file_path = "{0}/{1}_{2}.{3}".format(self.options.out_directory,out_filename,i,out_file_ext)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
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,
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
183 clip_path,1, out_file_path)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
184
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
185 try:
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
186 output = subprocess.call(command, shell=True)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
187 except subprocess.CalledProcessError as e:
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
188 print (e.output)
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
189 raise
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
190 start_time_secs += self.options.slice_length_sec
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
191
741
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
192
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
193 def main(args=sys.argv[1:]):
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
194 """CLI"""
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
195
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
196 # parse command line options
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
197 parser = FFSliceParser()
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
198 options = parser.parse_args(args)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
199
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
200 # compute durations
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
201 durations = {clip: duration(clip) for clip in options.clips}
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
202 if options.print_durations:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
203 returncode = 0
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
204 total = 0.
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
205 for clip in options.clips:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
206 _duration = durations[clip]
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
207 if _duration is None:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
208 print ("Duration not found: '{}'".format(clip))
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
209 returncode = 1
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
210 else:
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
211 print ('{} : {}'.format(clip,
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
212 parser.format_seconds(_duration)))
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
213 sys.exit(returncode)
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
214
754
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
215 parser.slice()
f011ec45b8e8 add example load type interface
Jeff Hammel <k0scist@gmail.com>
parents: 741
diff changeset
216
741
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
217 if __name__ == '__main__':
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
218 main()
9681a0bd74d6 add utility for slicing video files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
219