Myth2mp3.pl

From MythTV Official Wiki
Jump to: navigation, search

Important.png Note: The correct title of this article is myth2mp3.pl. It appears incorrectly here due to technical restrictions.


Author Graham Auld
Description User Job to convert recorded radio into MP3's (optionally split in to 5 min chunks) for listening to on an MP3 player.
Supports


I designed this user job script for people who want to record radio programmes to later listen to on their MP3 players. Specifically MP3 players that don't pay any attention to playlists, don't resume where you paused before powering off and have no great UI for seeking in long MP3 files.

The initial aim was to add a job that would convert MythTV recordings to a group of MP3's that would play in the correct order split up into 5 minutes to make it easier to skip to the bit you want.

There is nothing too exciting here, I've seen similar scripts for this sort of job around the MythTV website but none quite did everything.

The default is to make split files and delete the MythTV recording afterwards but there are two flags you can specify that will allow a single MP3 file and not to delete the MythTV recording.

The Perl script mostly glues together a motley crue of handy programs, those with a MythTV backend already installed will probably only need id3v2 and mp3splt(if you want split mp3s) to get started.

If you're having trouble, uncomment DEBUG=1 and everything that's going on will be written to a log file to give you some clues. Usually the most handy line in the log is the first which will tell you what you need to know to run the script on the command line. If it works on the command line and not from Myth then it's probably permissions :o)


Script.png myth2mp3.pl

#!/usr/bin/perl
###########
# Name:    myth2mp3.pl
# Version: 1.0
# Author:  Graham Auld <mythtv@graham.auld.me.uk>
# Dated:   6th June 2012
#
# Designed to run as a mythtv job which will convert your recordings into MP3s
#
# Handy if you want to record radio to listen to on your MP3 player
# 
# Job syntax (MythTVSetup->General) - Remember to allow the jobs to run on the backend!
#   myth2mp3.pl %FILE% %STARTTIMEISO% "%TITLE%" "%DESCRIPTION%" [NOSPLIT] [NODELETE]
#
# The final paramaters are optional,
# if NOSPLIT is specified then the mp3 will not be split into 5 minute chunks
# if NODELETE is specified then the Myth recording won't be deleted after mp3 conversion
#
# The standard operation converts the recording to a folder containing MP3s of about 5 minutes at a time.
# This is handy for MP3 players that don't have good controls for skipping many minutes into a track.
# The combination of filename and tags should mean that the files appear in the correct order.
# The sort of cheapy MP3 players that need this don't usually respect playlists :(
#
# Prerequsites:
# FFmpeg with mp3 code support (-enable-libmp3lame) (Fedora: yum install ffmpeg)
#  A MythTV installation probably already has this.
#   
# id3v2 for id3 tagging (Fedora: yum install id3v2)
#
# mp3splt (http://mp3splt.sourceforge.net/) (only required if you want to split your MP3's)
#        http://prdownloads.sourceforge.net/mp3splt/mp3splt-2.4.2.tar.gz at time of writing
#        or yum install mp3splt if you're on Fedora with the atrmps.net repo.
#
# perl-MythTV (only if you want to delete recordings) (Fedora: yum install perl-MythTV)
#  A MythTV installation probably already has this.
#
#
#
# There are many similar scripts on the web that do this, I couldn't find one that
# would optionaly split and delete the files too so here we are. This is based on
# a shell script I found on the Myth Wiki but couldn't find again to credit.
###########


my $OUTPUTDIR="/hd/www/default/radio";
my $INPUTDIR="/hd/tv";
my $LOGFILE="$INPUTDIR/myth2mp3log.txt";
#my $DEBUG=1;

echo("myth2mp3.pl " . join(' ',@ARGV));

($INFILE,$STARTTIMEISO,$PROGTITLE,$DESC)=@ARGV;
#strip the time just leaving the date
$STARTDATE=substr($STARTTIMEISO,0,10);

#parse the arguments
foreach $ARG (@ARGV) {
  if ($ARG eq 'NOSPLIT') {$NOSPLIT=1;}
  if ($ARG eq 'NODELETE') {$NODELETE=1;}
}

#sanitize the title for filename generation.
#unix and apache might be happy with 's and :s in file names but windows sure isn't!
my $PROGTITLE_CLEAN=$PROGTITLE;
$PROGTITLE_CLEAN=~s/[\:\*\?"<>]//g; #these look better just gone
$PROGTITLE_CLEAN=~s/[\\\/|]/_/g;#these look better underscored
my $OUTFILE="$OUTPUTDIR/$PROGTITLE_CLEAN - $STARTDATE.mp3";


#Recode to mp3
#########
echo("Converting $INPUTDIR/$INFILE to $OUTFILE");
exe("ffmpeg -i \"$INPUTDIR/$INFILE\" -acodec libmp3lame -ab 192k -ar 44100 -y -f mp3 \"$OUTFILE\"");
 

# Tag mp3
##########
echo("Tagging $OUTFILE");
my $YEAR=substr($STARTDATE,0,4);
exe("id3tag --artist=\"$PROGTITLE\" --song=$STARTDATE --album=\"$STARTDATE - $PROGTITLE\" --comment=\"$DESC\" -y$YEAR -gRadio \"$OUTFILE\"");


unless ($NOSPLIT==1) {
# Split into 5min mp3s
###########
echo("Splitting $OUTFILE into $OUTPUTDIR/$STARTDATE - $PROGTITLE_CLEAN/");
exe("mp3splt -t 5.00 -a -d \"$OUTPUTDIR/$STARTDATE - $PROGTITLE_CLEAN\" -o \"\@n2_\@f\" \"$OUTFILE\"");
#reset the permissions as the folder is created with mode 755 but files inside are 664
exe("chmod g+w \"$OUTPUTDIR/$STARTDATE - $PROGTITLE_CLEAN\"");


# Delete whole mp3
##########
echo("Removing the temp entire MP3 file $OUTFILE");
exe("rm -f \"$OUTFILE\"");
}

unless ($NODELETE==1) {
# Delete MythTV file
##########
echo("Ready to delete MythTV recording $INPUTDIR/$INFILE");
use MythTV;
$Myth = new MythTV();
my $file = $Myth->new_recording($INFILE);
$Myth->backend_command(['FORCE_DELETE_RECORDING', $file->to_string()], '0');
}


echo("Done.");

sub echo() {
  if ($LOGFILE eq '') { return; }
  if ($DEBUG) {
    print $_[0] ."\n";
  }
  `echo "$_[0]" >> $LOGFILE`;
}

sub exe() {
  if ($DEBUG) {
   &echo("Exe: " .$_[0]."\n");
  }
  $result=`$_[0]`;
  if ($DEBUG) {
    &echo($result);
  }
}