forked from happycube/ld-decode
-
Notifications
You must be signed in to change notification settings - Fork 46
/
ld-cut
executable file
·182 lines (154 loc) · 4.17 KB
/
ld-cut
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
from base64 import b64encode
import copy
from datetime import datetime
import getopt
import io
from io import BytesIO
import os
import signal
import sys
import argparse
import json
import traceback
import subprocess
from lddecode.core import *
from lddecode.utils import *
from lddecode.utils_logging import *
parser = argparse.ArgumentParser(
description="Extract a sample area from raw RF laserdisc captures. (Similar to ld-decode, except it outputs samples)"
)
parser.add_argument("infile", metavar="infile", type=str, help="source file")
parser.add_argument(
"outfile",
metavar="outfile",
type=str,
help="destination file (recommended to use .lds suffix)",
)
parser.add_argument(
"-s",
"--start",
metavar="start",
type=float,
default=0,
help="rough jump to frame n of capture (default is 0)",
)
parser.add_argument(
"-l",
"--length",
metavar="length",
type=int,
default=-1,
help="limit length to n frames",
)
parser.add_argument(
"-S",
"--seek",
metavar="seek",
type=int,
default=-1,
help="seek to frame n of capture",
)
parser.add_argument(
"-E", "--end", metavar="end", type=int, default=-1, help="cutting: last frame"
)
parser.add_argument(
"-p", "--pal", dest="pal", action="store_true", help="source is in PAL format"
)
parser.add_argument(
"-n", "--ntsc", dest="ntsc", action="store_true", help="source is in NTSC format"
)
parser.add_argument(
"-C",
"--ldf-compression-level",
dest="ldfcomp",
type=int,
default=11,
help="compression level for .ldf files",
)
parser.add_argument(
"-F",
"--fmpeg-options",
dest="ffmpeg_options",
type=str,
default=None,
help="custom ffmpeg format options"
)
args = parser.parse_args()
# print(args)
filename = args.infile
outname = args.outfile
if outname == '-':
outname = "/dev/stdout"
vid_standard = "PAL" if args.pal else "NTSC"
if args.pal and args.ntsc:
print("ERROR: Can only be PAL or NTSC")
exit(1)
try:
loader = make_loader(filename, None)
except ValueError as e:
print(e)
exit(1)
makelds = True if outname[-3:] == "lds" else False
makeldf = True if outname[-3:] == "ldf" else False
system = "PAL" if args.pal else "NTSC"
# Wrap the LDdecode creation so that the signal handler is not taken by sub-threads,
# allowing SIGINT/control-C's to be handled cleanly
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
logger = init_logging(None)
ldd = LDdecode(filename, None, loader, system=system, doDOD=False, _logger=logger)
signal.signal(signal.SIGINT, original_sigint_handler)
# note that endloc and startloc are in field #'s
if args.seek != -1:
startloc = ldd.seek(args.seek if args.start == 0 else args.start, args.seek)
if startloc is None:
print("ERROR: Seeking failed")
exit(1)
elif startloc > 1:
startloc -= 1
else:
startloc = args.start * 2
if args.end != -1:
endloc = ldd.seek(startloc, args.end)
if endloc is None:
print("ERROR: Seeking failed")
exit(1)
elif args.length != -1:
endloc = startloc + (args.length * 2) + 2
else:
# Set end location to well after any reasonable length so EOF is reached
endloc = startloc + (2 ** 40)
ldd.roughseek(startloc)
startidx = int(ldd.fdoffset)
ldd.roughseek(endloc)
endidx = int(ldd.fdoffset)
if args.ffmpeg_options is not None:
process, fd = ffmpeg_pipe(outname, args.ffmpeg_options)
elif makelds:
process = subprocess.Popen(
["ld-lds-converter", "-o", outname, "-p"], stdin=subprocess.PIPE
)
fd = process.stdin
elif makeldf:
process, fd = ldf_pipe(outname, args.ldfcomp)
else:
fd = open(outname, "wb")
# print(startloc, endloc, startidx, endidx)
for i in range(startidx, endidx + 16384, 16384):
l = endidx - i
if l > 16384:
l = 16384
else:
break
# l = 16384 if (l > 16384) else l
data = ldd.freader(ldd.infile, i, l)
if data is not None and len(data) == l:
dataout = np.array(data, dtype=np.int16)
fd.write(dataout)
else:
break
fd.close()
if makelds:
# allow ld-lds-converter to finish after EOFing it's input
process.wait()
# exit(0)