[MPlayer-users] Perl script to copy RealAudio to mp3 (uses mplayer).

Albert Schueller schuelaw at whitman.edu
Tue May 18 10:38:47 CEST 2004


Tue May 18 01:38:10 PDT 2004

Hi all,

Here's a perl script that uses mplayer, sox, and lame to copy real
audio streams to mp3 files.  Enjoy.

A



#!/usr/bin/perl

# A perl script that uses mplayer to copy a real audio stream to an mp3
# file.  Requires: sox, lame, mplayer 1.0pre4, and of course perl.

# USAGE:
# To determine stream urls, in linux anyway, run the regular real player and
# look inside the tmp file it creates in /tmp.

# This file is set up to copy a US radio program that I enjoy, called All
# Things Considered, from that program's audio archives.  You can modify
# the list of urls and corresponding filenames to your own purposes.

# COMMENTS:
# It's overly complicated because mplayer does not automatically exit at
# the end of a fixed-length (non-live) real audio stream, hence there's a
# complicated scheme to detect when the audio outputfile has stopped
# changing.

# Perl hackers out there, feel free to suggest/implement changes.  Post
# them to the list.

# Mplayer developers, it would be nice if mplayer would automatically
# exit at the end of a fixed length real audio stream.
#
# Mplayer developers, it would be nice if mencoder could write to the mp3
# file directly.  Though it seems able to do streaming, it requires there
# to be video and doesn't have the -vc dummy -vo null options.

###########################################################################
# Various fudge constants.
###########################################################################
# Default signal to use when killing the stream capture program.
$DEFAULT_KILL_SIGNAL=15;
# The number of seconds to wait before starting to check the data file.
$START_TIME=120;
# The number of seconds without modification before the data file is
# declared finished.
$STALE_TIME=60;
# The time interval, in seconds, to check the data file's status.
$CHECK_TIME=1;

# Program date.
$DATE=20040517;

#list of stream urls to rip.
@url = (
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_01.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_02.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_03.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_04.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_05.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_06.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_07.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_08.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_09.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_10.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_11.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_12.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_13.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_14.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_15.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_16.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_17.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_18.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_19.rm",
 "rtsp://real.npr.na-central.speedera.net/real.npr.na-central/atc/$DATE"."_atc_20.rm"
);

#list of corresponding filename prefixes, end result will be [filename prefix].mp3
@filename = (
 "ATC-$DATE-01",
 "ATC-$DATE-02",
 "ATC-$DATE-03",
 "ATC-$DATE-04",
 "ATC-$DATE-05",
 "ATC-$DATE-06",
 "ATC-$DATE-07",
 "ATC-$DATE-08",
 "ATC-$DATE-09",
 "ATC-$DATE-10",
 "ATC-$DATE-11",
 "ATC-$DATE-12",
 "ATC-$DATE-13",
 "ATC-$DATE-14",
 "ATC-$DATE-15",
 "ATC-$DATE-16",
 "ATC-$DATE-17",
 "ATC-$DATE-18",
 "ATC-$DATE-19",
 "ATC-$DATE-20"
);


for ($i=0;$i<=$#url;$i++) {

 # Get start time for the stream copy so can report how long it took.
 $begin=time();

 # Start stream capture program.
 system "mplayer -vc dummy -vo null -af resample=44100 -ao pcm -aofile $filename[$i].pcm $url[$i]&";

 # Get process id.  If more than one, exit with error.
 $pid = `ps -C mplayer -o pid=`;
 @pids = split("\n",$pid);
 if ($#pids > 0) { 
  print "There are conflicting processes running!\n";
  print "Killing newest process.  Kill other processes before trying to run again.\n";
  kill $DEFAULT_KILL_SIGNAL, $pids[$#pid];
  exit(0);
 }
 $pid = $pids[0];

 # Give stream capture program time to get going.
 sleep $START_TIME;

 # If loop detects a difference of more than $STALE_TIME seconds between
 # the data file mod time and the current time, it stops, else it sleeps
 # for $CHECK_TIME seconds.

 while (time() - (stat("$filename[$i].pcm"))[9] < $STALE_TIME) { 
  sleep $CHECK_TIME; 
 }

 # File has stopped growing, kill mplayer.
 kill $DEFAULT_KILL_SIGNAL, $pid;

 # Compute time for stream to be captured.
 $total_time = time()-$begin;
 $total_time /= 60.0;

 # Convert pcm to 1 ch, wav.
 system "sox -s -w -r 44100 -traw -c 2 $filename[$i].pcm -c 1 $filename[$i].wav";

 # Delete pcm file.
 unlink "$filename[$i].pcm";

 # MP3 encoding.
 system "lame -b 32 $filename[$i].wav $filename[$i].mp3";

 # Delete wav file.
 unlink "$filename[$i].wav";
 print "Approx download time: $total_time mins.\n";
 print "Your stream is in $filename[$i].mp3\n";


}




More information about the MPlayer-users mailing list