#!/bin/bash WORKDIR="$HOME" URL="$1" REGEX="https:\/\/boards\.4chan\.org\/([a-z]+)\/thread\/([0-9]+)#p([0-9]+)" if [[ $URL =~ $REGEX ]]; then BOARD="${BASH_REMATCH[1]}" THREAD="${BASH_REMATCH[2]}" POST="${BASH_REMATCH[3]}" else echo "ERROR. BAD URL" && exit -1 fi API_CALL="https://a.4cdn.org/${BOARD}/thread/${THREAD}.json" POST_JSON="$( curl -s "$API_CALL" | jq -c ".posts[] | select( .no == ${POST} )" )" video="$( jq --argjson post "$POST_JSON" -n '$post.tim' )$( jq --argjson post "$POST_JSON" -n '$post.ext' | tr -d '"' )" audio="$( jq --argjson post "$POST_JSON" -n '$post.filename' | sed -r "s|.*files\.catbox\.moe%2F([a-zA-Z0-9%\.]+).*|\1|g" )" video_file="/tmp/${video}" audio_file="/tmp/${audio}" video_url="https://i.4cdn.org/${BOARD}/${video}" audio_url="https://files.catbox.moe/${audio}" output="${WORKDIR}/${video}" echo -e "\nWriting to ${output}...\n" yt-dlp "$video_url" -o "$video_file" && wget "$audio_url" -O "$audio_file" ffmpeg -y -v 24 -i "${audio_file}" -c:a libvorbis -q:a 4 "${audio_file%%.*}.ogg" && rm -f "${audio_file}" ffmpeg -y -v 24 -i "${video_file}" -i "${audio_file%%.*}.ogg" -c:v copy -c:a libvorbis -filter_complex "[1:0]apad" -shortest "${output}" && rm -f "$video_file" && rm -f "$audio_file" echo -e "\nDone.\n"