-
Notifications
You must be signed in to change notification settings - Fork 37
/
mseg.py
280 lines (264 loc) · 9.1 KB
/
mseg.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/python2
import argparse
import sys
import mplayer as mp
import curses
def updatemarks(mwin, marks, remove=False, add=False, edit=False, pgUp=False,
pgDwn=False):
maxlines = mwin.getmaxyx()[0]
# Moving in half-pages is better to track where we were
if pgUp:
updatemarks.count -= maxlines / 2
if updatemarks.count < 1:
updatemarks.count = 1
elif pgDwn:
updatemarks.count += maxlines / 2
if updatemarks.count >= len(marks):
updatemarks.count = len(marks)
mwin.vline(0, 0, 0, maxlines)
mwin.addstr(0, 2, 'Marks:\n')
if remove:
curses.echo()
mwin.addstr(0, 16, 'Mark to remove? ')
m = mwin.getstr()
try:
mwin.addstr(0, 16, 'Removed mark at: ' +
str(marks.pop(int(m) - 1)) + '\n')
except (ValueError, IndexError):
mwin.addstr(0, 16, 'Invalid mark index: ' + m + '\n')
curses.noecho()
if add:
curses.echo()
mwin.addstr(0, 16, 'Add mark at? ')
val = mwin.getstr()
try:
mwin.addstr(0, 16, 'Added mark at: ' + val + '\n')
marks.append(float(val))
except (ValueError, IndexError):
mwin.addstr(0, 16, 'Invalid mark value: ' + val + '\n')
curses.noecho()
if edit:
curses.echo()
mwin.addstr(0, 16, 'Mark to manually edit? ')
m = mwin.getstr()
try:
mwin.addstr(0, 16, 'Insert mark ' + m + ' new value: ')
val = mwin.getstr()
mwin.addstr(0, 16, 'Updated mark ' + m + '\n')
marks[int(m) - 1] = float(val)
except (ValueError, IndexError):
mwin.addstr(0, 16, 'Invalid mark index: ' + m + '\n')
curses.noecho()
marks.sort()
lincount = 1
for m in marks[updatemarks.count - 1:]:
mwin.addstr(mwin.getyx()[0], 2, str(updatemarks.count - 1 + lincount)
+ ': ' + str(m) + '\n')
lincount += 1
if lincount >= maxlines - 1:
break
# Clean lines in case we delete the last ones
for l in xrange(lincount + 1, maxlines):
mwin.addstr(mwin.getyx()[0], 2, '\n')
mwin.refresh()
def writebuf(marks, outf):
outf.write('#' + args.infile + '\n')
for l in marks:
outf.write(str(l) + '\n')
def main(infile, marks, outf):
# Curses initialization
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(False)
# Main screen
maxlines = stdscr.getmaxyx()[0]
scr = stdscr.subwin(maxlines, 32, 0, 0)
scr.nodelay(True)
scr.keypad(True)
# Prepare marks window
mwin = stdscr.subwin(0, 33)
# mwin.scrollok(True)
# mplayer initialization
p = mp.Player()
p.loadfile(infile)
# Start paused
p.pause()
p.time_pos = 0.0
scr.addstr('Total time: ')
scr.addstr(str(p.length))
scr.addstr(' seconds\n')
scr.addstr(6, 0, 'Pause: ' + str(p.paused) + '\n')
scr.addstr(8, 0, 'Default keys:\n')
scr.addstr('Quit: <esc> or q\n')
scr.addstr('Pause: p\n')
scr.addstr('Mark position: <space>\n')
scr.addstr('Manually edit mark: e\n')
scr.addstr('Add manual mark: a\n')
scr.addstr('Remove mark: r\n')
scr.addstr('Faster speed: <Key-Up>\n')
scr.addstr('Slower speed: <Key-Down>\n')
scr.addstr('Rewind: <Key-Left>\n')
scr.addstr('Fast Forward: <Key-Right>\n')
scr.addstr('Scroll down marks: <Key-pgDwn>\n')
scr.addstr('Scroll up marks: <Key-pgUp>\n')
pausedhere = False
moveFirstTime = True
movePos = 0.0
updatemarks.count = 1 # Start showing from the first mark
updatemarks(mwin, marks)
# Main loop
while True:
scr.addstr(1, 0, 'Time: ')
if moveFirstTime:
movePos = p.time_pos
scr.addstr(str(movePos))
scr.addstr(' seconds\n')
scr.refresh()
# updatemarks(mwin, marks, maxlines)
c = scr.getch()
if c == ord('q') or c == 27:
break
elif c == ord(' '): # Space pressed
if not p.paused:
p.pause() # Make sure both screen and file has the same time
pausedhere = True
scr.addstr(2, 0, 'Mark at: ' + str(p.time_pos) + ' seconds\n')
marks.append(p.time_pos)
updatemarks(mwin, marks)
if pausedhere:
pausedhere = False
p.pause()
elif c == ord('e'):
if not p.paused:
p.pause()
pausedhere = True
if len(marks) > 0:
updatemarks(mwin, marks, edit=True)
else:
scr.addstr(5, 0, 'No mark to edit\n')
if pausedhere:
pausedhere = False
p.pause()
elif c == ord('a'):
if not p.paused:
p.pause()
pausedhere = True
updatemarks(mwin, marks, add=True)
if pausedhere:
pausedhere = False
p.pause()
elif c == ord('r'):
if not p.paused:
p.pause()
pausedhere = True
if len(marks) > 0:
updatemarks(mwin, marks, remove=True)
else:
scr.addstr(5, 0, 'No mark to remove\n')
if pausedhere:
pausedhere = False
p.pause()
elif c == ord('p'):
if not moveFirstTime:
moveFirstTime = True
p.time_pos = movePos
p.pause()
scr.addstr(6, 0, 'Pause: ' + str(p.paused) + '\n')
elif c == curses.KEY_UP:
if not p.paused:
p.pause()
pausedhere = True
p.speed_incr(0.05)
scr.addstr(3, 0, 'Speed set at: x' + str(p.speed) + '\n')
if pausedhere:
pausedhere = False
p.pause()
elif c == curses.KEY_DOWN:
if not p.paused:
p.pause()
pausedhere = True
p.speed_incr(-0.05)
scr.addstr(3, 0, 'Speed set at: x' + str(p.speed) + '\n')
if pausedhere:
pausedhere = False
p.pause()
elif c == curses.KEY_LEFT:
if not p.paused:
p.pause()
pausedhere = True
if moveFirstTime:
movePos = p.time_pos
moveFirstTime = False
movePos -= TIME_STEP
scr.addstr(4, 0, 'Back to ' + str(movePos) + ' seconds\n')
if pausedhere:
pausedhere = False
moveFirstTime = True
p.time_pos = movePos
p.pause()
elif c == curses.KEY_RIGHT:
if not p.paused:
p.pause()
pausedhere = True
if moveFirstTime:
movePos = p.time_pos
moveFirstTime = False
movePos += TIME_STEP
scr.addstr(4, 0, 'Forward to ' + str(movePos) + ' seconds\n')
if pausedhere:
pausedhere = False
moveFirstTime = True
p.time_pos = movePos
p.pause()
elif c == curses.KEY_PPAGE:
updatemarks(mwin, marks, pgUp=True)
elif c == curses.KEY_NPAGE:
updatemarks(mwin, marks, pgDwn=True)
# Write the output
if outf is not None:
writebuf(marks, outf)
# Reset terminal to normal operation before quitting
curses.echo()
curses.nocbreak()
curses.curs_set(True)
scr.keypad(False)
stdscr.nodelay(False)
curses.endwin()
sys.exit()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Helps to mark positions in a\
media file.')
parser.add_argument('infile', type=str,
help='Specifies the input file')
parser.add_argument('-o', dest='outfile', type=str, default=sys.stdout,
help='Specifies an output file.')
parser.add_argument('-i', dest='mfile', type=str, default=None,
help='Specifies an existing marks input file.')
parser.add_argument('-ts', dest='timestep', type=float, default=1.0,
help='Specifies the timestep for jumping forwards or\
backwards, default 1.0 seconds.')
args = parser.parse_args()
# Process arguments
print 'Reading file from:', args.infile
marks = []
if args.mfile is not None:
print 'Reading existing marks file from:', args.mfile
with open(args.mfile, 'r') as mfile:
mfile.readline() # Skip filename
for m in mfile:
marks.append(float(m))
if args.outfile != sys.stdout:
outfile = args.outfile
print 'Writing output to:', args.outfile
else:
outfile = sys.stdout
print 'Writing output to: stdout'
TIME_STEP = args.timestep
# End of argument processing
# Do the real work
if outfile != sys.stdout:
with open(outfile, 'w') as outf:
main(args.infile, marks, outf)
else:
main(args.infile, marks, None)