summaryrefslogtreecommitdiff
path: root/scriptlets/soundpost_dl.sh
blob: 28947b626a46ab6cf1a1c38b8b3aa3dba29be9e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/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"