-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextra-mp4s.py
More file actions
executable file
·84 lines (69 loc) · 2.09 KB
/
extra-mp4s.py
File metadata and controls
executable file
·84 lines (69 loc) · 2.09 KB
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
#!/usr/bin/env python
from functools import partial
from os import remove
from os.path import basename, splitext
from sys import stderr
import pandas as pd
from click import command, argument, option
from disk_tree import find
from utz import iec
def rm_root(path, root):
if path == root:
return '.'
elif path.startswith(f'{root}/'):
return path[len(root)+1:]
else:
raise ValueError(f"{path} doesn't start with {root}")
err = partial(print, file=stderr)
@command(no_args_is_help=True)
@option('-r', '--remove', 'do_remove', is_flag=True)
@argument('root')
def main(
do_remove: bool,
root: str,
):
d = find.index(root)
d['path'] = d['path'].apply(rm_root, root=root)
d['basename'] = d.path.apply(basename)
d['name'] = d.basename.apply(lambda n: splitext(n)[0])
mp4s = d[d.path.str.endswith('.mp4')]
insv = d[d.path.str.endswith('.insv')]
fs = pd.concat([ mp4s, insv ])
fs = pd.concat([
fs,
fs.basename.str.extract(r'VID_(?P<dt>\d{8}_\d{6})_(?P<id>\d\d)_(?P<idx>\d\d\d)\.(?P<xtn>.*)'),
], axis=1)
def extra_mp4s(df):
mp4s = df[df.basename.str.endswith('.mp4')].path.tolist()
if not mp4s:
return pd.DataFrame()
ins = df[df.basename.str.endswith('.insv')].path.tolist()
if len(ins) == 2:
return pd.DataFrame([
dict(mp4=mp4, ins0=ins[0], ins1=ins[1])
for mp4 in mp4s
])
else:
for mp4 in mp4s:
err(f"{mp4}: {len(ins)} `.insv`s")
return pd.DataFrame()
ms = fs.groupby(['dt', 'idx']).apply(extra_mp4s, include_groups=False).reset_index(drop=True)
total_size = (
ms
.set_index('mp4')
.merge(
d.set_index('path')[['size']],
how='left',
left_index=True,
right_index=True,
)['size']
.sum()
)
err(f'{len(ms)} MP4s, {iec(total_size)}:')
for m in ms['mp4']:
path = f'{root}/{m}'
print(m)
if do_remove:
remove(path)
if __name__ == '__main__':
main()