-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplice
executable file
·120 lines (106 loc) · 3.25 KB
/
splice
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
#!/usr/bin/env python3
# -*- python -*-
#
# Copyright 2019 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 json
import xmltodict
import re
import tempfile
import shutil
import pty
import subprocess
import sys
import viddin
def build_argparser():
parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="+", help="input files")
parser.add_argument("dest", help="output file")
parser.add_argument("--chapters", action="store_true",
help="add chapter markers at splice points")
parser.add_argument("--debug", action="store_true", help="print debug info")
parser.add_argument("--ffmpeg", action="store_true", help="use ffmpeg to concatenate")
return parser
def main():
args = build_argparser().parse_args()
longest = None
chapters = []
for path in args.files:
if not os.path.exists(path):
print("No such file:", path)
exit(1)
videos = [viddin.Media(x) for x in args.files]
for vid in videos:
length = vid.length
if not longest or length > longest[1]:
longest = (vid, length)
if len(chapters):
length += chapters[-1]
chapters.append(length)
dsize = longest[0].resolution
print(dsize)
tmpdir = tempfile.mkdtemp()
print(tmpdir)
inputs = []
for vid in videos:
res = vid.resolution
if res == dsize:
inputs.append(os.path.abspath(vid.path))
else:
tmp, ext = os.path.splitext(os.path.basename(vid.path))
tmp += "_%i" % (args.files.index(vid.path)) + ext
tmp = os.path.join(tmpdir, tmp)
cmd = ["ffmpeg", "-i", vid.path,
"-vf", "scale=%i:%i" % (dsize[0], dsize[1]),
"-c", "copy", "-vcodec", "libx264", "-map", "0", tmp];
print(viddin.listToShell(cmd))
viddin.runCommand(cmd, args.debug)
inputs.append(tmp)
if args.ffmpeg:
mergetxt = os.path.join(tmpdir, "merge.txt")
f = open(mergetxt, "w")
for vid in inputs:
f.write("file '%s'\n" % (vid))
f.close()
cmd = ["ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", mergetxt,
"-c", "copy", args.dest]
else:
cmd = ["mkvmerge", "-o", args.dest]
for vid in inputs:
idx = inputs.index(vid)
if idx:
cmd.append("+")
cmd.append(vid)
print(viddin.listToShell(cmd))
stat = viddin.runCommand(cmd, args.debug)
if stat != 0:
print("Failed to splice")
exit(1)
shutil.rmtree(tmpdir)
if args.chapters:
chapters.sort()
if len(chapters) > 0 and chapters[0] < 2:
chapters[0] = 0
else:
chapters.insert(0, 0)
chapters = chapters[:-1]
vfile = viddin.Media(args.dest)
vfile.addChapters(chapters)
vfile.writeChapters()
return
if __name__ == '__main__':
exit(main() or 0)