Skip to content

Instantly share code, notes, and snippets.

@cronfy
Created November 14, 2020 16:49
Show Gist options
  • Save cronfy/741b6074878da1d22d51a71909bb62d0 to your computer and use it in GitHub Desktop.
Save cronfy/741b6074878da1d22d51a71909bb62d0 to your computer and use it in GitHub Desktop.
Download getcourse video
#!/usr/bin/env bash
set -euo pipefail
function failExit() {
local msg="$1"
echo "FAIL: $msg" >&2
exit 1
}
TASKNAME="${1-}"
LINK="${2-}"
[ -n "$TASKNAME" ] || failExit "no task name"
[ -n "$LINK" ] || failExit "no link"
cd "`dirname $0`"
if [ ! -e "$TASKNAME/dl.ended" ] ; then
mkdir -p "$TASKNAME"
MASTER_FILE="$TASKNAME/master.txt"
wget --continue -O "$MASTER_FILE" "$LINK"
PLAYLIST_LINK="`grep -E "^https" "$MASTER_FILE" | tail -n 1`"
PLAYLIST_FILE="$TASKNAME/playlist.txt"
wget --continue -O "$PLAYLIST_FILE" "$PLAYLIST_LINK"
cnt=1
grep '^https' "$PLAYLIST_FILE" | while read url ; do
echo $cnt
wget -O "$TASKNAME/$cnt.mp4" --continue "$url"
cnt=$((cnt + 1))
done
touch "$TASKNAME/dl.ended"
else
echo " -- Already downloaded, skipping."
fi
if [ ! -e "$TASKNAME/combine1.ended" ] ; then
ANYFILE="false"
FILES=""
START=1
END=1000
CYCLE=1
while true ; do
echo "CYCLE: $CYCLE, start $START, end $END"
ANYFILE="false"
if [ ! -e "$TASKNAME/$START.mp4" ] ; then
break
else
CURRENT_TARGET="$TASKNAME/combined-$CYCLE.mp4"
if [ ! -e "$TASKNAME/combined-$CYCLE.mp4" ] ; then
COMMAND="mkvmerge -o $TASKNAME/combined-$CYCLE-tmp.mp4 $TASKNAME/$START.mp4"
SEQ_START=$((START + 1))
for i in `seq $SEQ_START 1 $END` ; do
if [ ! -e "$TASKNAME/$i.mp4" ] ; then
break
fi
ANYFILE="true"
COMMAND="$COMMAND + $TASKNAME/$i.mp4"
done
if [ "$ANYFILE" = "true" ] ; then
$COMMAND
mv "$TASKNAME/combined-$CYCLE-tmp.mp4" "$CURRENT_TARGET"
else
cp "$TASKNAME/$START.mp4" "$TASKNAME/combined-$CYCLE.mp4"
fi
else
echo " -- Current target alredy combined, cycle $CYCLE"
fi
FILES="$FILES $CURRENT_TARGET"
START=$((START + 1000))
END=$((END + 1000))
CYCLE=$((CYCLE + 1))
fi
done
touch "$TASKNAME/combine1.ended"
echo "FILES: $FILES"
else
echo " -- Already combined (first combine)."
fi
if [ ! -e "$TASKNAME/combine-final.ended" ] ; then
FILES="`ls -v $TASKNAME/combined-*.mp4`"
set -- $FILES
if [ "$#" = 1 ] ; then
mv "$1" "$TASKNAME/$TASKNAME.mp4"
elif [ "$#" = 0 ] ; then
failExit "No precombined files found"
else
FIRST="$1"
shift
COMMAND="mkvmerge -o $TASKNAME/$TASKNAME.mp4 $FIRST"
for i in "$@" ; do
COMMAND="$COMMAND + $i"
done
$COMMAND
fi
touch "$TASKNAME/combine-final.ended"
else
echo " -- Already completely combined."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment