Skip to content

Instantly share code, notes, and snippets.

@J0J0
Created April 5, 2023 09:59
Show Gist options
  • Save J0J0/540700cf43eb31ad6bd930d73cfc2c59 to your computer and use it in GitHub Desktop.
Save J0J0/540700cf43eb31ad6bd930d73cfc2c59 to your computer and use it in GitHub Desktop.
Script to join many mp3 files into fewer ones (without re-encoding)
#!/bin/zsh
# first argument: number of files to join into new one, default 5
num=${1:-5}
tmpdir=$(mktemp --directory)
outdir=$(date +"%Y-%m-%d_%H-%M")
mkdir $outdir
ls > $tmpdir/files
(
cd $tmpdir
split --lines=$num files
{
print "#!/bin/zsh"
print "out=$outdir"
} > ff
cat >> ff <<-'FOOBAR'
(( $ARGC == 1 )) || { exit 1; }
ffmpeg -loglevel quiet -i "concat:${(j:|:)${(f)$(cat $1)}}" -acodec copy "$out/$(basename $1).mp3"
FOOBAR
chmod u+x ff
)
parallel -j 4 $tmpdir/ff '{}' ::: $tmpdir/x*
@J0J0
Copy link
Author

J0J0 commented Apr 5, 2023

Notable prerequisites:

  • zsh
  • ffmpeg
  • GNU parallel (which could be easily avoided, though)

Usage:

cd /path/to/many/mp3files
/path/to/glue.sh 10

will take groups of 10 files from the current directory and join them into new files to be put in a subdirectory of the original one (i.e. if there were 300 files before, it will be 30 afterwards in this example).

Notes:

  • Original filenames are lost in the process.
  • In case of non-mp3 files in the directory, one needs to tweak line 10 to exclude, e.g. "folder.jpg" etc.
  • In the same line, one might want to use ls -v instead to sort numbers naturally, i.e. 1,2,10 instead of 1,10,2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment