source: gutenbach/debian/lib/sipbmp3-filter @ 7e27cc3

debianmacno-cupsnodebathenaweb
Last change on this file since 7e27cc3 was 7e27cc3, checked in by root <root>, 16 years ago

from quentin: split_playlist

  • Property mode set to 100755
File size: 6.2 KB
Line 
1#!/usr/athena/bin/perl
2# Play the data on STDIN as an audio file
3#
4# $Id: sipbmp3-filter,v 1.19 2008-10-04 07:50:26 root Exp $
5# $Source: /tmp/tmp.UFBNno9997/RCS/sipbmp3-filter,v $
6#
7# TODO
8# ----
9# Make this structured code. It's a mess.
10# Repeat what we just played for EXT files too
11# Support HTTP Auth on ogg streams
12# License, cleanup and package
13#
14# Jered Floyd <jered@mit.edu> takes very little credit for this code
15# apparently neither does Quentin Smith <quentin@mit.edu>
16
17use Getopt::Std;
18
19use Image::ExifTool qw(ImageInfo);
20use File::Spec::Functions;
21use LWP::UserAgent;
22use Data::Dumper;
23
24my $ua = new LWP::UserAgent;
25
26close(STDERR);
27open(STDERR, ">>", "/tmp/sipbmp3.log") or warn "Couldn't open log: $!";
28
29$ENV{"TERM"}="vt100";
30
31print STDERR "STDERR FROM SPOOL FILTER\n";
32
33# set real uid to be effective uid
34$< = $>;
35
36# Select the correct output device and set the volume
37#system("amixer -q set Headphone 100\% unmute");
38
39# The command line we get from lpd is (no spaces between options and args):
40#  -C lpr -C class
41#  -A LPRng internal identifier
42#  -H originating host
43#  -J lpr -J jobname (default: list of files)
44#  -L lpr -U username
45#  -P logname
46#  -Q queuename (lpr -Q)
47#  -Z random user-specified options
48#  -a printcap af (accounting file name)
49#  -d printcap sd entry (spool dir)
50#  -e print job data file name (currently being processed)
51#  -h print job originiating host (same as -H)
52#  -j job number in spool queue
53#  -k print job control file name
54#  -l printcap pl (page length)
55#  -n user name (same as -L)
56#  -s printcap sf (status file)
57#  -w printcap pw (page width)
58#  -x printcap px (page x dimension)
59#  -y printcap py (page y dimension)
60# accounting file name
61
62# All the filter_options from lpd
63getopt('ACFHJLPQRZacdefhijklnprswxy', \%opts);
64
65# Status messages at start of playback
66open(ZEPHYR, '|/usr/athena/bin/zwrite -d -n -c sipb-auto -i ' .
67  'sipbmp3@zsr -s "SIPB LPR music spooler"');
68print(ZEPHYR "$opts{'n'}\@$opts{'H'} is playing:\n");
69
70# So, the file we're currently processing is "-d/-e".
71
72# Read the metadata information from the file.
73my ($filepath) = catfile($opts{'d'}, $opts{'e'});
74my ($fileinfo) = ImageInfo($filepath);
75my ($magic) = $fileinfo->{FileType};
76
77if ($magic) {
78    printf(ZEPHYR "%s file %s\n", $magic, $opts{'J'});
79    printf(ZEPHYR "\@b(%s)\n", $fileinfo->{'Title'}) if exists $fileinfo->{'Title'};
80    foreach my $key (qw/Artist Album AlbumArtist Tracknumber TrackDuration/) {
81        printf(ZEPHYR "%s\n", $fileinfo->{$key}) if exists $fileinfo->{$key};
82    }
83}
84elsif ($opts{'C'} eq 'Z') {
85    $filepath = resolve_external_reference($filepath, \%opts);
86}
87elsif (-T $filepath) {
88    split_playlist($filepath, \%opts);
89    close(ZEPHYR);
90    exit 0;
91}
92#printf(STDERR Dumper(\%opts));
93#printf(STDERR "Job priority %s\n", $opts{'C'}) if $opts{'C'} eq 'Z';
94#printf(ZEPHYR "Job priority %s\n", $opts{'C'}) if ($opts{'C'} && ($opts{'C'} ne 'A'));
95close(ZEPHYR);
96play_mplayer_audio($filepath, \%opts);
97
98# Play an external stream reference
99sub resolve_external_reference {
100    # Retrieve those command line opts.
101    my ($filepath, $opts) = @_;
102
103    my $format, $uri, $userpass;
104
105    if (<STDIN> =~ /^(\S+)/) {
106        $uri=$1;
107
108        my $response = $ua->head($uri);
109       
110        $contenttype=($response->content_type() or "unknown");
111       
112        if ($contenttype eq "audio/mpeg") { $format="MP3" }
113        elsif ($contenttype eq "application/x-ogg") { $format="OGG" }
114        elsif ($contenttype eq "application/ogg") { $format="OGG" }
115        elsif ($contenttype eq "audio/x-scpls") { $format="SHOUTCAST" }
116        else {
117            print ZEPHYR
118                "Unknown Content-Type $contenttype for URI $uri\n";
119            close(ZEPHYR);
120        }
121    } else {
122        print ZEPHYR "Couldn't read URI for external reference\n";
123        close(ZEPHYR);
124        return $filepath;
125    }
126
127    if ($format eq "SHOUTCAST") {
128        print ZEPHYR "Shoutcast playlist...\n";
129        #Don't close ZEPHYR yet, will print the name of the stream if available
130        return &get_shoutcast($uri);
131    } elsif ($format eq "MP3") {
132    } elsif ($format eq "OGG") {
133    } else {
134      print ZEPHYR "Unrecognized stream format: $format\n";
135    }
136    return $uri;
137}
138
139sub split_playlist {
140    my ($file, $opts) = @_;
141
142    my $i = 0;
143   
144    while (<STDIN>) {
145        chomp;
146        if (/^([^#]\S+)/) {
147            printf (STDERR "Found line: %s\n", $_);
148            open(LPR, "|-", qw/mit-lpr -Psipbmp3@localhost -CZ/, '-J'.$opts->{J});
149            print LPR $1;
150            close(LPR);
151        $i++;
152        }
153    }
154    printf(ZEPHYR "Playlist containing %d valid entries, split into separate jobs.\n", $i);
155}
156
157# Process a Shoutcast playlist
158# get_shoutcast(URI)
159sub get_shoutcast {
160  my $uri = shift(@_);
161 
162  my $response = $ua->get($uri);
163
164  foreach (split("\n", $response->content())) {
165      if (/^File\d+=(\S+)/) {
166          push(@uris, $1);
167      }
168      if (/^Title\d+=(.+)$/) {
169          push(@titles, $1);
170      }
171  }
172 
173  # choose a random server
174  $server = int(rand scalar(@uris));
175  # print the name of the stream if available
176  print ZEPHYR "$titles[$server]\n";
177  return $uris[$server];
178}
179
180sub play_mplayer_audio {
181    my ($filepath, $opts) = @_;
182
183    # Prepare to write status:
184    open(ZEPHYR, '|/usr/athena/bin/zwrite -d -n -c sipb-auto -i ' .
185         'sipbmp3@zsr -s "SIPB LPR music spooler"');
186   
187    # fork for mpg123
188    my $pid = open(MP3STATUS, "-|");
189    unless (defined $pid) {
190        print ZEPHYR "Couldn't fork: $!\n";
191        close(ZEPHYR);
192        return;
193    }
194   
195    if ($pid) { #parent
196        # Check if there were any errors
197        if ($_ = <MP3STATUS>) {
198            print ZEPHYR "Playback completed with the following errors:\n";
199            print ZEPHYR $_;
200            while (<MP3STATUS>) {
201                print ZEPHYR $_;
202            }
203        } else {
204            print ZEPHYR "Playback completed successfully.\n";
205        }
206        close(MP3STATUS) || print ZEPHYR "mplayer exited $?\n";
207       
208        close(ZEPHYR);
209    }
210  else { # child
211      # redirect STDERR to STDOUT
212      open STDERR, '>&STDOUT';
213      # make sure that mplayer doesn't try to intepret the file as keyboard input
214      close(STDIN);
215      open(STDIN, "/dev/null");
216      #print STDERR Dumper([qw|/usr/bin/mplayer -nolirc -ao alsa -quiet|, $filepath]);
217      exec(qw|/usr/bin/mplayer -nolirc -ao alsa -really-quiet|, $filepath) ||
218          die "Couldn't exec";
219  }
220}
221
222# ID3 comments often have useless crap because tools like iTunes were
223# written by drooling idiots
224sub filter_comment {
225  my $comment = shift(@_);
226
227  if ($comment =~ /^engiTunes_CDDB/) {
228    return undef;
229  }
230  return $comment;
231}
232
233
Note: See TracBrowser for help on using the repository browser.