Skip to content

Instantly share code, notes, and snippets.

@craigp
Forked from datashaman/transcode
Created May 26, 2018 18:07
Show Gist options
  • Save craigp/bfaa99d9705f0dc5b3753dbfecc85813 to your computer and use it in GitHub Desktop.
Save craigp/bfaa99d9705f0dc5b3753dbfecc85813 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment