-
Notifications
You must be signed in to change notification settings - Fork 1
/
split-near
executable file
·123 lines (109 loc) · 3.58 KB
/
split-near
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
#
# Copyright 2016 by Chris Osborn <[email protected]>
#
# This file is part of viddin.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
import argparse
import os
import datetime
from viddin import viddin
def build_argparser():
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="Video to split")
parser.add_argument("offset", help="Split at black scene closest to offset. Use negative number for offset from end.")
return parser
def bestMatch(offset, filename):
video, ext = os.path.splitext(filename)
print("Finding offset")
if offset < 0:
cmd = "vidinf %s | grep ID_LENGTH | sed -e 's/ID_LENGTH=//'" % filename
process = os.popen(cmd)
len = float(process.read())
process.close()
offset = len + offset
best = None
with open(video + ".split") as f:
for line in f:
info = line.split()
begin = float(info[1])
end = float(info[2])
seconds = begin + (end - begin) / 2
diff = abs(seconds - offset)
if not best or diff < best[0]:
best = [diff, seconds, begin, end]
bestsil = None
with open(video + ".sil") as f:
for line in f:
info = line.split()
begin = float(info[1])
end = float(info[2])
seconds = begin + (end - begin) / 2
diff = abs(seconds - best[1])
if ((begin >= best[2] and begin <= best[3]) or (end >= best[2] and end <= best[3])) \
and (not bestsil or diff < bestsil[0]):
bestsil = [diff, seconds, begin, end]
if bestsil:
print("Silence: ", bestsil, best)
begin = bestsil[2]
if begin < best[2]:
begin = best[2]
end = bestsil[3]
if end > best[3]:
end = best[3]
split = begin + (end - begin) / 2
print("Splitting at ", split)
return split
print("Black: ", best)
return best[1]
def splitVideo(pos, filename, before):
print("Splitting")
video, ext = os.path.splitext(filename)
if before:
start = "0:00.00"
end = str(datetime.timedelta(seconds = pos))
len = pos
else:
start = str(datetime.timedelta(seconds = pos))
cmd = "vidinf %s | grep ID_LENGTH | sed -e 's/ID_LENGTH=//'" % filename
process = os.popen(cmd)
len = float(process.read())
process.close()
end = str(datetime.timedelta(seconds = len))
len -= pos
cmd = "mkvmerge --split parts:%s-%s -o %sb%s %s > /dev/null" % (start, end, video, ext, filename)
print(cmd)
os.system(cmd)
if before:
splitname = "%s-pre%s" % (video, ext)
else:
splitname = "%s-post%s" % (video, ext)
if os.path.exists(splitname):
os.remove(splitname)
cmd = "ffmpeg -i %sb%s -t %f -codec copy %s > /dev/null 2>&1" % (video, ext, len, splitname)
os.system(cmd)
os.remove("%sb%s" % (video, ext))
print(splitname)
def main():
args = build_argparser().parse_args()
offset = float(args.offset)
viddin.findBlack(args.filename)
viddin.findSilence(args.filename)
split = bestMatch(offset, args.filename)
if args.offset[0] != '+':
splitVideo(split, args.filename, True)
if offset >= 0:
splitVideo(split, args.filename, False)
return
if __name__ == '__main__':
exit(main() or 0)