-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapall.pl
executable file
·192 lines (179 loc) · 6.88 KB
/
capall.pl
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
#!/usr/bin/perl -w
#
# This is a demo how to capture EPG data from multiple channels: The
# script will tune all the channels given in the list. For each channel
# it will capture teletext pages in the given ranges for 90 seconds, then
# try scraping TV schedules from them and write the result to a file in
# XMLTV format (while merging the new EPG data with the one present in
# the file). The latter is done in parallel for all channels sharing a
# transponder. At the end all XML files in the current directory are
# merged into one using helper script merge.pl
#
# This script does not parse command line parameters. Please edit the
# configuration variables at the top of the script as needed.
#
# Tool "util/tune_dvb.c" is used for tuning the DVB front-end: use "make
# tune_dvb" for building the executable. Note the tool only supports
# "channels.conf" tables mplayer/totem/xine/kaffeine and VDR formats.
#
# 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 3 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2006-2008,2020-2021 by T. Zoerner (tomzo at users.sf.net)
#
use strict;
#
# Configuration variables
#
my $channels_conf = "$ENV{HOME}/.mplayer/channels.conf";
my $device = "/dev/dvb/adapter0/demux0";
my $def_duration = 90;
my $def_pages = "300-399";
my @Nets = (
["Das Erste", "ard"],
["ZDF", "zdf"],
["arte", "arte", "300-699"], # today on 300-300, next 600-699
["KiKA", "kika"],
["3sat", "3sat"],
["ONE", "ard-one"],
["WDR", "wdr"],
["TELE 5", "tele5"],
["SAT.1", "sat1"],
["RTL", "rtl"],
["RTLplus", "rtl-plus"],
["ProSieben", "pro7"],
["RTLZWEI", "rtl2"],
["VOX", "vox"],
["VOXup", "vox-up"],
["sixx", "sixx"],
#["ServusTV", "servus"], # no teletext
["DMAX", "dmax"],
#["MTV", "mtv"], # no teletext
["MDR", "mdr"],
["NDR FS MV", "ndr"],
["SWR Fernsehen RP", "swr"],
["BR Fernsehen", "br3"],
["hr-fernsehen", "hr3", undef, 120],
["rbb Berlin", "rbb"],
["ARD-alpha", "bralpha"],
["phoenix", "phoenix"],
["kabel eins", "kabel1"],
["SUPER RTL", "srtl"],
["Eurosport 1", "eurosport"],
#["TV 5 Monde", "tv5"], # no teletext
["ntv", "ntv", "500-599"],
#["CNN", "cnni", "200-299"], # no teletext
);
# catch signal "INT" for removing "*.tmp" files created for tv_grab_ttx output redirection
my @SigTmps = ();
sub sig_handler {
my ($sig) = @_;
print "Caught a SIG$sig - shutting down\n";
unlink($_) foreach (@SigTmps);
@SigTmps = ();
exit(1);
};
$SIG{'INT'} = \&sig_handler;
while (@Nets) {
# get name of first channel that's not done yet
my $chan = $Nets[0][0];
# query for list of all channels sharing the transponder of this channel
my @tids = ();
if (open(TUNE, "./tune_dvb -c \"$channels_conf\" -P \"$chan\"|")) {
while (<TUNE>)
{
if (/(\d+)\t(.*)/) {
# search returned channel name in the "Nets" list
for (my $idx = 0; $idx <= $#Nets; $idx++) {
if ($Nets[$idx][0] eq $2) {
# match -> schedule this channel in this iteration
push(@tids, [$1, $Nets[$idx][1], $Nets[$idx][2], $Nets[$idx][3]]);
# remove channel from "Nets" list as it is done after this iteration
splice(@Nets, $idx, 1);
last;
}
}
}
}
}
if (@tids) {
# tuning option for analog TV card (obsolete)
#system "v4lctl -c $device setstation \"$chan\"";
# tune the transponder of the selected channels
# FIXME open does not return error when tuning fails
if (open(TUNE, "|./tune_dvb -c \"$channels_conf\" \"$chan\"")) {
print "Capturing from $chan (".scalar(@tids)." PIDs)\n";
my @pids = ();
# run tv_grab_ttx for each of the channels as background process
foreach (@tids) {
my ($tid, $fname, $pages, $duration) = @$_;
$pages = $def_pages if !defined $pages;
$duration = $def_duration if !defined $duration;
print "- capturing $fname (PID $tid, pages $pages, duration $duration)\n";
my $out_file = "ttx-$fname.xml";
push @SigTmps, "${out_file}.tmp";
my $pid = fork();
die "fork: $!\n" unless defined $pid;
if ($pid == 0) {
# child process: build the grabber command line & exec tv_grab_ttx
# - capture into temporary file
# - merge output with previous XML file of same name, if one exists
open(STDOUT, '>', "${out_file}.tmp") or die "Can't redirect STDOUT >${out_file}.tmp: $!";
my @cmd = ("./tv_grab_ttx", "-dev", $device,
"-duration", $duration,
"-page", $pages);
push(@cmd, "-dvbpid", $tid);
push(@cmd, "-merge", $out_file) if -e $out_file && !-z $out_file;
exec @cmd;
die "exec @cmd: $!\n";
}
else {
# parent process: remember the child PID for waiting below
push @pids, [$pid, $out_file];
}
}
# wait for all tv_grab_ttx processes to be done
foreach my $pid (@pids) {
if (waitpid($pid->[0], 0) == $pid->[0]) {
my $stat = $?;
my $out_file = $pid->[1];
# check exit code of tv_grab_ttx: discard output if not zero
if (($stat >> 8) == 0) {
if (-e "${out_file}.tmp") {
# replace XML file for that channel with "*.tmp" file created by tv_grab_ttx output
rename("${out_file}.tmp", $out_file) || warn "error renaming/replacing $out_file: $!\n";
}
else {
warn "missing output file ${out_file}.tmp\n";
}
}
else {
warn "tv_grab_ttx failed for $out_file\n";
unlink "${out_file}.tmp";
}
}
}
@SigTmps = ();
close(TUNE);
}
else {
#print "Failed to tune $chan\n"; # printed by sub-command
}
}
else {
print "No teletext PID for $chan\n";
splice(@Nets, 0, 1);
}
}
print "Finally merging all captured XMLTV files into tv.xml\n";
system "./merge.pl ttx-*.xml > tv.xml";