-
Notifications
You must be signed in to change notification settings - Fork 1
/
restore-ntsc
executable file
·140 lines (126 loc) · 4.45 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
138
139
140
#!/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
import viddin
import subprocess
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%", path]
inrate = subprocess.check_output(cmd).strip()
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", vtemp,
"--default-duration", "0:%0.3ffps" % (outrate),
"-d", "0", path]
viddin.runCommand(cmd, debugFlag=True)
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", path, "-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", 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.extend(["--volume", volume])
cmd.append(path)
viddin.runCommand(cmd)
return atemp
def expandChapters(path):
cmd = ["edit-chapters", "--fix", "--multiplier", "%0.3f" % (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", path, "%i:%s" % (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.Media(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" + ".mkv"
cmd = ["mkvmerge", "-o", outpath, "--no-audio", "--no-subtitles", vtemp, atemp]
if len(tinfo.subtitles):
for track in tinfo.subtitles:
cmd.extend(["--default-track", "0:%i" % (int(track['default_track'])),
"--forced-track", "0:%i" % (int(track['forced_track'])),
"--language", "0:%s" % (track['language']),
subpath + "_%i.idx" % (track['rv_track_id'])])
viddin.runCommand(cmd)
shutil.rmtree(tempdir)
return
if __name__ == '__main__':
exit(main() or 0)