Created
October 2, 2012 17:24
-
-
Save anonymous/3821384 to your computer and use it in GitHub Desktop.
mora file to iOS-compatible m4a converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
mp4box=/Applications/Osmo4.app/Contents/MacOS/MP4Box | |
tempname=mora | |
if [ ! -f "$mp4box" ] || [ ! -x "$mp4box" ]; then | |
echo "cannot find mp4box executable" > /dev/stderr | |
exit 1 | |
fi | |
for FILE in "$@"; do | |
if [ "${FILE:0-4}" = .mp4 ]; then | |
mv -- "$FILE" "${FILE::-4}.m4a" | |
FILE="${FILE::-4}.m4a" | |
fi | |
# get info | |
INFO=$("$mp4box" -noprog -quiet -info "$FILE") | |
if echo "$INFO" | grep -si error; then | |
echo invalid input file: "$FILE" > /dev/stderr | |
continue | |
fi | |
# extract audio | |
TEMPDIR=$(mktemp -d -t "$tempname") | |
"$mp4box" -noprog -quiet -single 1 -out "$TEMPDIR/output" "$FILE" > /dev/null | |
TEMPFILE="$TEMPDIR/output.m4a" | |
# extract cover | |
"$mp4box" -noprog -quiet -out "$TEMPDIR/cover.jpg" -dump-cover "$FILE" > /dev/null | |
if [ -f "$TEMPDIR/cover.jpg" ]; then | |
"$mp4box" -noprog -quiet -itags cover="$TEMPDIR/cover.jpg" "$TEMPFILE" > /dev/null | |
elif [ -f "$TEMPDIR/cover.png" ]; then | |
"$mp4box" -noprog -quiet -itags cover="$TEMPDIR/cover.png" "$TEMPFILE" > /dev/null | |
fi | |
# write copyright | |
COPYRIGHT=$(echo "$INFO" | sed -n '/^Copyrights:$/{n;p;q;}') | |
COPYRIGHT=${COPYRIGHT:7} # first character is tab, then 6 characters for region | |
if [ -n "$COPYRIGHT" ]; then | |
"$mp4box" -noprog -quiet -cprt "$COPYRIGHT" "$TEMPFILE" > /dev/null | |
fi | |
# write itunes tags | |
echo "$INFO" | sed -n '/^iTunes Info:$/,/^$/p' | while IFS= read -r LINE; do | |
KEY=$(echo "${LINE:1}" | grep -oE '^[^:]+') | |
VALUE=$(echo "${LINE}" | grep -oE '[^:]+$') | |
VALUE="${VALUE:1}" # first character is space | |
if [[ ! $KEY =~ ^\ *$ ]] && [[ ! $VALUE =~ ^\ *$ ]]; then | |
if [ "$KEY" = "Cover Art" ]; then | |
continue | |
fi | |
"$mp4box" -noprog -quiet -itags "$(echo "$KEY" | tr '[A-Z ]' '[a-z_]')"="$VALUE" "$TEMPFILE" > /dev/null | |
fi | |
done | |
mv "$FILE" "$FILE".orig | |
cp "$TEMPFILE" "$FILE" | |
rm -r "$TEMPDIR" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment