Skip to content

Instantly share code, notes, and snippets.

@fur-q
Last active August 29, 2015 14:16
Show Gist options
  • Save fur-q/78de01a576c7e7c42ca7 to your computer and use it in GitHub Desktop.
Save fur-q/78de01a576c7e7c42ca7 to your computer and use it in GitHub Desktop.
audio + picture -> youtube upload script
#! /bin/sh
error() {
echo >&2 "$1"
exit 1
}
required() {
command -v "$1" >/dev/null 2>&1 || error "$1 required but not installed; quitting"
}
required "ffmpeg"
required "youtube-upload"
while getopts ":a:d:i:t:" opt; do
case $opt in
a)
ALBUM=$OPTARG
;;
d)
DESCR=$OPTARG
;;
i)
IMAGE=$OPTARG
;;
t)
TAGS=$OPTARG
;;
\?)
error "Invalid option -$OPTARG"
;;
esac
done
shift $((OPTIND - 1))
if [ -z "$ALBUM" ] || [ ! -f "$IMAGE" ] || [ $# -eq 0 ]; then
error "Usage: $0 -a ALBUM-NAME -i IMAGE-FILE [-d DESCRIPTION] [-t TAGS] AUDIO-FILES..."
fi
[ ! -z "$DESCR" ] && DESCR="$DESCR\n"
[ ! -z "$TAGS" ] && TAGS=", $TAGS"
process() {
echo -n "Uploading $(basename "$1")..."
local md="$(ffmpeg -i "$1" -f ffmetadata 2>/dev/null -)"
[ -z "$md" ] && return 1
local artist="$(echo "$md" | grep -i artist | cut -d= -f2)"
local title="$(echo "$md" | grep -i title | cut -d= -f2)"
[ -z "$title" ] && return 1
[ "${1##*.}" = "flac" ] && local acodec="-c:a libmp3lame -q:a 0" || local acodec="-c:a copy"
ffmpeg -y -loop 1 -r 5 -i "$IMAGE" -i "$1" -shortest -c:v libx264 -preset ultrafast -tune stillimage -pix_fmt yuv420p $acodec "$2" 2>/dev/null || return 1
youtube-upload --title="$ALBUM - $title" --description="${DESCR}Composer: $artist" --category=Gaming --location="=" --tags="OST, soundtrack, $artist, $ALBUM, $title$TAGS" "$2" >/dev/null 2>&1 || return 1
echo "OK"
}
trap "exit 1" SIGINT
STATUS=0
for file; do
tempfile="/tmp/ytn_$$.mkv"
process "$file" "$tempfile" || { STATUS=1; echo "failed"; }
rm $tempfile
done
exit $STATUS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment