Skip to content

Instantly share code, notes, and snippets.

@smj10j
Last active August 12, 2016 04:55
Show Gist options
  • Select an option

  • Save smj10j/65ce9bde2fd9a020f786 to your computer and use it in GitHub Desktop.

Select an option

Save smj10j/65ce9bde2fd9a020f786 to your computer and use it in GitHub Desktop.
Shell script that converts ac3 audio files to mp3 using ffmpeg
#!/bin/bash
# ac3 => mp3 conversion for files in same dir
# Converting....
# echo "Starting 5 runs of ffmpeg for conversion..."
# for i in $(seq 5); do
# ( find . -regex '.*\.mp3$' -exec ffmpeg -n -i "{}" -pix_fmt rgb24 -vsync 2 -b:a 192K -aq 2 -f mp3 "{}.done" \;
# done
find . -regex '.*\.mp3$' -exec ffmpeg -n -i "{}" -pix_fmt rgb24 -vsync 2 -b:a 192K -aq 2 -f mp3 "{}.done" \;
find . -regex '.*\.mp3$' -exec lame -m s --mp3input -V 4 --vbr-new -q 2 -b 128 -B 160 "{}" "{}.lame.done" \;
watch "find . -regex '.*\.done' | wc -l"
wait
# Fix the names of the new versions
#printf '%s\0' echo **/*.mp3 | xargs -0 -L 1 rename 's/\.mp3\z/.bak/' *
#printf '%s\0' echo **/*.done | xargs -0 -L 1 rename 's/\.done\z//' *
find . -regex '.*\.mp3$' -exec rename 's/\.mp3\z/.bak/' * \;
#find . -regex '.*\.done$' -exec rename 's/\.done\z//' * \;
find . -regex '.*\.done$' -exec bash -c 'mv "{}" "$(echo "{}" | sed 's/\.done//')"' \;
# Verify we got what we wanted
echo "Verifying files were encoded properly..."
find . -regex '.*\.done$' -exec file -I "{}" \; | egrep -v "audio/mpeg; charset=binary$"
find . -regex '.*\.done$' -exec file "{}" \; | egrep -v "Audio file with ID3"
# Backup
find . -regex '.*\.bak$' -exec bash -c 'mkdir -p ~/Downloads/backup/./"$(dirname "{}")"; cp "{}" ~/Downloads/backup/"{}"' \;
# Delete originals
# echo "Removing all files with the previous encoding..."
# find . -regex '.*\.ac3$' -exec rm "{}" \;
#
# # Fix the names of the new versions
# echo "Setting the correct extension on the newly-encoded files..."
# find . -regex '.*\.mp3$' -exec echo 'IF="{}"; OF="$(echo "{}" | sed 's/\.mp3$//g')"; mv "$IF" "$OF"' \; | bash -s
# #find . -regex '.*\.done\.mp3$' -exec bash -c 'OF="$(echo "{}" | sed 's/\.mp3\.done\.mp3/.mp3/g')"; echo "$(echo "mv \"{}\" \"$OF\"")"' \; | bash -s
find . -type f -exec echo "{}" \; | wc -l
echo ""
echo "Done!"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment