I have many music files in flac format and I wanted to convert them to a more compressed mp3 format with ffmpeg on Ubuntu so I can upload them to a storage limited portable music player. To ease the conversion task, I decided to write this simple shell script to do the job. In brief, the script will do the following:
- find all the files with the extension .flac in the current directory
- replace the file name extension .flac with the .mp3 extension
- create a temporary script that calls the ffmpeg command to convert
- run the temporary script
The listing of the shell script is shown below.
# Define the internal field separator as a newline IFS=$'\n' # Find all the *.flac files in the current directory and perform the conversion for f in `find . -name "*.flac" `; do # Use the input flac file name prefix and replace the .flac extension with a .mp3 extension f=$(echo $f | cut -c 3-) outfile=$(basename $f .flac) outfile=$outfile.mp3 echo "Convert $f->$outfile..." # Form the ffmpeg command to convert the input flac file to mp3 cmd="ffmpeg -hide_banner -i \"$f\" -ab 320k -map_metadata 0 -id3v2_version 3 \"$outfile\" " # Create a temporary shell script for running the conversion echo $cmd > /tmp/tmp.sh # Run the conversion to mp3 bash /tmp/tmp.sh # Clean up rm /tmp/tmp.sh done
To use this shell script, you can do the following:
- Save the code listing above to a file e.g. run.sh in a directory, e.g. /path/to
/directory/ - Open up a Linux Terminal.
- In the Terminal, type in the command to change directory to the location of the flac files, e.g. /path/to/music/
$ cd /path/to/music - At the prompt, type in the command to run the shell script.
$ bash /path/to/run.sh
The flac files are converted to mp3 files.
No comments:
Post a Comment