Saturday, 9 December 2017

Speeding up / down audio files

My children want to have some of the songs they listen on the radio in their MP3s.

This is a no-brainer. After they find the videos they want in YouTube, I put all the URLs in a file ('bajar.txt') and can get all the MP3 files with the following command.

youtube-dl -a bajar.txt -x --audio-format mp3 -o '%(title)s.%(ext)s'

But the second step is a bit more fun. One of the MP3s is wrong and it plays the songs a bit slower than it should. Instead of buying a new one, I just speed up the MP3 files in the computer before uploading them to the player. This script will just get all the *mp3 files in a directory, speed them up according to the SP factor, and copy them to the directory 'emma_mp3s', ready to upload to my daughter's player (modifying the IFS variable allows having white spaces in the file names)

#!/bin/bash
# mp3speed.sh                                                                                                                                                          # Adapted from https://ubuntuforums.org/showthread.php?t=1259851                    
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

FILES="*.mp3"
mkdir -p emma_mp3s
for f in $FILES

do
 AR=96k
 SP=1.08897   # Speed up:  ini_length * (1/speedup) = final_length

 echo Encoding $f

 mplayer -af scaletempo -speed $SP $f -vc null -vo null -ao pcm:fast:waveheader:file=$f.wav

 ffmpeg -i $f.wav -acodec libmp3lame -ab $AR emma_mp3s/$f
 rm $f.wav

done

IFS=$SAVEIFS