-
Notifications
You must be signed in to change notification settings - Fork 3
/
wksplit
executable file
·128 lines (100 loc) · 2.4 KB
/
wksplit
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
#!/usr/bin/perl -w
use strict;
use warnings;
use Workout;
use Getopt::Long;
use I18N::Langinfo qw(langinfo CODESET);
my $charset = langinfo(CODESET);
binmode STDIN, ":encoding($charset)";
binmode STDOUT, ":encoding($charset)";
binmode STDERR, ":encoding($charset)";
# - TODO: split workout at specified time
# - TODO: optionally split on recint change
# - auto-split workouts at gaps of defined length,
# - automagically drop too short workouts.
my $debug;
my $fnpat;
my $gap = 2 * 3600;
my $len = 30;
my $itype;
my $otype;
my $needhelp;
my $wanthelp;
if( ! GetOptions(
"debug!" => \$debug,
"fnpat=s" => \$fnpat,
"gap=i" => \$gap,
"help!" => \$wanthelp,
"itype=s" => \$itype,
"len=i" => \$len,
"otype=s" => \$otype,
)){
$needhelp++;
}
if( $wanthelp ){
print <<EOF;
$0 [opt] <input file>
split input file at gaps, filter out short blocks with "noise"
Options:
--debug enable debuging output
--fnpat=<pattern> filename pattern (see man strptime)
--gap=<sec> maximum gap within one workout
--help this help
--itype=<type> input file type
--len=<sec> minimum lenght of consecutive recording
--otype=<type> output file type
EOF
exit 0;
}
my $ifname = shift;
if( ! $ifname ){
print STDERR "missing input file\n";
++$needhelp;
}
if( $needhelp ){
print STDERR "use $0 --help for usage info\n";
exit 1;
}
my $in = Workout::file_read( $ifname, {
debug => $debug,
ftype => $itype,
});
$otype ||= Workout::file_type_name( $ifname );
$otype ||= ($in->filetypes)[0];
$fnpat ||= '%Y-%m-%d_%H-%M-%S.' . $otype;
my $blocks = $in->block_marks;
my @splits;
foreach my $block ( @$blocks ){
if( $block->dur > $len ){
if( ! @splits || ( $block->start - $splits[-1]{end}) > $gap ){
$debug && print STDERR "new split @", $block->start,"\n";
push @splits, {
start => $block->start,
end => $block->end,
};
} else {
$splits[-1]{end} = $block->end;
}
}
}
foreach my $split ( @splits ){
my $out = Workout::file_new( {
ftype => $otype,
recint => $in->recint,
debug => $debug,
} );
my $iter = Workout::filter( 'Timespan', $in, {
start => $split->{start},
end => $split->{end},
debug => $debug,
});
$out->from( $iter );
$out->recint( $out->chunk_first->dur );
my $ofname = DateTime->from_epoch(
epoch => $split->{start},
time_zone => 'local',
)->strftime( $fnpat );
$out->meta_prune;
$out->write( $ofname );
print $ofname, "\n";
}