I record audio notes to myself and it can be difficult to go back and listen to them. This is especially frustrating because I have to be at my computer to do so. I needed to be able to take my smaller notes in a single file on my phone and listen to them while I commute to work. After doing some research online, I was able to write the following bash script to do just that:
#!/bin/bash mkdir temp_mp3 find -size -20000k -name '*.WAV' | \ while read f; do echo "Processing $f" lame "$f" "$f.mp3" echo "cat $f.mp3 > Combined.mp3" cat "$f".mp3 Combined-temp.mp3 > Combined.mp3 mv Combined.mp3 Combined-temp.mp3 mv "$f".mp3 ./temp_mp3/ done mv Combined-temp.mp3 Combined.mp3 mp3val Combined.mp3 -f -nb
The bash script is straight-forward, but for those of you that cannot read the script:
- Run this script in your desired directory
- A list of all of the WAV files that are less than 20 megabytes is generated (using find)
- Each file will be changed into an MP3 (using LAME)
- The current MP3 will be concatenated into a single MP3 file, Combined.mp3 (using cat)
- After it has been concatenated, the MP3 file is moved to a directory, ./temp_mp3
- Once all of the applicable WAV files have been processed, the Combined-temp.mp3 is renamed to Combined.mp3
- An mp3val is ran on the newly named Combined.mp3 to clean up some of the ID3 header information
- Enjoy listening to your new mp3
This information was gathered from the following sources:
- Making BASH script `for` handle filenames with spaces (or workaround)
- Handle filename with spaces inside Bash-script – Super User
- (unix shell scripting) Trim last 3 characters of a line WITHOUT using sed, or perl, etc
- bash loop over file
- bash for loop
- Use bash to read a file and then execute a command from the words extracted
- Convert All .flv files in a folder to MP3
- bash script: making variable the result of a command
- How Do I Find Files with Linux?
I’d like to humbly offer a more concise and reliable way of doing this, since concatenating mp3s will not always operate the way it should.
You will need two programs, “sox” and “lame”. First we concatenate the .wavs, _then_ encode to mp3 so it’s one correctly encoded file.
#!/bin/bash
touch combined.wav # create if it isn't there, otherwise update timestamp
find -size -20000k -name '*.WAV' | while read file; do
echo "Adding $file"
sox "$file" combined.wav combined_temp.wav
mv combined_temp.wav combined.wav
done
lame combined.wav combined.mp3
Let me know what you think. Thanks!
Good evening, Eric. Sorry for the delay.
Thanks for sending this along. I did not know about the sox program! This certainly does make the process more precise and accurate when encoding a WAV vs. concatenating MP3 files.
This process has worked for me, but I don’t know if it has played around with jumping through the file since I mainly listen to these on my phone without looking at the screen very much.
I am going to work on another script to chop up WAV files and concatenate them so make a new cutup of the original source. I’ll be playing around with my soundscapes.
Once again, thanks for the information and help. Stay tuned for more media scripts.
Cheers!