Gearoid Sheehan
Gearoid Sheehan

Reputation: 23

Changing sample rate of mp3 files

I have a large amount of mp3 files that are not the correct sample rate for the external hardware I want to use them in. Is there any way of changing them all in one go rather than file by file through audacity?

Upvotes: 2

Views: 1610

Answers (1)

Scott Stensland
Scott Stensland

Reputation: 28325

You should mention what OS you're on ... this works on linux

sudo apt install libav-tools  #  install needed tool

// show what we have for one file

avprobe mysong.mp3 

bottom of its output says

Duration: 00:00:01.65, start: 0.000000, bitrate: 192 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, mono, s16p, 192 kb/s

OK its a normal CD quality 44.1kHz so lets lower sample rate in half to 22050 kHz

avconv -i mysong.mp3   -ar 22050  mysong_22k.mp3 

verify what we have now

avprobe mysong_22k.mp3 

Duration: 00:00:01.70, start: 0.050113, bitrate: 33 kb/s
Stream #0:0: Audio: mp3, 22050 Hz, mono, s16p, 32 kb/s

so far so good now lets wrap this to look across all files in one dir

#!/bin/bash

for curr_song in $( ls *mp3 ); do

    echo
    echo "current specs on song -->${curr_song}<--"
    echo

    curr_song_base_name=${curr_song%.*}

    echo curr_song_base_name $curr_song_base_name

    curr_new_output=${curr_song_base_name}_22k.mp3

    echo "avprobe  $curr_song "
          avprobe "$curr_song" 
    echo

    avconv -i ${curr_song}  -ar 22050  ${curr_new_output}

    echo now confirm it worked 
    echo

    avprobe ${curr_new_output}

done

this should get you up and running ... its runs fine for song names without spaces ... code is a tad more involved to handle spaces in filenames ... if you have spaces say so and I'll amend the code ... it cuts each output file by adding a _22k to end of file name so

input   songhere.mp3
output  songhere_22k.mp3

its easy enough to give it a different output directory

Upvotes: 0

Related Questions