add mtube.sh
authorColin P. McCabe <cmccabe@apache.org>
Wed, 4 Jan 2023 14:43:06 +0000 (06:43 -0800)
committerColin P. McCabe <cmccabe@apache.org>
Wed, 4 Jan 2023 14:43:06 +0000 (06:43 -0800)
mtube.sh [new file with mode: 0755]

diff --git a/mtube.sh b/mtube.sh
new file mode 100755 (executable)
index 0000000..2343649
--- /dev/null
+++ b/mtube.sh
@@ -0,0 +1,21 @@
+#!/usr/bin/env bash
+
+die() {
+    echo $@
+    exit 1
+}
+
+MTUBE_ARGS=$@
+MTUBE_DIR="./mtube-$$"
+ABSPATH_MTUBE_DIR="$(pwd)/${MTUBE_DIR}"
+mkdir "${MTUBE_DIR}" || die "failed to mkdir ${MTUBE_DIR}"
+trap "rm -rf -- \"${ABSPATH_MTUBE_DIR}\"" EXIT
+pushd "${MTUBE_DIR}" || die "failed to pushed ${MTUBE_DIR}"
+youtube-dl -f bestaudio -- ${MTUBE_ARGS} || die "youtube-dl failed"
+music-to-mp3.sh * || die "music-to-mp3.sh failed"
+shopt -s extglob
+rm !(*.mp3) || die "rm of non-mp3 files failed"
+set -x
+mv *.mp3 ..
+set +x
+popd || die "failed to popd"