-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmass-video-trim.py
executable file
·64 lines (50 loc) · 1.59 KB
/
mass-video-trim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3
import re
import os
import sys
import subprocess
DURATION_REGEX = re.compile(r'Duration: ([0-9]+):([0-9]+):([0-9\.]+),')
SECONDS_TO_TRIM = 10 # time to remove from end
THIS_PROGRAM = os.path.basename(sys.argv[0])
OUTPUT_DIR = 'out'
def recursive_trim(directory):
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
trim(os.path.join(dirpath, filename))
def trim(filename):
if os.path.basename(filename) in (THIS_PROGRAM, OUTPUT_DIR):
return
print(filename, end=' ')
output_file = os.path.join(OUTPUT_DIR, filename)
if os.path.isfile(output_file):
print('skipping')
return
output_dir = os.path.dirname(output_file)
os.makedirs(output_dir, exist_ok=True)
try:
output = subprocess.check_output(['ffprobe', filename], stderr=subprocess.STDOUT, encoding='UTF-8')
except subprocess.CalledProcessError:
print('error')
return
matches = DURATION_REGEX.findall(output)
if not matches:
raise ValueError(f"No duration outputted for '{file}'")
match = matches[0]
hours = int(match[0])
minutes = int(match[1]) + 60 * hours
seconds = float(match[2]) + 60 * minutes
seconds_end = seconds - SECONDS_TO_TRIM
print(f'to {seconds_end}')
subprocess.check_call([
'ffmpeg',
'-i', filename,
'-c', 'copy',
'-to', str(seconds_end),
output_file,
])
if __name__ == '__main__':
if len(sys.argv) > 2:
directory = sys.argv[1]
else:
directory = '.'
recursive_trim(directory)