forked from FozzTexx/viddin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore-ntsc
executable file
·137 lines (123 loc) · 4.39 KB
/
restore-ntsc
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/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 re
import datetime
import sys
import tempfile
import shutil
from viddin import viddin
MULTIPLIER = 25 / (24000.0 / 1001)
def build_argparser():
parser = argparse.ArgumentParser()
parser.add_argument("file", help="input file")
parser.add_argument("--normalize", action="store_true", help="normalize audio")
parser.add_argument("--volume", help="adjust audio to specified level")
return parser
def subTimecode(tc):
ft = viddin.formatChapter(tc)
ft = ft[:-4] + ":" + ft[-3:] + ","
return ft
def slowVideo(path, tempdir):
cmd = "mediainfo '--Inform=Video;%%FrameRate%%' \"%s\"" % (path)
process = os.popen(cmd)
inrate = process.read().strip()
process.close()
if not len(inrate):
print("Not a constant frame rate")
return None
inrate = float(inrate)
outrate = 24000.0 / 25025 * inrate
vtemp = os.path.join(tempdir, os.path.basename(path))
cmd = "mkvmerge -o \"%s\" --default-duration 0:%0.3ffps -d 0 \"%s\"" % (vtemp, outrate, path)
viddin.runCommand(cmd)
return vtemp
def slowAudio(path, tempdir):
atemp, ext = os.path.splitext(os.path.basename(path))
atemp += ".ac3"
atemp = os.path.join(tempdir, atemp)
cmd = "ffmpeg -y -i \"%s\" -vn -acodec ac3 -ab 448k -filter:a" \
" asetpts='25025/24000*(PTS-STARTPTS)',aresample=48000:min_comp=0.01" \
":comp_duration=1:max_soft_comp=100000000:min_hard_comp=0.3 \"%s\"" % (path, atemp)
viddin.runCommand(cmd)
return atemp
def normalizeAudio(path, volume):
atemp, ext = os.path.splitext(path)
atemp += "-adj" + ext
cmd = "adjust-volume"
if not volume is None:
cmd += " --volume %s" % (volume)
cmd += " \"%s\"" % (path)
viddin.runCommand(cmd)
return atemp
def expandChapters(path):
cmd = "edit-chapters --fix --multiplier %0.3f \"%s\"" % (MULTIPLIER, path)
viddin.runCommand(cmd)
return
def expandSubtitles(path, tempdir, tinfo):
outpath, ext = os.path.splitext(os.path.basename(path))
subs = []
for track in tinfo.subtitles:
subpath = outpath + "_%i" % (track['rv_track_id'])
subpath = os.path.join(tempdir, subpath)
cmd = "mkvextract tracks \"%s\" %i:\"%s\"" % (path, track['rv_track_id'], subpath + ".sub")
viddin.runCommand(cmd)
out_idx = open(subpath + "-exp.idx", "w")
for line in open(subpath + ".idx", "r"):
if line.startswith("timestamp:"):
fields = line.split()
tcstr = fields[1]
tcstr = tcstr[:-5] + "." + tcstr[-4:-1]
tc = viddin.decodeTimecode(tcstr)
tc *= MULTIPLIER
tcstr = subTimecode(tc)
fields[1] = tcstr
line = " ".join(fields) + "\n"
out_idx.write(line)
subs.append(subpath + ".idx")
os.rename(subpath + "-exp.idx", subpath + ".idx")
return subs
def main():
args = build_argparser().parse_args()
path = args.file
tempdir = os.path.dirname(path)
tempdir = tempfile.mkdtemp(dir=tempdir)
vfile = viddin.VideoSpec(path)
tinfo = vfile.getTitleInfo()
vtemp = slowVideo(path, tempdir)
atemp = slowAudio(path, tempdir)
if args.normalize:
atemp = normalizeAudio(atemp, args.volume)
expandChapters(vtemp)
subs = expandSubtitles(vtemp, tempdir, tinfo)
outpath, ext = os.path.splitext(path)
subpath, _ = os.path.splitext(os.path.basename(path))
subpath = os.path.join(tempdir, subpath)
outpath += "-NTSC" + ext
cmd = "mkvmerge -o \"%s\" --no-audio --no-subtitles \"%s\" \"%s\"" % (outpath, vtemp, atemp)
if len(tinfo.subtitles):
for track in tinfo.subtitles:
cmd += " --default-track 0:%i --forced-track 0:%i --language 0:%s \"%s\"" \
% (int(track['default_track']), int(track['forced_track']), track['language'],
subpath + "_%i.idx" % (track['rv_track_id']))
viddin.runCommand(cmd)
shutil.rmtree(tempdir)
return
if __name__ == '__main__':
exit(main() or 0)