-
Notifications
You must be signed in to change notification settings - Fork 0
/
vidsplit
executable file
·80 lines (75 loc) · 1.81 KB
/
vidsplit
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh -e
#
# - download youtube video split by chapter
# Strict deps
type jq >/dev/null 2>&1 || exit 1
type yt-dlp >/dev/null 2>&1 || exit 1
# Util funcs
dateunix()
{
awk 'BEGIN { print srand(); }'
}
t2hms()
{
read -r t
h="$((t / 3600))"
m="$((t % 3600 / 60))"
s="$((t % 60))"
printf "%02d:%02d:%02d\n" "$h" "$m" "$s"
}
# Handle args:
#
# $1 is the URL.
# Unknown arguments are passed as yt-dlp flags (for the download, no the json dump).
url="$1"; shift
YTDLP_OPTS="--ignore-config --embed-thumbnail --geo-bypass --no-call-home"
YTDLP_OPTS_DL=""
while test "$#" -ne 0; do
arg="$1"; shift
case "$arg" in
('--dir')
if test -z "$1"; then
exit 1
fi
dir="$1"; shift
;;
('--number-outputs')
number_outputs=1
;;
(*)
YTDLP_OPTS_DL="${YTDLP_OPTS_DL} ${arg}"
;;
esac
done
# Write video json dump to temp file
temp="${TMPDIR:-/tmp}/vidsplit-$(date -u +'%Y%m%d%H%M%S')-$(seed)"
on_exit()
{
rm "$temp"
}
trap on_exit EXIT
yt-dlp $YTDLP_OPTS --dump-json "$url" | jq >"$temp"
# Create dir for files, cd into it and mv json dump from $temp to dir/dump.json
if test -z "$dir"; then
dir="$(jq -r '.title' "$temp") [$(jq -r '.id' "$temp")].d" # Mimic yt-dlp video format.
fi
mkdir -p "$dir"
cd "$dir" || exit 1
cp "$temp" dump.json
chapters_length="$(jq -r '.chapters | length' dump.json)"
last_i="$(( chapters_length - 1))"
for i in $(seq 0 "$last_i"); do
start_time="$(jq -r ".chapters[${i}].start_time" dump.json | t2hms)"
end_time="$(jq -r ".chapters[${i}].end_time" dump.json | t2hms)"
title="$(jq -r ".chapters[${i}].title" dump.json)"
o="${title}"
if test "$number_outputs" = '1'; then
o="${i}"
fi
printf %s\\n "+=+=+=+=+=+=+=+ ${i} ${start_time} ${end_time} ${title}"
yt-dlp \
$YTDLP_OPTS $YTDLP_OPTS_DL \
-o "${o}.%(ext)s" \
--download-sections "*$start_time-$end_time" \
-- "$url"
done