Monday, November 8, 2010

GStreamer in Perl

Scott: Is there any way to get a nicer looking recording program? What you wrote works okay. A GUI might be nice. The ability to edit the sermon title would be fantastic.

Narrator: Hmmmm, it may be time for the next version of our church's digital recording software. Okay, this time let's use a graphical interface instead of the console. Easy enough with Glade and Perl. Now how does Perl record from the microphone?

Google and CPAN turned up one module. It worked okay - not great, just okay. Thing was, Ubuntu does not provide a package for that module. I really wanted something that updated with the rest of the system. That led me into the world of GStreamer.

GStreamer's documentation uses C instead of Perl. Their examples involve too much complexity. Perl makes so many things easy - why not this? Turns out that GStreamer works much simpler than it appears.

Start by installing the gstreamer-tools package. The gst-inspect command produces a list of available sources.
#gst-inspect | grep src
dvdread: dvdreadsrc: DVD Source
sndfile: sfsrc: Sndfile source
rfbsrc: rfbsrc: Rfb source
dccp: dccpserversrc: DCCP server source
...
pulseaudio: pulsesrc: PulseAudio Audio Source
video4linux: v4lsrc: Video (video4linux/raw) Source
rtsp: rtspsrc: RTSP packet receiver
alsa: alsasrc: Audio source (ALSA)


The gst-launch manual page then provides examples for testing my own recording pipeline.
#gst-launch alsasrc ! lame ! filesink location="test.mp3"

Talk into the microphone. Hit Ctrl-C after a bit. Presto! You have an MP3 recording of whatever you said. We now know that GStreamer can record from the microphone into a file. How does it work in Perl?
#!/usr/bin/perl
use GStreamer -init;
my $loop = Glib::MainLoop->new( );
my $play = GStreamer::parse_launch( qq(alsasrc ! lame ! filesink location="test.mp3") );
$play->set_state( 'playing' );
$loop->run( );


Really, that's it. This little script does the same thing as gst-launch above. Pretty simple, huh?

No comments:

Post a Comment