Tuesday, September 19, 2006

Perl script to split MP3 files

This is a simple script I used recently to split a large (50 minute) MP3 file into a series of smaller files. I used the MP3::Splitter module. MP3::Splitter is very versatile, so what I am doing does not use all of its functionality. But this does a good quick and dirty job of chopping up a file into bits. This comes in handy so you don't have a huge file to fast forward through on your iPod or other MP3 player.

The first two parameters of mp3split are the name of the file and a hash with options-- the only one I use is to set "verbose" to true. After that are a series of array references that describe each bit I want written out to a file.

The first parameter is the start time. I start the very first piece at 0 minutes. After that, each bit starts relative to the end of the last bit-- this is marked by the > symbol.

The next parameter shows how long the bit should be in seconds-- I chose to make each one two minutes long.

Since my MP3 file was about 50 minutes long, I just made 25 two minute bits. The last bit has a special marker as the finish: =INF. This means to use all of the rest of the file.




use MP3::Splitter;
mp3split('01-italian.mp3', {verbose => 1},
["0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "120"],
[">0", "=INF"]
);

2 comments:

  1. Trying to figure out the size of the mp3 so that I can iterate through "[">0", "120"]" the correct amount of times....any suggestions on how to use mp3::splitter's read functionality to do this?

    ReplyDelete
  2. Great question; I'm sure it would do it (there must be a function that reads the ID3 tags, for example). I'm not in a great place to look it up, though. Try running "perldoc mp3::splitter" from the command line. Good luck!

    ReplyDelete

I moderate comments blog posts over 14 days old. This keeps a lot of spam away. I generally am all right about moderating. Thanks for understanding.