forked from sbidoul/edledit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkvedlmerge
executable file
·138 lines (123 loc) · 4.4 KB
/
mkvedlmerge
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
#
# This file is part of edledit.
# Copyright (C) 2010 Stephane Bidoul
#
# edledit 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 3 of the License, or
# (at your option) any later version.
#
# edledit 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with edledit. If not, see <http://www.gnu.org/licenses/>.
__version__ = "1.0"
import sys, os, subprocess
from optparse import OptionParser
import pyedl
#def _td2sec(td):
# return td.days*86400+td.seconds+td.microseconds/1000000.
def _td2hms(td):
hours, remainder = divmod(td.seconds, 3600)
hours = td.days*24 + hours
minutes, seconds = divmod(remainder, 60)
return "%02d:%02d:%02d.%06d" % (hours, minutes, seconds, td.microseconds)
def getSplitTimeCodes(edl):
l = []
for block in edl:
if block.startTime:
#l.append("%fs" % _td2sec(block.startTime))
l.append(_td2hms(block.startTime))
if block.stopTime:
#l.append("%fs" % _td2sec(block.stopTime))
l.append(_td2hms(block.stopTime))
return "timecodes:" + ",".join(l)
def getSplitNumbers(edl):
i = 0
l = []
for block in edl:
if block.startTime:
# include chunk before block, unless block starts at 0
i += 1
l.append((i,True))
# exclude chunk corresponding to block
i += 1
l.append((i,False))
# include chunk after last block (this chunk may not exist
# is last block spans until end of file, but we don't know since
# we don't have the file duration here)
i += 1
l.append((i,True))
return l
usage = "usage: %prog [options] -i INPUT -o OUTPUT"
parser = OptionParser(usage=usage, version="%prog " + __version__)
parser.add_option("-i", "--input", dest="inputFileName",
help="Input movie file name", metavar="INPUT")
parser.add_option("-o", "--output", dest="outputFileName",
help="Output movie file name", metavar="OUTPUT")
parser.add_option("-e", "--edl", dest="edlFileName",
help="EDL file name", metavar="EDL")
parser.add_option("-k", "--keep-temp-files", action="store_true",
help="Keep temporary files", dest="keepTempFiles")
(options, args) = parser.parse_args()
if not options.inputFileName:
parser.error("missing INPUT")
sys.exit(1)
if not options.outputFileName:
parser.error("missing OUTPUT")
sys.exit(1)
if not options.edlFileName:
options.edlFileName = os.path.splitext(options.inputFileName)[0] + ".edl"
if not os.path.exists(options.inputFileName):
parser.error("input file %s does not exist" % options.inputFileName)
sys.exit(1)
if not os.path.exists(options.edlFileName):
parser.error("EDL file %s does not exist" % options.edlFileName)
sys.exit(1)
if os.path.exists(options.outputFileName):
parser.error("output file %s already exists" % options.outputFileName)
sys.exit(1)
# load EDL
edl = pyedl.load(open(options.edlFileName))
edl.validate()
edl.normalize()
# split
if 1:
args = ["mkvmerge", "-o", options.outputFileName,
"--split", getSplitTimeCodes(edl),
options.inputFileName]
print " ".join(args)
r = subprocess.call(args)
if r != 0:
sys.exit(r)
# merge
if 1:
outf = os.path.splitext(options.outputFileName)
args = ["mkvmerge", "-o", options.outputFileName]
first = True
for i, keep in getSplitNumbers(edl):
if keep:
inf = "%s-%03d%s" % (outf[0], i, outf[1])
if os.path.exists(inf):
# inf may not exist if it is last chunk and last cut
# spanned until end of file
if first:
first = False
args.append(inf)
else:
args.append("+" + inf)
print " ".join(args)
r = subprocess.call(args)
if r != 0:
sys.exit(r)
# delete temp files
if not options.keepTempFiles:
outf = os.path.splitext(options.outputFileName)
for i, keep in getSplitNumbers(edl):
inf = "%s-%03d%s" % (outf[0], i, outf[1])
if os.path.exists(inf):
os.remove(inf)