Archives du mot-clé linux

Random mp3 part extract script

For some reason I needed a script that goes through a directory filled with mp3′s files and extract a random part with a random lenght and outputs it in a wav format. It then concatenated all files in a random order into a final mp3 (or ogg) file.

Install mp3 support for SOX

sudo apt-get install sox lame
sudo apt-get install libsox-fmt-mp3

Get mp3 file duration
soxi -d filename.mp3

EDIT : This following is wrong and unfinished, skip to the next part for a clean perl script.

Script <># As my other scripts, probably not the best way to do it, but it works !
# Feel free of course to use, tweak or do whatever you want with it
# runs fine on xubuntu 13.04

for f in $FILES
echo « fichier $f en cours… »
# action pour chaque fichier $f stock le nom du fichier
DURATION=$(soxi -D « $f »)
MUL=$(($DURATION * 1000))
echo $MUL
# RANDOMEND=$(shuf -i 0-$DURATION | head -1)

>> PerlVersion that actually works and yes, I've learned my lesson not to use
bash for file operations

use v5.10;
use List::Util qw/shuffle/;

foreach (<*.mp3 */*.mp3 */*/*.mp3 */*/*/*.mp3>) {
$rand_end = rand(`soxi -D « $_ »`);
say « file: $_: $rand_end »;
$file= »$i.wav »;
#`sox « $_ » « $file » trim ${rand_end} ${dur}`;
push @list, $file;
`sox @list victory.wav`

When dealing with large amounts of files some errors occur. First of all the list is too long (> 1024 apparently) which stops sox. Some files also have header issues. I finally used Foobar to concatenate the files into one single wav