Archives du mot-clé ffmpeg

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 <random_audio_extractor.sh># 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
#!/bin/bash

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

>> 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/;

@list=();
$i=0;
foreach (<*.mp3 */*.mp3 */*/*.mp3 */*/*/*.mp3>) {
$rand_end = rand(`soxi -D « $_ »`);
say « file: $_: $rand_end »;
$dur=rand(0.5);
$file= »$i.wav »;
#`sox « $_ » « $file » trim ${rand_end} ${dur}`;
push @list, $file;
$i++;
}
@list=shuffle(@list);
`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