Skip to content

Instantly share code, notes, and snippets.

@datashaman
Last active May 26, 2018 18:07
Show Gist options
  • Save datashaman/6866879e4ad07b0483e6efd9ea49d8a2 to your computer and use it in GitHub Desktop.
Save datashaman/6866879e4ad07b0483e6efd9ea49d8a2 to your computer and use it in GitHub Desktop.
Convert audio to 320kbps CBR .mp3, convert video to x264 .mp4
#!/usr/bin/env bash
#
# Usage:
# transcode filename [filename...]
#
# File type is determined using:
#
# file --mime-type filename
#
# Audio files:
# Audio: anything -> 320kbps CBR MP3
# Output: .mp3 alongside the source file(s)
#
# Video files:
# Video: anything -> x264
# Audio: copy
# Output: .mp4 alongside the source file(s)
#
# Anything else is ignored
#
# I live here: https://gist.github.com/datashaman/6866879e4ad07b0483e6efd9ea49d8a2
#
trap 'exit -1' INT
BINARY=ffmpeg
BITRATE=320k
CRF=23
PRESET=slow
for INPUT in "$@"
do
MIME_TYPE=$(file -b --mime-type "${INPUT}")
DIRNAME=$(dirname "${INPUT}")
BASENAME=$(basename "${INPUT}")
# EXTENSION="${BASENAME##*.}"
FILENAME="${BASENAME%.*}"
if [[ $MIME_TYPE == audio* ]]; then
OUTPUT="${DIRNAME}/${FILENAME}.mp3"
$BINARY -i "${INPUT}" -vn -ab ${BITRATE} "${OUTPUT}"
elif [[ $MIME_TYPE == video* ]]; then
OUTPUT="${DIRNAME}/${FILENAME}.mp4"
$BINARY -i "${INPUT}" -c:v libx264 -preset ${PRESET} -crf ${CRF} -c:a copy "${OUTPUT}"
fi
done
@ryanwild
Copy link

ryanwild commented May 5, 2017

rad

@datashaman
Copy link
Author

Updated to add autodetection of input file type, and transcoding of audio files to 320kbps CBR MP3s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment